How to reset a string in C












-1














So, I have this string
char currentString[212] = { 0 };



and after I'm using it once, I want to reset it.
I triend many ways, such as:



for (int k = 0; k < strlen(currentString); k++)
{
currentString[k] = '';
}


but it won't go over the loop more than once, and it give '' only to the first char, the rest remain the same.



and I also tried:



currentString[0] = '';


yet I get the same result.



any suggestions for what can I do?
thanks!










share|improve this question


















  • 1




    Can you explain why you want to do this? What's your outer problem? (The strlen function determines the length of a string based on its value. Calling strlen repeatedly while you're changing the string's value doesn't make much sense.) You can use memset, but there's a good chance you don't need to.
    – David Schwartz
    yesterday








  • 1




    Don't forget that char strings in C are really called null-terminated byte strings. That null-terminator is what you write to the first position, effectively making the string "empty". The rest of the data after the null-terminator is unused and doesn't matter.
    – Some programmer dude
    yesterday












  • I want to have a fresh new String so I could assign new character to. Obviously I prefer using the "currentString[0] = '';" method
    – Idan
    yesterday










  • How about just strcpy(currentString, newContents)? Why do you need to "reset" it? What is the use-case for that? What is the real problem that is supposed to solve?
    – Some programmer dude
    yesterday
















-1














So, I have this string
char currentString[212] = { 0 };



and after I'm using it once, I want to reset it.
I triend many ways, such as:



for (int k = 0; k < strlen(currentString); k++)
{
currentString[k] = '';
}


but it won't go over the loop more than once, and it give '' only to the first char, the rest remain the same.



and I also tried:



currentString[0] = '';


yet I get the same result.



any suggestions for what can I do?
thanks!










share|improve this question


















  • 1




    Can you explain why you want to do this? What's your outer problem? (The strlen function determines the length of a string based on its value. Calling strlen repeatedly while you're changing the string's value doesn't make much sense.) You can use memset, but there's a good chance you don't need to.
    – David Schwartz
    yesterday








  • 1




    Don't forget that char strings in C are really called null-terminated byte strings. That null-terminator is what you write to the first position, effectively making the string "empty". The rest of the data after the null-terminator is unused and doesn't matter.
    – Some programmer dude
    yesterday












  • I want to have a fresh new String so I could assign new character to. Obviously I prefer using the "currentString[0] = '';" method
    – Idan
    yesterday










  • How about just strcpy(currentString, newContents)? Why do you need to "reset" it? What is the use-case for that? What is the real problem that is supposed to solve?
    – Some programmer dude
    yesterday














-1












-1








-1







So, I have this string
char currentString[212] = { 0 };



and after I'm using it once, I want to reset it.
I triend many ways, such as:



for (int k = 0; k < strlen(currentString); k++)
{
currentString[k] = '';
}


but it won't go over the loop more than once, and it give '' only to the first char, the rest remain the same.



and I also tried:



currentString[0] = '';


yet I get the same result.



any suggestions for what can I do?
thanks!










share|improve this question













So, I have this string
char currentString[212] = { 0 };



and after I'm using it once, I want to reset it.
I triend many ways, such as:



for (int k = 0; k < strlen(currentString); k++)
{
currentString[k] = '';
}


but it won't go over the loop more than once, and it give '' only to the first char, the rest remain the same.



and I also tried:



currentString[0] = '';


yet I get the same result.



any suggestions for what can I do?
thanks!







c string






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked yesterday









Idan

303




303








  • 1




    Can you explain why you want to do this? What's your outer problem? (The strlen function determines the length of a string based on its value. Calling strlen repeatedly while you're changing the string's value doesn't make much sense.) You can use memset, but there's a good chance you don't need to.
    – David Schwartz
    yesterday








  • 1




    Don't forget that char strings in C are really called null-terminated byte strings. That null-terminator is what you write to the first position, effectively making the string "empty". The rest of the data after the null-terminator is unused and doesn't matter.
    – Some programmer dude
    yesterday












  • I want to have a fresh new String so I could assign new character to. Obviously I prefer using the "currentString[0] = '';" method
    – Idan
    yesterday










  • How about just strcpy(currentString, newContents)? Why do you need to "reset" it? What is the use-case for that? What is the real problem that is supposed to solve?
    – Some programmer dude
    yesterday














  • 1




    Can you explain why you want to do this? What's your outer problem? (The strlen function determines the length of a string based on its value. Calling strlen repeatedly while you're changing the string's value doesn't make much sense.) You can use memset, but there's a good chance you don't need to.
    – David Schwartz
    yesterday








  • 1




    Don't forget that char strings in C are really called null-terminated byte strings. That null-terminator is what you write to the first position, effectively making the string "empty". The rest of the data after the null-terminator is unused and doesn't matter.
    – Some programmer dude
    yesterday












  • I want to have a fresh new String so I could assign new character to. Obviously I prefer using the "currentString[0] = '';" method
    – Idan
    yesterday










  • How about just strcpy(currentString, newContents)? Why do you need to "reset" it? What is the use-case for that? What is the real problem that is supposed to solve?
    – Some programmer dude
    yesterday








1




1




Can you explain why you want to do this? What's your outer problem? (The strlen function determines the length of a string based on its value. Calling strlen repeatedly while you're changing the string's value doesn't make much sense.) You can use memset, but there's a good chance you don't need to.
– David Schwartz
yesterday






Can you explain why you want to do this? What's your outer problem? (The strlen function determines the length of a string based on its value. Calling strlen repeatedly while you're changing the string's value doesn't make much sense.) You can use memset, but there's a good chance you don't need to.
– David Schwartz
yesterday






1




1




Don't forget that char strings in C are really called null-terminated byte strings. That null-terminator is what you write to the first position, effectively making the string "empty". The rest of the data after the null-terminator is unused and doesn't matter.
– Some programmer dude
yesterday






Don't forget that char strings in C are really called null-terminated byte strings. That null-terminator is what you write to the first position, effectively making the string "empty". The rest of the data after the null-terminator is unused and doesn't matter.
– Some programmer dude
yesterday














I want to have a fresh new String so I could assign new character to. Obviously I prefer using the "currentString[0] = '';" method
– Idan
yesterday




I want to have a fresh new String so I could assign new character to. Obviously I prefer using the "currentString[0] = '';" method
– Idan
yesterday












How about just strcpy(currentString, newContents)? Why do you need to "reset" it? What is the use-case for that? What is the real problem that is supposed to solve?
– Some programmer dude
yesterday




How about just strcpy(currentString, newContents)? Why do you need to "reset" it? What is the use-case for that? What is the real problem that is supposed to solve?
– Some programmer dude
yesterday












5 Answers
5






active

oldest

votes


















1














strlen will find the length by searching for the first occurrence of . So if you want to reset the whole array, you should change strlen(currentString) to sizeof currentString. However, do note that this will not work with pointers.



If you pass the array to a function, you cannot determine the size of the array afterwards, so this will not work:



void foo(char * arr) {
for (int k = 0; k < sizeof arr; k++)
arr[k] = '';
}


Instead you need to do like this:



void foo(char * arr, size_t size) {
for (int k = 0; k < size; k++)
arr[k] = '';
}


But of course there's no reason to write custom functions for this when memset is available.






share|improve this answer























  • it works like a charm. thanks! but why doesnt this "currentString[0] = '';" works?
    – Idan
    yesterday










  • @Idan Depends on what you mean with "works". It does work. It will set the first character to zero, which means that it is a string of zero length.
    – Broman
    yesterday










  • my intention was to make all characters ''
    – Idan
    yesterday










  • @Idan you have special functions to do it.
    – P__J__
    yesterday










  • @Idan Well, then it does not work because you're only instructing it to reset the first character. :)
    – Broman
    yesterday



















0














Imagine char currentString = "abc"; and then running you loop:




  • k = 0

  • initialy strlen(currentString) = 3, there are 3 characters before '' byte. the loop condition k < strlen(currentString) is true


  • k = 0 -> currentString[0] = ''


  • k++ -> k = 1

  • then strlen(currentString) = 0 (as the first byte of currentString is equal to '', there are no characters before '')

  • the loop condition is false k < strlen(currentString) -> 1 < 0


So the loop will always run only one time.



If you want to write only zero bytes to a memory region, use memset



 memset(currentString, 0, sizeof(currentString));


