reset a string?

This is a discussion on reset a string? within the C Programming forums, part of the General Programming Boards category; How can I reset the value of a string at the end of a loop?...

  1. #1
    Registered User
    Join Date
    Jan 2003
    Posts
    42

    reset a string?

    How can I reset the value of a string at the end of a loop?

  2. #2
    booyakasha
    Join Date
    Nov 2002
    Posts
    208
    str = null;

    or

    str = "";

    but it depends on what you mean by reset, and what type of string you are using.

  3. #3
    Registered User
    Join Date
    Jan 2003
    Posts
    42
    I am using a string array to take input from the user.
    char str1[100]

  4. #4
    Registered User
    Join Date
    Feb 2003
    Posts
    6
    its:
    Code:
    str = " ";
    there needs to be a space between there

  5. #5
    Registered User
    Join Date
    Jan 2003
    Posts
    42
    Thank you.

  6. #6
    booyakasha
    Join Date
    Nov 2002
    Posts
    208
    Originally posted by juancardenas
    its:
    Code:
    str = " ";
    there needs to be a space between there
    well that's not gonna give you an empty string, it's gonna give you a space and then the null char.

    if you do str = ""; Then it will be correct because the null char will be in the first position.

  7. #7
    Registered User
    Join Date
    Jan 2003
    Posts
    42
    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');
    	
    
    
    	
    }

  8. #8
    Black Mage Extraordinaire VegasSte's Avatar
    Join Date
    Oct 2002
    Posts
    167
    Please use code tags next time!!

  9. #9
    Mayor of Awesometown Govtcheez's Avatar
    Join Date
    Aug 2001
    Location
    MI
    Posts
    8,825
    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';

  10. #10
    Registered User
    Join Date
    Jan 2003
    Posts
    42
    Thank you.

  11. #11
    Code Warrior
    Join Date
    Nov 2001
    Posts
    669

    Re: reset a string?

    Originally posted by john_murphy69
    How can I reset the value of a string at the end of a loop?
    You can reset value of the string with function memset.
    Example:
    memset ( str1, '\0', 100);
    Current projects:
    1) User Interface Development Kit (C++)
    2) HTML SDK (C++)
    3) Classes (C++)
    4) INI Editor (Delphi)

  12. #12
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    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]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 07:02 AM
  2. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  3. Something is wrong with this menu...
    By DarkViper in forum Windows Programming
    Replies: 2
    Last Post: 12-14-2002, 11:06 PM
  4. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 07:49 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21