Thread: Split a string(C)

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    14

    Split a string(C)

    hello,
    i have a problem to split a string.
    i have this sentence :
    char str1[100] = "hello this is my sentence and max(a,b) is my method";

    i want to to split the sentence from the max(a,b) to 2 strings.

    with :
    char *str2;
    str2 = strstr(str1,"max");
    now i took the right side of the sentence to str2.
    bow how can i take the left side to other string?
    maby how can i get the place of max by numbers(lenght)?
    i didnt find any str command to do this.
    i`ll happy if someone help me.
    Thanks ,
    Or.

  2. #2
    Registered User
    Join Date
    Jun 2009
    Posts
    120
    Length of str1 before str2 starts:
    Code:
    int len = str2 - str1;
    There are 2 ways:

    You can create new string
    Code:
    char str3[100] = {0};
    if (len > 0)
    {
        strncpy(str3, str1, len);
    }
    Or you can modify existing str1 by inserting terminating character.
    Code:
    if (len > 0)
    {
        str1[len - 1] = 0;
    }

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by oror84 View Post
    hello,
    i have a problem to split a string.
    i have this sentence :
    char str1[100] = "hello this is my sentence and max(a,b) is my method";

    i want to to split the sentence from the max(a,b) to 2 strings.

    with :
    char *str2;
    str2 = strstr(str1,"max");
    now i took the right side of the sentence to str2.
    bow how can i take the left side to other string?
    maby how can i get the place of max by numbers(lenght)?
    i didnt find any str command to do this.
    i`ll happy if someone help me.
    Thanks ,
    Or.
    Ok... you need to do three things here...

    Code:
    #include <stdlib.h>
    #include <string.h>
    
    int main (void)
      {   // testing example
          char test[] = "hello this is my sentence and max(a,b) is my method";
          char *Left;
          char *Right;
             
           // first make a copy
           Left = strdup(test);
    
           // second locate the desired text
           Right = strstr(Left,"max");
    
           // third split the string
           *(Right - 1) = '\0';
    
           // print the results
           printf("Original : %s\nLeft side: %s\nRight side: %s\n\n",test,Left,Right);
    
           // clean up the mess
           free(Left);
         
           // and exit
           return 0; }
    Last edited by CommonTater; 02-01-2011 at 07:55 AM.

  4. #4
    Registered User
    Join Date
    Jan 2011
    Posts
    14

    Thank You

    Great, You helped me alot!
    Thank you very much!!

  5. #5
    Registered User
    Join Date
    Apr 2011
    Posts
    1
    Quote Originally Posted by DRK View Post
    Or you can modify existing str1 by inserting terminating character.
    Code:
    if (len > 0)
    {
        str1[len - 1] = 0;
    }
    Thank you,

    I have had so many errors trying to change filename strings. Nothing seemed to work until this and it's very simple like it should be too.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. split pdf files with ghostscript API
    By zoidberg in forum C++ Programming
    Replies: 1
    Last Post: 07-08-2010, 04:52 PM
  2. Split String
    By puneetlakhina in forum C Programming
    Replies: 1
    Last Post: 10-06-2008, 02:12 AM
  3. split file
    By andriss in forum C Programming
    Replies: 4
    Last Post: 10-18-2006, 03:47 PM
  4. Split line
    By groorj in forum C Programming
    Replies: 8
    Last Post: 04-24-2005, 12:05 PM
  5. Split int variable
    By Jas11 in forum C++ Programming
    Replies: 4
    Last Post: 03-28-2005, 05:06 PM