Thread: splitting strings

  1. #1
    Unreg1
    Guest

    Question splitting strings

    hi,

    is there any way to split a string into two in C or C++... like there is a 'SPLIT' in BASIC to split the strings to two.

  2. #2
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    Code:
    #include <iostream>
    #include <string>
    
    
    
    int main(){
    	
    	std::string fullStr = "Hello World",
    				startStr,
    				endStr;
    	
    	startStr = fullStr.substr(0,5);
    	endStr = fullStr.substr(6,5);
    	
    	std::cout << startStr << " " << endStr;
    }

  3. #3
    Registered User
    Join Date
    Sep 2002
    Posts
    72
    look up strtok()

  4. #4
    Registered User
    Join Date
    Jun 2002
    Posts
    79
    Suppose you want to split the string after character no. 5. The two substrings are being stored in string2 and string3, while string1 is the main string.
    Code:
    #include<stdio.h>
    #include<string.h>
    int main()
    {
       char string1[MAX],string2[MAX],string3[MAX];
       int ctr1,ctr2,ctr3;
       gets(string1);
       for(ctr=0; ctr<5; ++ctr)
          string2[ctr]=string1[ctr];
       ctr3=0;
       for(ctr2=ctr; ctr2<strlen(string1); ++ctr2)
       {
          string3[ctr3]=string1[ctr2]; ++ctr3;
       }
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. splitting strings...
    By newbie_socketsp in forum C Programming
    Replies: 4
    Last Post: 08-15-2008, 10:17 AM
  2. splitting up names (strings)
    By Butters007 in forum C Programming
    Replies: 8
    Last Post: 12-07-2006, 01:13 AM
  3. Strings Program
    By limergal in forum C++ Programming
    Replies: 4
    Last Post: 12-02-2006, 03:24 PM
  4. Programming using strings
    By jlu0418 in forum C++ Programming
    Replies: 5
    Last Post: 11-26-2006, 08:07 PM
  5. Splitting strings into words.
    By 0x7f in forum C++ Programming
    Replies: 6
    Last Post: 03-31-2003, 03:49 PM