Find letter in array

May 21, 2020 at 2:58am
Hello, I am trying to find the letter that the user entered to search for it on the array. For example if the user enters the char "b" then it will search through the array of list names that have the letter containing b, also i think i know what is wrong with it, it may be just the string can't be read from that charter but IDK really I'm a little lost. Thanks in advanced




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
  #include<iostream>

using namespace std;

int main(){
    
    string array[] = {"Stacy", "Carry", "Bob", "Tim"};
    cout << "search by letter in array string" << endl;
    
    char letter;
    cin >> letter;
    
    for(char i = 'A'; i < sizeof(array); i++){
       
       if(array.find(letter)){
            cout << "the letter " << letter << "is found on " << array[i] << endl;
        }
        else{
            cout << "could not find letter in array" << endl;
        }
    }
    
    return 0;
}


May 21, 2020 at 3:18am
1
2
3
4
5
6
7
for (int i = 0; i < 4; i++)
{
    if (array[i].find(letter) != std::string::npos)
    {
        // ... letter found ...
    }
}
Last edited on May 21, 2020 at 3:20am
May 21, 2020 at 1:59pm
char "b" is not a letter. a letter is 'b'. It matters depending on what you are doing.

Topic archived. No new replies allowed.