472,371 Members | 1,351 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,371 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 3053
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: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge required to effectively administer and manage Oracle...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was proposed, which integrated multiple engines and...
1
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web server and have made sure to enable curl. I get a...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the synthesis of my design into a bitstream, not the C++...
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS starter kit that's not only easy to use but also...
2
by: Ricardo de Mila | last post by:
Dear people, good afternoon... I have a form in msAccess with lots of controls and a specific routine must be triggered if the mouse_down event happens in any control. Than I need to discover what...
1
by: Johno34 | last post by:
I have this click event on my form. It speaks to a Datasheet Subform Private Sub Command260_Click() Dim r As DAO.Recordset Set r = Form_frmABCD.Form.RecordsetClone r.MoveFirst Do If...
1
by: ezappsrUS | last post by:
Hi, I wonder if someone knows where I am going wrong below. I have a continuous form and two labels where only one would be visible depending on the checkbox being checked or not. Below is the...
0
by: jack2019x | last post by:
hello, Is there code or static lib for hook swapchain present? I wanna hook dxgi swapchain present for dx11 and dx9.

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.