
-----------------------------------
JR
Mon Feb 05, 2007 8:45 am

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.

-----------------------------------
wtd
Mon Feb 05, 2007 9:24 am

RE:how to seperate a string into characters?
-----------------------------------
Try using the String class' split method.

-----------------------------------
JR
Mon Feb 05, 2007 2:15 pm

Re: how to seperate a string into characters?
-----------------------------------
what is the name of the function that does that?

-----------------------------------
wtd
Mon Feb 05, 2007 2:18 pm

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.

-----------------------------------
abcdefghijklmnopqrstuvwxy
Mon Feb 05, 2007 2:54 pm

RE:how to seperate a string into characters?
-----------------------------------
If everything is seperated by spaces you could use the extraction operator. 


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.

-----------------------------------
JR
Mon Feb 05, 2007 3:03 pm

Re: RE:how to seperate a string into characters?
-----------------------------------
If everything is seperated by spaces you could use the extraction operator. 


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.

-----------------------------------
deville75
Tue Feb 06, 2007 11:10 am

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 [].

-----------------------------------
wtd
Tue Feb 06, 2007 1:14 pm

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.

-----------------------------------
abcdefghijklmnopqrstuvwxy
Tue Feb 06, 2007 3:04 pm

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.

-----------------------------------
ownageprince
Fri Feb 09, 2007 7:25 pm

Re: how to seperate a string into characters?
-----------------------------------
string word="program";
char ch;
for(int i=0;i