Thread: how to split a string into two?

  1. #1
    Registered User
    Join Date
    Nov 2002
    Posts
    117

    how to split a string into two?

    assume in a txt file, there are lines like below

    123456 7890
    123 456789
    1234 567890

    which have a space/tab to seperate them.

    i would like to ask how to seperarte the line into two strings?

    i.e sting1 = "123456", string2 ="7890"

    (i think i can get each character and combine those characters in a string until i read a "space", but what function should i use to do the combine job?)

    thank you for helping.

  2. #2
    Registered User
    Join Date
    Dec 2002
    Posts
    119
    The extraction operator (>>) stops at whitespace.

    numbers.txt
    123456 7890
    123 456789
    1234 567890

    Code:
    int six_numbers[6];
    
    ifstream filein("numbers.txt");
    
    int i;
    for(i=0; i<6; ++i){
        filein >> six_numbers[i];
    }
    If you speak or are learning Spanish, check out this Spanish and English Dictionary, it is a handy online resource.
    What happens is not as important as how you react to what happens. -Thaddeus Golas

  3. #3
    Registered User
    Join Date
    Nov 2002
    Posts
    117

    how to make it stop if i dun define the times?

    in the program, you have told the program to do the job 6 times right?

    but if the txt file update everyday and the number of lines is unknown, how to make the reading stop when it reach the end of the file?

  4. #4
    Registered User
    Join Date
    Dec 2002
    Posts
    119
    Here's one way. Change the value of MAX_NUMBERS to whatever you think is reasonable.

    Code:
    const int MAX_NUMBERS = 1024;
    
    int six_numbers[MAX_NUMBERS];
    
    ifstream filein("numbers.txt");
    
    for(int i=0; i<MAX_NUMBERS && filein >> six_numbers[i]; ++i);
    -Futura
    If you speak or are learning Spanish, check out this Spanish and English Dictionary, it is a handy online resource.
    What happens is not as important as how you react to what happens. -Thaddeus Golas

  5. #5
    Registered User
    Join Date
    Jun 2002
    Posts
    79
    Suppose you want to split the string at the point where the space occurs. The two substrings are being stored in string2 and string3, while string1 is the main string.
    Code:
    #include<stdio.h>
    #include<string.h>
    #define MAX 50
    int main()
    {
       char string1[MAX],string2[MAX],string3[MAX];
       int ctr1,ctr2,ctr3;
       gets(string1);
       ctr1=0;
       while(string1[ctr1]!=' ')
       {
          string2[ctr1]=string1[ctr1]; ++ctr1;
       }
       ctr3=0;
       for(ctr2=ctr1+1; ctr2<strlen(string1); ++ctr2)
       {
          string3[ctr3]=string1[ctr2]; ++ctr3;
       }
       return(0);
    }

  6. #6
    ResurgentBarbecue UnclePunker's Avatar
    Join Date
    May 2002
    Posts
    128
    wonga
    Compiler == Visual C++ 6.0
    "Come Out Fighting."

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. char Handling, probably typical newbie stuff
    By Neolyth in forum C Programming
    Replies: 16
    Last Post: 06-21-2009, 04:05 AM
  2. split string function
    By paxmanchris in forum C Programming
    Replies: 6
    Last Post: 04-23-2007, 09:56 AM
  3. Custom String class gives problem with another prog.
    By I BLcK I in forum C++ Programming
    Replies: 1
    Last Post: 12-18-2006, 03:40 AM
  4. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM
  5. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 02:45 PM