Thread: String Manipulation

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    128

    String Manipulation

    It's easy to find tutorials on c++ but "c" is not as easy.

    I'm looking for any tutorials on string mainpulation in c. I have a large text area (a long string) and I need to search for a unique 4-letter combination and then strip out everything after the trailing spaces.

    example:
    "123 64 grg 765 myval364 jgfk34"
    I need to grab myval364.

    I can do this in c++ but I don't know much about c. Again, any tutorials would be great.

    Thanks.

  2. #2
    FOX
    Join Date
    May 2005
    Posts
    188
    Use strtok.

  3. #3
    Registered User Dante Shamest's Avatar
    Join Date
    Apr 2003
    Posts
    970
    I need to search for a unique 4-letter combination and then strip out everything after the trailing spaces.

    example:
    "123 64 grg 765 myval364 jgfk34"
    I need to grab myval364.
    Where is the "unique 4-letter combination" in that string ?

  4. #4
    Registered User
    Join Date
    Aug 2005
    Posts
    128
    I'll assume then whatever string manipulation I can do in c++, I can do in c with the same key words?

  5. #5
    Registered User cbastard's Avatar
    Join Date
    Jul 2005
    Location
    India
    Posts
    167
    use strstr.
    assume anything watever you want after all its your assumption.But dont assume your assumption will be right.atmost wat will happen just an error.then make a new assumption

  6. #6
    Registered User
    Join Date
    Aug 2005
    Posts
    128
    thanks, I'll assume this will all work out.

    oops, my fault on the "4-char" and then I give myval364 as an example.

  7. #7
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    555
    Quote Originally Posted by FoodDude

    example:
    "123 64 grg 765 myval364 jgfk34"
    I need to grab myval364.
    How are you determining myval364 is what you need? The 5th word in the string? something beginning with myval?

    I'm personally confused, although it seems like you already got what you need, so nevermind.

  8. #8
    Registered User
    Join Date
    Aug 2005
    Posts
    128
    one last thing. c++ has std::string. Does c have strings or am I limited to chars?

  9. #9
    Registered User Dante Shamest's Avatar
    Join Date
    Apr 2003
    Posts
    970
    Does c have strings or am I limited to chars?
    Yes, you're pretty much limited to manipulating zero-terminated char arrays. With the standard C functions at least.

  10. #10
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    your best friends in C are strstr() which someone else has already mentioned. Then if you want to delete that string, use memmove() to move everything from one spot to another.
    Example:
    Code:
    int len;
    char* ptr;
    char str[] = "123 64 grg 765 myval364 jgfk34";
    // find text to be removed
    ptr = strstr(str,"myval364");
    if(ptr != 0)
    {
    	len = strlen(ptr+8);
       // the string exists, 
       memmove(ptr, ptr+8,strlen(ptr+8));
       // now null-terminate the string
       ptr[len] = 0;
    }
    a briefer way to do it uses strcpy().
    Code:
    char str[] = "123 64 grg 765 myval364 jgfk34";
    // find text to be removed
    char *ptr = strstr(str,"myval364");
    if(ptr != 0)
    {
       // the string exists, so delete it
       strcpy(ptr, ptr+8);
    }
    Last edited by Ancient Dragon; 09-28-2005 at 04:02 PM.

  11. #11
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    555
    a briefer way to do it uses strcpy().
    Code:
    char str[] = "123 64 grg 765 myval364 jgfk34";
    // find text to be removed
    char *ptr = strstr(str,"myval364");
    if(ptr != 0)
    {
       // the string exists, so delete it
       strcpy(ptr, ptr+8);
    }
    http://www.lira.dist.unige.it/teachi...files/man2.pdf
    DESCRIPTION
    The strcpy() function copies the string pointed to by src (including the terminating `\0' character) to the
    array pointed to by dest. The strings may not overlap, and the destination string dest must be large
    enough to receive the copy.

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, 06:02 AM
  2. Compile Error that i dont understand
    By bobthebullet990 in forum C++ Programming
    Replies: 5
    Last Post: 05-05-2006, 09:19 AM
  3. Replies: 4
    Last Post: 03-03-2006, 02:11 AM
  4. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 10:33 PM
  5. string manipulation
    By SPEKTRUM in forum Linux Programming
    Replies: 3
    Last Post: 01-26-2002, 11:41 AM