Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 how to seperate a string into characters?
Index -> Programming, C++ -> C++ Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
JR




PostPosted: 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.
Sponsor
Sponsor
Sponsor
sponsor
wtd




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

Try using the String class' split method.
JR




PostPosted: 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?
wtd




PostPosted: 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.
abcdefghijklmnopqrstuvwxy




PostPosted: 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.
JR




PostPosted: 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.
deville75




PostPosted: 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 [].
wtd




PostPosted: 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.
Sponsor
Sponsor
Sponsor
sponsor
abcdefghijklmnopqrstuvwxy




PostPosted: 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.
ownageprince




PostPosted: 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"
wtd




PostPosted: 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;
}
Display posts from previous:   
   Index -> Programming, C++ -> C++ Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 11 Posts ]
Jump to:   


Style:  
Search: