Finding a character value in a string

The goal of my project is to define a character array with a word, for example "Christmas". I want the output to create a vector of strings where the first one is Christmas, the next one Christma, then Chri, so basically removing the letters until a vowel is read. My program works for just removing one letter at a time but I am having trouble doing the if statement for finding the vowels in the string. Any help would be fantastic, thanks!

#include <iostream>
#include <vector>

using namespace std;

int main(void)
{
string str="Antidisestablishmentarianism";
vector<string>name;

for(int i=str.size(); i>0; i--)
{
name.push_back(str.substr(0,i));
}

for(int i=0; i<str.size(); i++)
{
if(name[i] == "a" || name[i] == "e" || name[i] == "i" || name[i] == "o" || name[i] == "u")
{
cout << name[i] << endl;
}
}

return 1;
}
1
2
3
4
5
6
7
8
for(int i=0; i<str.size(); i++)
{
    char temp = name[i].at(name[i].size() - 1);  // Gets last character in string
    if(temp == 'a' || temp == 'e' || temp == 'i' || temp == 'o' || temp == 'u')
    {
        cout << name[i] << endl;
    }
}


You're comparing each string element in your vector to just "a" since your substrings are never that small, the if statement is never going to evaluate to true.

You need to get the last character in the string of each element in the vector then check to see if its a vowel or not.

Also since you're comparing chars use 'a' instead of "a"
Last edited on
Thank you so much!
Topic archived. No new replies allowed.