Thread: strip out certain characters

  1. #1
    Registered User
    Join Date
    Apr 2007
    Location
    Florida
    Posts
    45

    strip out certain characters

    Hello again,
    I am trying to strip first 4 characters from a string, and put the result in a variable buf. this is basically what I have now:
    Code:
    #include <stdio.h>    
    #include <string.h>   
    
    int main(void)
    {
        char buf[10];
        char temp[10];
        int c;
    
        printf("enter string>");
        fgets(temp,10,stdin);
        for (c=0;c<5;c++)
        {
             strcpy(buf,temp[c]);
    
        }
    }
    I keep getting seg fualt. I have tried it other ways but then I get garbage characters or FFFFF.
    Any help is appreciated as always.
    Thanks.
    Brad

  2. #2
    Registered User Noir's Avatar
    Join Date
    Mar 2007
    Posts
    218
    Try using strncpy instead of strcpy. That way you can specify how many characters to copy:
    Code:
    #include <stdio.h>    
    #include <string.h>   
    
    int main(void)
    {
        char buf[10];
        char temp[10];
        int c;
    
        printf("enter string>");
        fgets(temp,10,stdin);
        strncpy( buf, temp, 4 );
        buf[4] = '\0';
        puts( buf );
    }

  3. #3
    Registered User
    Join Date
    Apr 2007
    Location
    Florida
    Posts
    45
    thanks Noir.
    I tried your suggestion, but I need to do it in a loop. Lets say i type hello. I just wan to strip out hell and put it into buf. does that make sense?
    p.s. sorry to start a huge ordeal on scanf.

  4. #4
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Code:
    #include <stdio.h>    
    #include <string.h>   
    
    int main(void)
    {
        char buf[10];
        char temp[10];
        int c;
    
        printf("enter string>");
        fflush(stdout);
        fgets(temp,10,stdin);
        buf[0] = '\0';
        if(strlen(temp) > 4)
        {
            strcpy(buf, temp + 4);
        }
    }
    EDIT: Whoops, I misinterpretted you. I though you wanted to strip off the first four characters and place the rest in buf. But you meant that you only want to KEEP the first four. Noir's code is correct for that.

    But notice the call to fflush(). You need that because there is no newline on the preceding printf(). Which means the stdout buffer might not flush, and you might not see the string. So, you must flush the buffer yourself.
    Last edited by brewbuck; 04-20-2007 at 02:14 PM.

  5. #5
    Registered User Noir's Avatar
    Join Date
    Mar 2007
    Posts
    218
    but I need to do it in a loop
    Not from where I'm sitting. If you copy 4 characters from temp into buf, then if temp has "hello", buf will have "hell" after calling strncpy(). If you also want to remove those characters from temp, you need to shift them away after copying them. You can do that with memmove:
    Code:
    strncpy( buf, temp, 4 );
    buf[4] = '\0';
    memmove( temp, temp + 4, strlen( temp + 4 ) );
    or with a loop:
    Code:
    strncpy( buf, temp, 4 );
    buf[4] = '\0';
    
    for ( c = 0; temp[c]; c++ ) {
      temp[c] = temp[c + 1];
    }
    p.s. sorry to start a huge ordeal on scanf.
    No biggie. It was a good debate until the other guy started showing his true colors.

  6. #6
    Registered User
    Join Date
    Apr 2007
    Location
    Florida
    Posts
    45
    for ( c = 0; temp[c]; c++ ) I have never seen for loop like this. The second part temp[c], I have always seen it with operator.(index<string)What does that mean?

  7. #7
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by bradleyd View Post
    for ( c = 0; temp[c]; c++ ) I have never seen for loop like this. The second part temp[c], I have always seen it with operator.(index<string)What does that mean?
    The loop runs until the middle expression becomes false. temp[c] is just the c'th character of the string (counting from zero). The end of the string is terminated by a zero byte. The value zero is false. Therefore, this loop terminates when it reaches the end of the string.

  8. #8
    Registered User
    Join Date
    Apr 2007
    Location
    Florida
    Posts
    45
    great explanation, thanks.

  9. #9
    Registered User
    Join Date
    Apr 2007
    Location
    Florida
    Posts
    45
    never mind. I was goin to say the loop was cutting of the first character, but I figured it out.

  10. #10
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Quote Originally Posted by bradleyd View Post
    for ( c = 0; temp[c]; c++ ) I have never seen for loop like this. The second part temp[c], I have always seen it with operator.(index<string)What does that mean?
    A string is just an array of characters ending with a '\0':
    Code:
    char tmp[] = { 'h', 'e', 'l', 'l', 'o', '\0' };
    In C, any non-zero value is true, and 0 is false. So tmp[0] is true, tmp[1] is true, etc. all the way to tmp[5] which is false and would break the loop.

    You could write your own strlen() function that just looked like this:
    Code:
    int my_strlen(char *str)
    {
      int i = 0;
    
      while(str[i])
        i++;
    
      return i;
    }
    Last edited by itsme86; 04-20-2007 at 02:58 PM.
    If you understand what you're doing, you're not learning anything.

  11. #11
    Registered User
    Join Date
    Apr 2007
    Location
    Florida
    Posts
    45
    Thanks for all your help, I understand it a little better now.
    Take care.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A development process
    By Noir in forum C Programming
    Replies: 37
    Last Post: 07-10-2011, 10:39 PM
  2. HELP!!!!emergency ~expert please help
    By unknowppl in forum C Programming
    Replies: 1
    Last Post: 08-19-2008, 07:35 AM
  3. Replies: 10
    Last Post: 07-10-2008, 03:45 PM
  4. How do you check how many characters a user has entered?
    By engstudent363 in forum C Programming
    Replies: 5
    Last Post: 04-08-2008, 06:05 AM
  5. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 04:54 PM