473,386 Members | 1,743 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

How do I take apart a string and get individual characters/words?

I used to program text RPG's in C back in high school, but I was very crude
at it and only learned what I needed to get the job done. I am now about to
graduate college with a degree in English and have had an interest in the
structure of language. In order to begin with my little program, I need to
be able to take a string like "The dog jumped." and be able to take the
first word from the sentence and assign it to a string, such as word1, then
skip the blank space and assign dog to the string word2 and then skip once
again and assign jumped to the string word3. I would give you some code I
was working with to try to do this, but I would rather save your stomachs
from hurting too much.... the laughter would be too much to bear =) But I
have attempted it on my own, and just need a little jump in the right
direction, if not just an all out solution. I appreciate any help you can
provide.
Jul 22 '05 #1
5 3111
For that, I would look up how to use stringstream.

Jul 22 '05 #2
So you want to tokenize the string.
One quick approach, however, is to use sstream:

#include <sstream>
#include <string>
#include <iostream>

int main()
{
std::istringstream s;
std::string w1, w2;

s.str("A b c");
s >> w1 >> w2;
std::cout << "word1 = "<< w1 << "\nword2 = " << w2 << "\n";
}
To really tokenize a C++ string, you'll need to write your own
function, however.

to get at the individual characters, you can use the subscript
operator, i.e.

string s = "_lah";
s[0] = 'b';
changes s to "blah"

So another possibility is to read the string and build a list of
words...

string stringVar = "blah blah blah";
vector <string> words;

for(pos = last = 0; pos < stringVar.length(); pos++) {
if (stringVar[pos] == ' ') {
words.pushBack(stringVar.substr(last, pos-lst));
last = pos+1;
}
}

Jul 22 '05 #3
derrick wrote:
I used to program text RPG's in C back in high school, but I was very crude
at it and only learned what I needed to get the job done. I am now about to
graduate college with a degree in English and have had an interest in the
structure of language. In order to begin with my little program, I need to
be able to take a string like "The dog jumped." and be able to take the
first word from the sentence and assign it to a string, such as word1, then
skip the blank space and assign dog to the string word2 and then skip once
again and assign jumped to the string word3. I would give you some code I
was working with to try to do this, but I would rather save your stomachs
from hurting too much.... the laughter would be too much to bear =) But I
have attempted it on my own, and just need a little jump in the right
direction, if not just an all out solution. I appreciate any help you can
provide.


I'd be tempted to say "buy a book". If you talk about such variables
(you seem not to know about arrays) and if you don't even know how to
parse a string, then I suggest you try to learn the language with simple
programs first.

Here's something to get you started with your problem.

# include <string>
# include <iostream>

int main()
{
std::string s = "The dog jumped.";

std::string::size_type start=0, end=0;

start = s.find_first_not_of(" ");

if ( start == std::string::npos )
{
std::cout << "not found";
return 0;
}

end = s.find_first_of(" ", start);

if ( end == std::string::npos )
{
std::cout << "not found";
return 0;
}

std::cout << "first word: " << s.substr(start, end - start);
}

Look up std::string's member functions and std::vector for a collection
(such as for putting the individual words).
Jonathan
Jul 22 '05 #4

"derrick" <ra**********@yahoo.com> wrote in message
news:Q5***************@newssvr30.news.prodigy.com. ..
I used to program text RPG's in C back in high school, but I was very crude at it and only learned what I needed to get the job done. I am now about to graduate college with a degree in English and have had an interest in the
structure of language. In order to begin with my little program, I need to
be able to take a string like "The dog jumped." and be able to take the
first word from the sentence and assign it to a string, such as word1, then skip the blank space and assign dog to the string word2 and then skip once
again and assign jumped to the string word3. I would give you some code I
was working with to try to do this, but I would rather save your stomachs
from hurting too much.... the laughter would be too much to bear =) But I
have attempted it on my own, and just need a little jump in the right
direction, if not just an all out solution. I appreciate any help you can
provide.


#include <algorithm>
#include <iostream>
#include <iterator>
#include <sstream>
#include <string>
#include <vector>

int main()
{
std::string s("The dog jumped over the cat");
std::vector<std::string> words;

std::istringstream iss(s);
std::copy(std::istream_iterator<std::string>(iss),
std::istream_iterator<std::string>(),
std::back_inserter(words));

std::cout << "string: " << s << "\n\n"
<< "words:\n";

std::copy(words.begin(), words.end(),
std::ostream_iterator<std::string>(std::cout, ", "));

std::cout << "\n\n";

std::string word(words[2]);
std::cout << "Third word: " << word << "\n\n";

std::vector<char> w(words[2].begin(), words[2].end());

std::copy(std::istream_iterator<char>(iss),
std::istream_iterator<char>(),
std::back_inserter(w));

std::cout << "Characters of \"" << word << "\"\n";

std::copy(w.begin(), w.end(),
std::ostream_iterator<char>(std::cout, ", "));

std::cout.put('\n');
return 0;
}

-Mike
Jul 22 '05 #5
You have all been very helpful, except perhaps the first responder... I
appreciate all the help you have provided =) and my program is well on its
way since I last spoke to you all.
Jul 22 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Erinys | last post by:
Hi, I need to access the individual bytes in a string in my javascript function. If the characters in the string were all ascii characters then there would not be a problem, however in my case...
12
by: Hp | last post by:
Hi All, Thanks a lot for all your replies. My requirement is as follows: I need to read a text file, eliminate certain special characters(like ! , - = + ), and then convert it to lower case and...
4
by: J | last post by:
anybody know a vb.net command to populate an array with each word in a string? for example " hi how are you" array(0) = hi array(1) = how array(2) = are array(3) = you i know i could...
7
by: JustSomeGuy | last post by:
I need to make a class called uid. A UID is a unique identifier. It looks like... 1.2.3.345.1.2.4.566 This uid get transmitted over a network as 8 bit binary data. If the length of the UID is...
24
by: garyusenet | last post by:
I'm working on a data file and can't find any common delimmiters in the file to indicate the end of one row of data and the start of the next. Rows are not on individual lines but run accross...
7
by: Leon | last post by:
Hi, I'm creating a python script that can take a string and print it to the screen as a simple multi-columned block of mono-spaced, unhyphenated text based on a specified character width and...
1
by: kellysgirl | last post by:
Now what you are going to see posted here is both the set of instructions I was given..and the code I have written. The instructions I was given are as follows In this case, you will create...
11
by: Jacek Dziedzic | last post by:
Hi! I need a routine like: std::string nth_word(const std::string &s, unsigned int n) { // return n-th word from the string, n is 0-based // if 's' contains too few words, return "" //...
6
by: shapper | last post by:
Hello, I have a string and I need to get as many words possible counting from the beginning but without exceeding 120 characters. I can't break words and I the string shouldn't end with a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.