How can I reset the value of a string at the end of a loop?
How can I reset the value of a string at the end of a loop?
str = null;
or
str = "";
but it depends on what you mean by reset, and what type of string you are using.
I am using a string array to take input from the user.
char str1[100]
its:
there needs to be a space between thereCode:str = " ";
Thank you.
well that's not gonna give you an empty string, it's gonna give you a space and then the null char.Originally posted by juancardenas
its:
there needs to be a space between thereCode:str = " ";
if you do str = ""; Then it will be correct because the null char will be in the first position.
OK. When I enter that it does not work. Can you look at my code.
I problably have the syntax wrong.
Code:#include <stdio.h> #include <string.h> #include <ctype.h> #define str_length 100 void main() { char str1[100]; char str2[100]; int count1; int count2; char quit = 'n'; char more = 'y'; /* variable to continue with the loop y||Y */ printf("Please enter the first word\n"); gets(str1); printf("Please enter the second word\n"); gets(str2); do { count1 = strlen(str1); count2 = strlen(str2); /* concatenate str2 to end of str1 */ if(count1 == count2 ||count1 < count2 ) { printf(" %s %s and \n", str1,str2); printf(" %s %s\n",strrev(str1), str2); } else /* concatenate str1 to end of str2 */ { (count1 > count2); printf(" %s %s and \n", str2,str1); printf(" %s %s \n",strrev(str2),str1); } printf("Would you like to continue? y or n:\n"); scanf("%c", &quit); if(quit == 'y' || quit == 'Y') { str1 = " "; str2 = " "; printf("Please enter the first word.\n"); /* ask for 1st word */ gets(str1); printf("Please enter the second word.\n"); /* ask for 2nd word */ gets(str2); } }while(quit == 'y' || quit == 'Y'); }
Please use code tags next time!!
1) Use code tags.
2) main returns an integer, not void
3) gets(str1); - gets is unsafe - use fgets instead
4) (count1 > count2); - That line doesn't do anything.
5) str1 = " " - You can't assign a string like that. Use strcpy(str1, "") or str1[0] = '\0';
-Govtcheez
[email protected]
Thank you.
You can reset value of the string with function memset.Originally posted by john_murphy69
How can I reset the value of a string at the end of a loop?
Example:
memset ( str1, '\0', 100);
Current projects:
1) User Interface Development Kit (C++)
2) HTML SDK (C++)
3) Classes (C++)
4) INI Editor (Delphi)
Neither this:
str = null;
or this:
str = " ";
reset the value of a string. I have to assume str is a pointer, as it can't be an array, therefore the only thing being done in those two lines of code is to change the pointer to point to either NULL or a string literal. Neither touch the data being pointed to.
To have the affect of emptying a char array, which is what is being used in the code provided, you can use this:
myarray[0] = '\0';
Or it you want to fill the complete array with \0, use memset.
Having said all that, in the code provided, there is no need to empty the array anyway, and the next call to gets() (which you should not be using) will overwrite whats already there.
When all else fails, read the instructions.
If you're posting code, use code tags: [code] /* insert code here */ [/code]