Write a function that is given a string, replace every letter with its position in the alphabet. If anything in the text isn't a letter, ignore it and don't return it. a being 1, b being 2, etc.As an example: AlphabetPosition("The sunset sets at twelve o' clock.") Should return "20 8 5 19 21 14 19 5 20 19 5 20 19 1 20 20 23 5 12 22 5 15 3 12 15 3 11" as a string.
std::string alphabet_position(std::string message)
{
message.erase(std::remove_if(message.begin(), message.end(), isspace), message.end());
std::ostringstream os;
for (auto const & c : message)
{
char c_lower = std::tolower(c);
if (c_lower < 'a' || c_lower > 'z') continue;
int pos = c_lower - 'a' + 1 ;
os << pos;
os << ' ';
}
return os.str();
}
int main(){
std::string text;
std::cout<<"Enter a phrase :"<<std::endl;
std::cin>>text;
std::string s_out = alphabet_position(text);
std::cout<<"the alphabet positions :"<<s_out<<std::endl;
return 0;
}
I am really struggling with this challenge. The Issue here is that when I enter a phrase, the code will give me only the alphabet positions of the letters before the first space. example: Input: The sunset sets at twelve o' clock. Output: 20 8 5