Thread: help splitting strings in C

  1. #1
    Registered User
    Join Date
    Jul 2011
    Posts
    7

    help splitting strings in C

    so i kno how to split a string and use a portion of it after the 0 marker but wut if i wanted to use the first half of a string instead of the second half how do i go about do tthat? heres an my code. if u look at the last line im trying to split string one and display the first half and the second half of string to together
    Code:
    #include <stdio.h> 
    #include <string.h> 
    
    int main() 
    
    { 
    
    char string1[255]; 
    char string2[255]; 
    /*not sure if the \0 rule applies to individual char strings so i made it 255 instead of 254*/
    
    int length1; 
    int length2;
    
    
    
        printf("Program: String Processing\n");
        
        printf("Author: VeXxRaptor\n\n");
        
    /*  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  */
    
    
    
    
    
    printf("Enter the string1: "); 
    
    gets(string1); 
    
    printf("Enter the string2: "); 
    
    gets(string2);
    
    length1=strlen(string1); 
    
    length2=strlen(string2); 
    
    printf("\nString 1 is %d bytes long, and string2 is %d bytes long.\n",length1,length2); 
    /*Prints variables 1 & 2 total string length to the screen*/
    
    printf("%s%s", string1, string2 +10);
    
        getchar();
        
        return 0;
    
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    The first half of the string doesn't move or anything, it's still there, with the same name.

    (EDIT: And also, read the FAQ on gets.)

  3. #3
    Registered User
    Join Date
    Jul 2011
    Posts
    7
    thanks for the gets Faqs suggestion...
    btw...

    example...

    this is a string /*string 1*/
    this is also a string /*string 2*/

    this is /*being part of string 1*/
    so a string /*being part of string 2*/
    this is so a string /*being output*/

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Y'know what... that's one of the most complex things you can do in C... Splitting strings up in to arbitrary chunks is just not easy.

    1) Copy the first string to a new buffer with strcpy().
    2) Locate the required breakpoint in the first string using some combination of strstr() and/or strchr()
    3) Locate the breakpoint in the second string like you did in #2
    4) strcpy() the second breakpoint to the first...

    Left of first string is now melded with right of second string.

    And no, that's not easily done.

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    I assumed, since you mentioned it in your first post, that you knew you had to put a \0 character in your string to split it up. Once you do so, then you are done.

  6. #6
    Registered User
    Join Date
    Jul 2011
    Posts
    7
    how does \0 slit up my string? i thought that was only used as a placement to end my string... something that the sompiler will do auto if i dont right?


    also @Tator this project is about midway through m programming course and the rest of the assignment came to easy... so i guess i found the challenge thanks man, and can someone post a link to the gets forum page please...
    Last edited by David Cloud; 07-14-2011 at 10:16 PM.

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by David Cloud View Post
    how does \0 slit up my string? i thought that was only used as a placement to end my string... something that the sompiler will do auto if i dont right?
    Right. So if you want to end a string in order to, say, make it into two strings, then guess what gets used?

  8. #8
    Registered User
    Join Date
    Jul 2011
    Posts
    7
    how does this apply to the user input function then? i mean can i tell the program to cut it halfway through wutever text the user inputs?

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Why not? It's your job to put the \0 where you think it belongs, so the first order of business is to figure out where you think it belongs.

  10. #10
    Registered User
    Join Date
    Jul 2011
    Posts
    7
    ??? and how do i manage that? could u use an example code?

  11. #11
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Well, I don't know how you want to split your string up.
    Code:
    char presto[] = "Hey presto";
    printf("%s\n", presto);
    presto[3] = '\0'; /* split presto into two pieces */
    printf("Part 1: %s\n", presto);
    printf("Part 2: %s\n", presto+4);

  12. #12
    Registered User
    Join Date
    Jul 2011
    Posts
    7
    this is the exact wording i am using for my test inputs. but they arent prest strings they are from my user input?

    Quote Originally Posted by David Cloud View Post
    thanks for the gets Faqs suggestion...

    example...

    this is a string /*string 1*/
    this is also a string /*string 2*/

    this is /*being part of string 1*/
    so a string /*being part of string 2*/
    this is so a string /*being output*/
    Last edited by David Cloud; 07-14-2011 at 10:39 PM.

  13. #13
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    So? What difference does that make?

  14. #14
    Registered User
    Join Date
    Jul 2011
    Posts
    7
    you know what man ur right it doesnt matter ha ha.... thanks it worked... now i can move on and learn more fascinating things!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Splitting a vector of strings into tokens?
    By Flamefury in forum C++ Programming
    Replies: 5
    Last Post: 03-09-2010, 07:53 PM
  2. splitting strings...
    By newbie_socketsp in forum C Programming
    Replies: 4
    Last Post: 08-15-2008, 10:17 AM
  3. splitting up names (strings)
    By Butters007 in forum C Programming
    Replies: 8
    Last Post: 12-07-2006, 01:13 AM
  4. Splitting strings into words.
    By 0x7f in forum C++ Programming
    Replies: 6
    Last Post: 03-31-2003, 03:49 PM
  5. splitting strings
    By Unreg1 in forum C++ Programming
    Replies: 3
    Last Post: 12-29-2002, 11:02 PM