will set the memory region as pointed to by currentString pointer with sizeof(currentString) bytes to zeros.



Setting the first byte to zero:



 currentString[0] = '';


maybe considered enough to "clear a string".






share|improve this answer





























    0














    to zero the whole array



    char arr[SOMESIZE];

    /* ... */

    memset(arr, 0, sizeof(arr));


    pointer - you need to know the size of the allocated memory as sizeof will return the size of the pointer itself only, not the referenced object;



    char *p = malloc(SIZE);
    /* ..... */
    memset(p, 0 , SIZE);





    share|improve this answer





























      0














      Setting the first byte to '' wont clear out the currentString.You may think that because ANSI C thinks that is a string terminator and if you print your string it will show empty.But if you check the second byte you will see the second char from your string. As other's said the best option to wipe out the string is:



      memset(currentString, 0, sizeof(currentString));


      And is way safer and faster.Also in ANSI C 0 and '' are the same.






      share|improve this answer































        0














        It is never a good decision to calculate anything again and again. Instead you should calculate the strlen() only once.
        That being said, in your case, doing so will solve the problem, as the reason it didn't work was that strlen() returned 0 right after the first round, since the length of the string became 0.



        int n = strlen(currentString);
        for (int k = 0; k < n; k++)
        {
        currentString[k] = '';
        }





        share|improve this answer





















          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53943743%2fhow-to-reset-a-string-in-c%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          5 Answers
          5






          active

          oldest

          votes








          5 Answers
          5






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          strlen will find the length by searching for the first occurrence of . So if you want to reset the whole array, you should change strlen(currentString) to sizeof currentString. However, do note that this will not work with pointers.



          If you pass the array to a function, you cannot determine the size of the array afterwards, so this will not work:



          void foo(char * arr) {
          for (int k = 0; k < sizeof arr; k++)
          arr[k] = '';
          }


          Instead you need to do like this:



          void foo(char * arr, size_t size) {
          for (int k = 0; k < size; k++)
          arr[k] = '';
          }


          But of course there's no reason to write custom functions for this when memset is available.






          share|improve this answer























          • it works like a charm. thanks! but why doesnt this "currentString[0] = '';" works?
            – Idan
            yesterday










          • @Idan Depends on what you mean with "works". It does work. It will set the first character to zero, which means that it is a string of zero length.
            – Broman
            yesterday










          • my intention was to make all characters ''
            – Idan
            yesterday










          • @Idan you have special functions to do it.
            – P__J__
            yesterday










          • @Idan Well, then it does not work because you're only instructing it to reset the first character. :)
            – Broman
            yesterday
















          1














          strlen will find the length by searching for the first occurrence of . So if you want to reset the whole array, you should change strlen(currentString) to sizeof currentString. However, do note that this will not work with pointers.



          If you pass the array to a function, you cannot determine the size of the array afterwards, so this will not work:



          void foo(char * arr) {
          for (int k = 0; k < sizeof arr; k++)
          arr[k] = '';
          }


          Instead you need to do like this:



          void foo(char * arr, size_t size) {
          for (int k = 0; k < size; k++)
          arr[k] = '';
          }


          But of course there's no reason to write custom functions for this when memset is available.






          share|improve this answer























          • it works like a charm. thanks! but why doesnt this "currentString[0] = '';" works?
            – Idan
            yesterday










          • @Idan Depends on what you mean with "works". It does work. It will set the first character to zero, which means that it is a string of zero length.
            – Broman
            yesterday










          • my intention was to make all characters ''
            – Idan
            yesterday










          • @Idan you have special functions to do it.
            – P__J__
            yesterday










          • @Idan Well, then it does not work because you're only instructing it to reset the first character. :)
            – Broman
            yesterday














          1












          1








          1






          strlen will find the length by searching for the first occurrence of . So if you want to reset the whole array, you should change strlen(currentString) to sizeof currentString. However, do note that this will not work with pointers.



          If you pass the array to a function, you cannot determine the size of the array afterwards, so this will not work:



          void foo(char * arr) {
          for (int k = 0; k < sizeof arr; k++)
          arr[k] = '';
          }


          Instead you need to do like this:



          void foo(char * arr, size_t size) {
          for (int k = 0; k < size; k++)
          arr[k] = '';
          }


          But of course there's no reason to write custom functions for this when memset is available.






          share|improve this answer














          strlen will find the length by searching for the first occurrence of . So if you want to reset the whole array, you should change strlen(currentString) to sizeof currentString. However, do note that this will not work with pointers.



          If you pass the array to a function, you cannot determine the size of the array afterwards, so this will not work:



          void foo(char * arr) {
          for (int k = 0; k < sizeof arr; k++)
          arr[k] = '';
          }


          Instead you need to do like this:



          void foo(char * arr, size_t size) {
          for (int k = 0; k < size; k++)
          arr[k] = '';
          }


          But of course there's no reason to write custom functions for this when memset is available.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited yesterday

























          answered yesterday









          Broman

          6,262112241




          6,262112241












          • it works like a charm. thanks! but why doesnt this "currentString[0] = '';" works?
            – Idan
            yesterday










          • @Idan Depends on what you mean with "works". It does work. It will set the first character to zero, which means that it is a string of zero length.
            – Broman
            yesterday










          • my intention was to make all characters ''
            – Idan
            yesterday










          • @Idan you have special functions to do it.
            – P__J__
            yesterday










          • @Idan Well, then it does not work because you're only instructing it to reset the first character. :)
            – Broman
            yesterday


















          • it works like a charm. thanks! but why doesnt this "currentString[0] = '';" works?
            – Idan
            yesterday










          • @Idan Depends on what you mean with "works". It does work. It will set the first character to zero, which means that it is a string of zero length.
            – Broman
            yesterday










          • my intention was to make all characters ''
            – Idan
            yesterday










          • @Idan you have special functions to do it.
            – P__J__
            yesterday










          • @Idan Well, then it does not work because you're only instructing it to reset the first character. :)
            – Broman
            yesterday
















          it works like a charm. thanks! but why doesnt this "currentString[0] = '';" works?
          – Idan
          yesterday




          it works like a charm. thanks! but why doesnt this "currentString[0] = '';" works?
          – Idan
          yesterday












          @Idan Depends on what you mean with "works". It does work. It will set the first character to zero, which means that it is a string of zero length.
          – Broman
          yesterday




          @Idan Depends on what you mean with "works". It does work. It will set the first character to zero, which means that it is a string of zero length.
          – Broman
          yesterday












          my intention was to make all characters ''
          – Idan
          yesterday




          my intention was to make all characters ''
          – Idan
          yesterday












          @Idan you have special functions to do it.
          – P__J__
          yesterday




          @Idan you have special functions to do it.
          – P__J__
          yesterday












          @Idan Well, then it does not work because you're only instructing it to reset the first character. :)
          – Broman
          yesterday




          @Idan Well, then it does not work because you're only instructing it to reset the first character. :)
          – Broman
          yesterday













          0














          Imagine char currentString = "abc"; and then running you loop:




          • k = 0

          • initialy strlen(currentString) = 3, there are 3 characters before '' byte. the loop condition k < strlen(currentString) is true


          • k = 0 -> currentString[0] = ''


          • k++ -> k = 1

          • then strlen(currentString) = 0 (as the first byte of currentString is equal to '', there are no characters before '')

          • the loop condition is false k < strlen(currentString) -> 1 < 0


          So the loop will always run only one time.



          If you want to write only zero bytes to a memory region, use memset



           memset(currentString, 0, sizeof(currentString));


          will set the memory region as pointed to by currentString pointer with sizeof(currentString) bytes to zeros.



          Setting the first byte to zero:



           currentString[0] = '';


          maybe considered enough to "clear a string".






          share|improve this answer


























            0














            Imagine char currentString = "abc"; and then running you loop:




            • k = 0

            • initialy strlen(currentString) = 3, there are 3 characters before '' byte. the loop condition k < strlen(currentString) is true


            • k = 0 -> currentString[0] = ''


            • k++ -> k = 1

            • then strlen(currentString) = 0 (as the first byte of currentString is equal to '', there are no characters before '')

            • the loop condition is false k < strlen(currentString) -> 1 < 0


            So the loop will always run only one time.



            If you want to write only zero bytes to a memory region, use memset



             memset(currentString, 0, sizeof(currentString));


            will set the memory region as pointed to by currentString pointer with sizeof(currentString) bytes to zeros.



            Setting the first byte to zero:



             currentString[0] = '';


            maybe considered enough to "clear a string".






            share|improve this answer
























              0












              0








              0






              Imagine char currentString = "abc"; and then running you loop:




              • k = 0

              • initialy strlen(currentString) = 3, there are 3 characters before '' byte. the loop condition k < strlen(currentString) is true


              • k = 0 -> currentString[0] = ''


              • k++ -> k = 1

              • then strlen(currentString) = 0 (as the first byte of currentString is equal to '', there are no characters before '')

              • the loop condition is false k < strlen(currentString) -> 1 < 0


              So the loop will always run only one time.



              If you want to write only zero bytes to a memory region, use memset



               memset(currentString, 0, sizeof(currentString));


              will set the memory region as pointed to by currentString pointer with sizeof(currentString) bytes to zeros.



              Setting the first byte to zero:



               currentString[0] = '';


              maybe considered enough to "clear a string".






              share|improve this answer












              Imagine char currentString = "abc"; and then running you loop:




              • k = 0

              • initialy strlen(currentString) = 3, there are 3 characters before '' byte. the loop condition k < strlen(currentString) is true


              • k = 0 -> currentString[0] = ''


              • k++ -> k = 1

              • then strlen(currentString) = 0 (as the first byte of currentString is equal to '', there are no characters before '')

              • the loop condition is false k < strlen(currentString) -> 1 < 0


              So the loop will always run only one time.



              If you want to write only zero bytes to a memory region, use memset



               memset(currentString, 0, sizeof(currentString));


              will set the memory region as pointed to by currentString pointer with sizeof(currentString) bytes to zeros.



              Setting the first byte to zero:



               currentString[0] = '';


              maybe considered enough to "clear a string".







              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered yesterday









              Kamil Cuk

              8,8381523




              8,8381523























                  0














                  to zero the whole array



                  char arr[SOMESIZE];

                  /* ... */

                  memset(arr, 0, sizeof(arr));


                  pointer - you need to know the size of the allocated memory as sizeof will return the size of the pointer itself only, not the referenced object;



                  char *p = malloc(SIZE);
                  /* ..... */
                  memset(p, 0 , SIZE);





                  share|improve this answer


























                    0














                    to zero the whole array



                    char arr[SOMESIZE];

                    /* ... */

                    memset(arr, 0, sizeof(arr));


                    pointer - you need to know the size of the allocated memory as sizeof will return the size of the pointer itself only, not the referenced object;



                    char *p = malloc(SIZE);
                    /* ..... */
                    memset(p, 0 , SIZE);





                    share|improve this answer
























                      0












                      0








                      0






                      to zero the whole array



                      char arr[SOMESIZE];

                      /* ... */

                      memset(arr, 0, sizeof(arr));


                      pointer - you need to know the size of the allocated memory as sizeof will return the size of the pointer itself only, not the referenced object;



                      char *p = malloc(SIZE);
                      /* ..... */
                      memset(p, 0 , SIZE);





                      share|improve this answer












                      to zero the whole array



                      char arr[SOMESIZE];

                      /* ... */

                      memset(arr, 0, sizeof(arr));


                      pointer - you need to know the size of the allocated memory as sizeof will return the size of the pointer itself only, not the referenced object;



                      char *p = malloc(SIZE);
                      /* ..... */
                      memset(p, 0 , SIZE);






                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered yesterday









                      P__J__

                      8,8922723




                      8,8922723























                          0














                          Setting the first byte to '' wont clear out the currentString.You may think that because ANSI C thinks that is a string terminator and if you print your string it will show empty.But if you check the second byte you will see the second char from your string. As other's said the best option to wipe out the string is:



                          memset(currentString, 0, sizeof(currentString));


                          And is way safer and faster.Also in ANSI C 0 and '' are the same.






                          share|improve this answer




























                            0














                            Setting the first byte to '' wont clear out the currentString.You may think that because ANSI C thinks that is a string terminator and if you print your string it will show empty.But if you check the second byte you will see the second char from your string. As other's said the best option to wipe out the string is:



                            memset(currentString, 0, sizeof(currentString));


                            And is way safer and faster.Also in ANSI C 0 and '' are the same.






                            share|improve this answer


























                              0












                              0








                              0






                              Setting the first byte to '' wont clear out the currentString.You may think that because ANSI C thinks that is a string terminator and if you print your string it will show empty.But if you check the second byte you will see the second char from your string. As other's said the best option to wipe out the string is:



                              memset(currentString, 0, sizeof(currentString));


                              And is way safer and faster.Also in ANSI C 0 and '' are the same.






                              share|improve this answer














                              Setting the first byte to '' wont clear out the currentString.You may think that because ANSI C thinks that is a string terminator and if you print your string it will show empty.But if you check the second byte you will see the second char from your string. As other's said the best option to wipe out the string is:



                              memset(currentString, 0, sizeof(currentString));


                              And is way safer and faster.Also in ANSI C 0 and '' are the same.







                              share|improve this answer














                              share|improve this answer



                              share|improve this answer








                              edited yesterday

























                              answered yesterday









                              Loghin Alexandru

                              133




                              133























                                  0














                                  It is never a good decision to calculate anything again and again. Instead you should calculate the strlen() only once.
                                  That being said, in your case, doing so will solve the problem, as the reason it didn't work was that strlen() returned 0 right after the first round, since the length of the string became 0.



                                  int n = strlen(currentString);
                                  for (int k = 0; k < n; k++)
                                  {
                                  currentString[k] = '';
                                  }





                                  share|improve this answer


























                                    0














                                    It is never a good decision to calculate anything again and again. Instead you should calculate the strlen() only once.
                                    That being said, in your case, doing so will solve the problem, as the reason it didn't work was that strlen() returned 0 right after the first round, since the length of the string became 0.



                                    int n = strlen(currentString);
                                    for (int k = 0; k < n; k++)
                                    {
                                    currentString[k] = '';
                                    }





                                    share|improve this answer
























                                      0












                                      0








                                      0






                                      It is never a good decision to calculate anything again and again. Instead you should calculate the strlen() only once.
                                      That being said, in your case, doing so will solve the problem, as the reason it didn't work was that strlen() returned 0 right after the first round, since the length of the string became 0.



                                      int n = strlen(currentString);
                                      for (int k = 0; k < n; k++)
                                      {
                                      currentString[k] = '';
                                      }





                                      share|improve this answer












                                      It is never a good decision to calculate anything again and again. Instead you should calculate the strlen() only once.
                                      That being said, in your case, doing so will solve the problem, as the reason it didn't work was that strlen() returned 0 right after the first round, since the length of the string became 0.



                                      int n = strlen(currentString);
                                      for (int k = 0; k < n; k++)
                                      {
                                      currentString[k] = '';
                                      }






                                      share|improve this answer












                                      share|improve this answer



                                      share|improve this answer










                                      answered yesterday









                                      Michael Haephrati

                                      1,4781528




                                      1,4781528






























                                          draft saved

                                          draft discarded




















































                                          Thanks for contributing an answer to Stack Overflow!


                                          • Please be sure to answer the question. Provide details and share your research!

                                          But avoid



                                          • Asking for help, clarification, or responding to other answers.

                                          • Making statements based on opinion; back them up with references or personal experience.


                                          To learn more, see our tips on writing great answers.





                                          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                                          Please pay close attention to the following guidance:


                                          • Please be sure to answer the question. Provide details and share your research!

                                          But avoid



                                          • Asking for help, clarification, or responding to other answers.

                                          • Making statements based on opinion; back them up with references or personal experience.


                                          To learn more, see our tips on writing great answers.




                                          draft saved


                                          draft discarded














                                          StackExchange.ready(
                                          function () {
                                          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53943743%2fhow-to-reset-a-string-in-c%23new-answer', 'question_page');
                                          }
                                          );

                                          Post as a guest















                                          Required, but never shown





















































                                          Required, but never shown














                                          Required, but never shown












                                          Required, but never shown







                                          Required, but never shown

































                                          Required, but never shown














                                          Required, but never shown












                                          Required, but never shown







                                          Required, but never shown







                                          Popular posts from this blog

                                          Mossoró

                                          Error while reading .h5 file using the rhdf5 package in R

                                          Pushsharp Apns notification error: 'InvalidToken'