473,241 Members | 1,584 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,241 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 1837
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: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

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.