Question:
How to break up a string into multiple strings in C++?
x_abbie_2006_x
2007-09-15 05:37:31 UTC
I need to know to break up a string (or character array) into multiple strings in C++ based on spaces (it must be able to handle multiple spaces).

So, for example, if I have the string "aardvark cat dog person",
I need to get four different strings of "aardvark", "cat", "dog", "person".

How do I do this?
Three answers:
2007-09-15 07:42:50 UTC
The C std library (C++ is a superset of C) includes the strtok method which allows you to specify a token (in your case space) which will then split the input string into an array of strings broken at the token you specify.



Google string token function in C and look at examples. It's a little confusing to understand at first but if you copy an example, should be easy to implement in a few lines of code. The previous answer is about 20 lines more than you really need.
2016-05-20 05:06:10 UTC
I'm going to base my suggestion on Lecky T's. I'm on Linux, have been for years, but it seems to me using c libraries (in particular cstdio and cstlib) you may be able to do so easily. The function exec(char *) allows you to run an external program. If you have a command line with options you have to construct on the fly, you create a string with sprintf (String PRINT Format) which works like printf. If you want to use iostream functions use sstream for it ( see sources). Now here Linux and Unix are straightforward. So I might do a "exec("ls *.txt > workfile");, and what I'd get is a text file with every file ending in txt listed on its own separate line. I'm not sure there is an exact MS-DOS equivalent, but open a command shell and type "dir /b" and see what you get. In MS-DOS, the last time I was on it (XP), you got two versions of the file name. The second was the name as it appears in Windows. The first was a twelve character string. If the name before the extension was longer than eight letters or the extension longer than 3, you would get the first six letters of the name, the characters "~1" and of course the period which is always the 9th character, then the first 3 letters of the extension. And everything would be capitalized. With error checking you can do it: Open workfile with either an fstream or fopen(), read a string, (either infile >> string; or fscanf(Infile, "%s", string);) test to see if the substring string[9] through [11]=="TXT" and take action if it is. If not test for the end of file and if not, read the next string and repeat. While Windows doesn't have the full piping capabilities of UNIX and Linux, ">" works. You should have no trouble getting a text file which lists the directory. Then just open it as a file, and sequentially read the strings, and use them to open each of the text files listed and perform your actions, until you reach the end of file on your directory file. Again, you read them using fgets or the >> operator on a predeclared array of chars (an ASCIIZ string). On the compilers I use, that is what format both fopen and ifile.open() take for a file name.
2007-09-15 05:50:05 UTC
use a 2d array then !!!!!!

so simple



here u go





while(i
{

if((ar[i]==' ')&&(i
{

while((ar[i]==' ')&&(i
{

i++; //skipping spaces

}

}



else if((ar[i]!=' ')&&(i
{

while((ar[i]!=' ')&&(i
{

a[r][j]=ar[i];

i++;

j++;//column of a[],the 2d array in which we are storing the

//words

}



r++;

a[r][j]= '\0' \\for null character,otherwise it will show u junk

\\values while printing

}





and then u print the 2d array,enjoy!

see the logic is the words get separated in different rows

so u can use them now for watever problem.......like sorting them alphabetically,printing them in orders of length,etc



any other problems just add me to ur answers profile


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Continue reading on narkive:
Loading...