Create a stringstream from the string, read in each string (it automagically splits by spaces), remove any non-alpha characters (e.g. punctuation), and then check the last two letters.
If you just need to read from std:cin, then you can skip the stringstream, but the logic is the same. Keep reading in word after word, remove any characters which are not letters, and then check the last two characters.
Start with a program like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
#include <iostream>
#include <string>
int main()
{
std::string word;
std::size_t count = 0;
while(std::cin >> word)
{
//for loop: remove characters which are not letters
//if statement: check the last two characters -> ++count;
}
std::cout << count << std::endl;
}
I've already written a lot for you, you may want to rewrite it yourself so you understand how it works.
so from my understnding i would esentially be writting a code that reads in a string then removes any characters that are not letters withtin the string then check if the string ends in ng?
example:
sting= i love driving and fishing
that would become ilovedrivingandfishing
then when i check how many words in the string end in ng it would output 1.
or maybe im just misunderstanding the whole thing haha. im just trying to understand this in time for my test.
No, first you would get i, then you would get love, then you would get driving, then you would get and, then you would get fishing. The code I have written for you gets one word at a time.
I'm assuming you meant "for loop" and not "if statement".
There is no replace function for strings, which is fine because you won't need one. All you need is the substring before the character to remove and the substring after the character to remove. You could also do it with iterators, but considering what you've said you've not learned I am willing to gues you haven't learned about iterators either.
yea i meant for loop. i have tried a bunch of stuff couldnt get it to work. cant spend to much time on this got to study other questions. i wouldnt mind if you showed me how but if not thanks for all your help. helped me get a better idea of what i was doing :)