Computer Science Canada

how to seperate a string into characters?

Author:  JR [ Mon Feb 05, 2007 8:45 am ]
Post subject:  how to seperate a string into characters?

Hi,
I have a file which contains some characters data, for example the first line is:

5 3

my question if i read the line as a string, how would i separate both characters? there are spaces in between.
like how would i put those 2 numbers into different variables.

Author:  wtd [ Mon Feb 05, 2007 9:24 am ]
Post subject:  RE:how to seperate a string into characters?

Try using the String class' split method.

Author:  JR [ Mon Feb 05, 2007 2:15 pm ]
Post subject:  Re: how to seperate a string into characters?

what is the name of the function that does that?

Author:  wtd [ Mon Feb 05, 2007 2:18 pm ]
Post subject:  RE:how to seperate a string into characters?

Err... for some reason I was thinking this was in the Java Help forum.

As for C++, use the std::stringstream class.

Author:  abcdefghijklmnopqrstuvwxy [ Mon Feb 05, 2007 2:54 pm ]
Post subject:  RE:how to seperate a string into characters?

If everything is seperated by spaces you could use the extraction operator.

code:

int x, y;
ifstream in("file.txt");

while (in >> x)
{
   in >> y;

   //do something with x
   //do something with y
}


What that does is put the first number into x and the second number into y. Asserting that first and second num are seperated by a space.

Author:  JR [ Mon Feb 05, 2007 3:03 pm ]
Post subject:  Re: RE:how to seperate a string into characters?

abcdefghijklmnopqrstuvwxy @ Mon Feb 05, 2007 2:54 pm wrote:
If everything is seperated by spaces you could use the extraction operator.

code:

int x, y;
ifstream in("file.txt");

while (in >> x)
{
   in >> y;

   //do something with x
   //do something with y
}


What that does is put the first number into x and the second number into y. Asserting that first and second num are seperated by a space.



some of the data in the file has no spaces, so that would not work properly i guess.

Author:  deville75 [ Tue Feb 06, 2007 11:10 am ]
Post subject:  RE:how to seperate a string into characters?

Why not use a char array? an array of chars already stores each character so you don't need a method. Also you can check each value char[0], char[1], ... for it's value to see if it's a space or not. So instead of reading it into a string read it into a char [].

Author:  wtd [ Tue Feb 06, 2007 1:14 pm ]
Post subject:  RE:how to seperate a string into characters?

Because he's dealing with C++, and has a real string type to work with, as a result.

C++'s std::string class can be iterated over in the same manner.

Author:  abcdefghijklmnopqrstuvwxy [ Tue Feb 06, 2007 3:04 pm ]
Post subject:  RE:how to seperate a string into characters?

I was going to mention how senseless it would be to use a char* but I figured since the advantage of std::string is covered so exhaustively in this forum and elsewhere on the net I wouldn't waste my finger muscles.

Author:  ownageprince [ Fri Feb 09, 2007 7:25 pm ]
Post subject:  Re: how to seperate a string into characters?

string word="program";
char ch;
for(int i=0;i<word.length();i++)
{
ch=word[i];
cout<<ch<<endl;
}

//this stores each character from word into ch...but after the for loop you will only have "m"

Author:  wtd [ Fri Feb 09, 2007 7:57 pm ]
Post subject:  RE:how to seperate a string into characters?

code:
#include <iostream>
#include <string>
#include <iterator>

int main()
{
   std::string foo = "foo bar";
   
   std::copy(foo.begin(), foo.end(),
             std::ostream_iterator<char>(std::cout, ""));
   std::cout << std::endl;
}


: