Thread: how do I do this??

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    63

    how do I do this??

    hi,
    Lets say I have format: FN MI LN
    Now..how do I initialize the space after MI to '\0' in a loop for varying lengths of FN MI LN..
    Btw..FN is First name..MI is middle initial..& LN is Last Name..
    Please Help.
    A

  2. #2
    Unregistered
    Guest
    I don't understand. Could you try again?

  3. #3
    Registered User sean345's Avatar
    Join Date
    Mar 2002
    Posts
    346
    You could loop through the string and keep a int that gives you the position. If that position String[Pos] is a space then set it equal to a '\0'.
    Code:
    while(Pos<strlen(String){
    if(String[Pos]=='\0';){
         String[Pos] = '\0';
    }
    Pos++;
    }
    - Sean
    If cities were built like software is built, the first woodpecker to come along would level civilization.
    Black Frog Studios

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231

    Re: how do I do this??

    Originally posted by aspand
    hi,
    Lets say I have format: FN MI LN
    Now..how do I initialize the space after MI to '\0' in a loop for varying lengths of FN MI LN..
    Btw..FN is First name..MI is middle initial..& LN is Last Name..
    Please Help.
    A
    I presume you mean something like this:
    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main(void)
    {
    	char name[] = "Eric T Cartman";
    	char *ptr;
    	
    	printf ("Name is currently: %s\n", name);
    	
    	if ((ptr = strrchr(name, ' ')) != NULL)
    		*ptr = '\0';		
    		
    	printf ("Name is now: %s\n", name);
    	return (0);
    }
    /* Program output:
    Name is currently: Eric T Cartman
    Name is now: Eric T
    */
    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