473,405 Members | 2,262 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,405 software developers and data experts.

Strings and for loops

Tom
Please help I'm busy with my assigment and I'm stuck on the following:
I must enter a sentence and then replace words in the sentence, this
works fine but I must repeat it in a for loop. the first time the loop
starts it asks for the sentence and then the word it must look for and
then replace it with antoher. The problem is on the second time of the
loop it does not pause at the first question, it shows it but continue
to the last.
please see code below:

int n;
string Sentence, Sub1, Sub2;
int Position;
// We'll do this for 3 strings
for (n = 0; (n < 5); ++n)
{
// Input a sentence and two words
cout << "Enter a sentence: ";
cin >Sentence;
cout << endl;
cout << "Enter a word to search for: ";
cin >Sub1;
cout << "Enter a word to replace it with: ";
cin >Sub2;
// Search for the Sub1 and replace all occurrences
// of it with the Sub2
Position = Sentence.find(Sub1);
while (Position != -1) {
Sentence.erase(Position, Sub1.size());
Sentence.insert(Position, Sub2);
Position = Sentence.find(Sub1);
}

Jul 5 '06 #1
2 1840
TB
Tom skrev:
Please help I'm busy with my assigment and I'm stuck on the following:
I must enter a sentence and then replace words in the sentence, this
works fine but I must repeat it in a for loop. the first time the loop
starts it asks for the sentence and then the word it must look for and
then replace it with antoher. The problem is on the second time of the
loop it does not pause at the first question, it shows it but continue
to the last.
please see code below:

int n;
string Sentence, Sub1, Sub2;
int Position;
// We'll do this for 3 strings
for (n = 0; (n < 5); ++n)
{
// Input a sentence and two words
cout << "Enter a sentence: ";
cin >Sentence;
cout << endl;
cout << "Enter a word to search for: ";
cin >Sub1;
cout << "Enter a word to replace it with: ";
cin >Sub2;
Say that you enter "foxy Brown jumped big doggystyle", then
on the first iteration you get:

Sentence = "foxy"
Sub1 = "brown";
Sub2 = "jumped";

On the second iteration, there is still input data available
so it continues without waiting for more:

Sentence = "big"
Sub1 = "doggystyle"

Look up std::getline() in <string>.
>
// Search for the Sub1 and replace all occurrences
// of it with the Sub2
Position = Sentence.find(Sub1);
while (Position != -1) {
while(Position != std::string::npos) {
>

Sentence.erase(Position, Sub1.size());
Sentence.insert(Position, Sub2);
Position = Sentence.find(Sub1);
}
--
TB @ SWEDEN
Jul 5 '06 #2
Tom wrote:
Please help I'm busy with my assigment and I'm stuck on the following:
I must enter a sentence and then replace words in the sentence, this
works fine
I question this assertion. See below.
but I must repeat it in a for loop. the first time the loop
starts it asks for the sentence and then the word it must look for and
then replace it with antoher. The problem is on the second time of the
loop it does not pause at the first question, it shows it but continue
to the last.
please see code below:

int n;
string Sentence, Sub1, Sub2;
int Position;
Suggestion: Don't declare variables until you need them and can
initialize them, and declare them in as small a scope as possible. This
will prevent some errors and make the code easier to follow. For
instance, the for-loop below could be better written as:

for( int n = 0; n < 5; ++n )

so the n would only exist in the loop itself. You should also put your
string declarations inside the for-loop braces (unless you need them
outside the loop).
// We'll do this for 3 strings
for (n = 0; (n < 5); ++n)
Is it three or five?
{
// Input a sentence and two words
cout << "Enter a sentence: ";
cin >Sentence;
cout << endl;
cout << "Enter a word to search for: ";
cin >Sub1;
cout << "Enter a word to replace it with: ";
cin >Sub2;
As it stands, if you entered "This is fun", your variables would be as
follows:

Sentence = "This"
Sub1 = "is"
Sub2 = "fun"

which is not what you want. What you do want is something like:

getline( Sentence, cin );

(The other lines can stay the same.) Fix that, get the first iteration
working, and then ask a specific question again. But take note of this
FAQ:

http://parashift.com/c++-faq-lite/ho...t.html#faq-5.2

You might also find these helpful:

http://www.parashift.com/c++-faq-lite/input-output.html

Cheers! --M

Jul 5 '06 #3

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

Similar topics

20
by: Ravi | last post by:
Hi, I have about 200GB of data that I need to go through and extract the common first part of a line. Something like this. >>>a = "abcdefghijklmnopqrstuvwxyz" >>>b = "abcdefghijklmnopBHLHT"...
17
by: Noen | last post by:
def XOR(s1,s2): """ XOR string s1 with s2 """ output = "" # Argument check if (type(s1) and type(s2)) != type(""): raise TypeError, "Arguments are not strings" if len(s1) != len(s2): raise...
9
by: Nico Grubert | last post by:
Hi there, Background of this question is: I want to convert all words <word> except 'and' / 'or' / 'and not' from a string into '*<word>*'. Example: I have the following string: "test and...
3
by: Edg Bamyasi | last post by:
This Is A Late Cross Post from comp.lang.python. It seems the mistery is deeper then i expected. What is the running time of conactination on character strings. i.e. >> joe="123" >>...
5
by: Colin Savage | last post by:
Please could somebody explain what an "atomized string" is? I have been doing loops in the msdn around XmlNameTable and NameTable but neither explain what an atomized string is. The example for one...
8
by: vonclausowitz | last post by:
Hi Everyone, This is a very complicated, if not impossible problem. I have two strings I want to compare. The problem is that somewhere in both strings there is a text which is identical, but...
3
by: Daniel Weinand | last post by:
hello ng, i have a problem and a imho an insufficient method of solution. strings should be sorted by specific text pattern and dispayed in groups. the strings are stored in a db and have the...
52
by: Paddy | last post by:
I was browsing the Voidspace blog item on "Flattening Lists", and followed up on the use of sum to do the flattening. A solution was: I would not have thought of using sum in this way. When...
95
by: hstagni | last post by:
Where can I find a library to created text-based windows applications? Im looking for a library that can make windows and buttons inside console.. Many old apps were make like this, i guess ...
4
by: dhinakar_ve | last post by:
Hi All, I am writing a function to generate the strings based on a pattern. For example A will generate A1, A2 and A3. If the pattern is A then it will generate the strings A11, A12, A21,...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.