Finding words that begin with specific letter

Hello

The program starts out with the user inputing a few words like

food
table
fast
computer
life
fear

then it's supposed to find the words that start with a specific letter, lets say f

so it will be like this

food
fast
fear

Im having a hard time figuring out a way to make it search for a words that start with the specific letter.
If you looked for a specific letter anywhere in the word you would use std::string find_first_of.

Now just for the first letter you can access the first element of a string like if ( word[0] == "f" ). Put all inputs into a for loop and check the first element for all of them.
Now just for the first letter you can access the first element of a string like if ( word[0] == "f" ). Put all inputs into a for loop and check the first element for all of them.


If word is of type std::string then word[0] cannot be meaningfully compared to a string literal. You should use a character literal instead. if ( word[0] == 'f' )
Last edited on
ok, thanks!
The reason this works is because a std::string is really just an array of chars ex:
1
2
3
4
5
    const std::string str( "Giblit" );
    const char str2[ 7 ] = "Giblit"; //equal to str
    const char str3[ 7 ] = { "Giblit" }; //equal to str and str2
    const char str4[ 6 ] = { 'G' , 'i' , 'b' , 'l' , 'i' , 't' }; //not equal to any because it has no null character
    const char str5[ 7 ] = { 'G' , 'i' , 'b' , 'l' , 'i' , 't' , '\0' }; //equal to str , str2 , str 3 
Last edited on
Topic archived. No new replies allowed.