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.
Printable View
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.
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;
}
look up strtok()
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;
}
}