Hello everyone,
I need to Write a program that takes a single word string from the user. After acquiring this input from the user, the program will use a loop to output whether the string contains either an āxā or an āeā (or both). I need to utilize only the size() and at() member functions of the string class as I iterate through the entire string and acquire each character individually to check its value.
What loop function would be best to use?
Also how would I use size and at functions here?
cout<<"enter a word"<<endl;
cin>>word;
std::string str (word);
for (unsigned i=0; i<str.length(); ++i)
if((str.at(i)=='e') && (str.at(i)!='x'))
{
cout<<"Your word, "<<word<<", contains the character 'e'"<<endl;
}
elseif((str.at(i)=='x') && (str.at(i)!='e'))
{
cout<<"Your word, "<<word<<", contains the character 'x'"<<endl;
}
elseif ((str.at(i)=='e') && (str.at(i)=='x'))
{
cout<<"Your word, "<<word<<", contains the character 'e'"<<endl;
cout<<"Your word, "<<word<<", contains the character 'x'"<<endl;
}
else
{
}
this works except it repeats the cout if multiple x's or e's are found.
#include <iostream>
#include <string>
usingnamespace std;
int main ()
{
int ex;
int letter;
int count;
string word;
cout<<"Which excercise? ";
cin>>ex;
cout<<endl;
if(ex==1)
{
cout<<"Enter a word. "<<endl;
cin>>word;
int letter =1;
while (letter<=word.size())
{
//if (word.find(letter)!='e')
letter++;
}
cout<<"your word contains an 'e'"<<endl;
}
I solved the above problem but now if multiple 'e's or 'x's are enter it repeats my output statement that many times, how can I fix this?
I tried to use break but that breaks the next if statement as well.
cout<<"enter a word"<<endl;
cin>>word;
std::string str (word);
for (unsigned i=0; i<str.length(); ++i)
if((str.at(i)=='e') && (str.at(i)!='x'))
{
cout<<"Your word, "<<word<<", contains the character 'e'"<<endl;
}
elseif((str.at(i)=='x') && (str.at(i)!='e'))
{
cout<<"Your word, "<<word<<", contains the character 'x'"<<endl;
}
elseif ((str.at(i)=='e') && (str.at(i)=='x'))
{
cout<<"Your word, "<<word<<", contains the character 'e'"<<endl;
cout<<"Your word, "<<word<<", contains the character 'x'"<<endl;
}
else
{
}