Find a letter in a given word

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?

thank you all and here is where I am at.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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;
                        
                    }
                    else if((str.at(i)=='x') && (str.at(i)!='e'))
                    {
                        cout<<"Your word, "<<word<<", contains the character 'x'"<<endl;
                    }
                    else if ((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.
Last edited on
What loop function would be best to use?
for loop
Also how would I use size and at functions here?
loop from 0 to size(). Use .at() istead of [] to access individual values.
This is where I am at :(


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
  #include <iostream>
#include <string>

using namespace 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;
    }
    

thank you for your help I am working on the above code and will post it when I finish,

Thank you again!
Last edited on
ok this works if 'e' comes before 'x' in the word entered but if 'x' comes first it only finds 'x', how can I fix this?




1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
        cout<<"enter a word"<<endl;
            cin>>word;
            std::string str (word);
                for (unsigned i=0; i<str.length(); ++i)
               
                    if(str.at(i)=='e')
                    {
                        cout<<"Your word, "<<word<<", contains the character 'e'"<<endl;
                        
                    }
                    else if(str.at(i)=='x')
                    {
                        cout<<"we found x"<<endl;
                        break;
                    }
                    else
                    {
                        
                    }
If you find x, you break the loop: line 14.
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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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;
                        
                    }
                    else if((str.at(i)=='x') && (str.at(i)!='e'))
                    {
                        cout<<"Your word, "<<word<<", contains the character 'x'"<<endl;
                    }
                    else if ((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
                    {
                        
                    }
1) you do not need two conditions here as onecharacter cannot be both e and x.
2) create a boolean varialbles called efound and xfound.
Use them:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
//Variant 1
bool efound = false;
bool xfound = false;
for( int i = 0; i < str.size(); ++i) {
    if(str.at(i)=='e')
        efound = true;
    else if ((str.at(i)=='x')
        xfound = true;
}
if(efound)
    std::cout << cout<<"Your word, "<<word<<", contains the character 'e'"<<endl;
if(xfound)
    std::cout << cout<<"Your word, "<<word<<", contains the character 'x"<<endl;

//Variant 2
bool efound = false;
bool xfound = false;
for( int i = 0; i < str.size(); ++i) {
    if(str.at(i)=='e' && !efound) {
        efound = true;
        std::cout << cout<<"Your word, "<<word<<", contains the character 'e'"<<endl;
    } else if ((str.at(i)=='x' && !xfound) {
        xfound = true;
        std::cout << cout<<"Your word, "<<word<<", contains the character 'x"<<endl;
    } else if (xfound && efound) //if found both, no need to loop further
        break;
}
}
thank you so much miinipaa, I am still trying to take all what you typed in.
if I am reading this correctly variant 1 and 2 are two different ways to do it.
variant 1 seems much more simple
Topic archived. No new replies allowed.