c++ string occurence

Pages: 12
Im trying to write a code that counts the number of words in a string that end with ng.


I started with this but this just checks the end of the string. I need it to check everyword in the string and also count them up.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int main()
{
	
    string s;
    
    cout << " enter string ";
    getline(cin, s);
    
    string end;
	end = s.substr(s.length()-2, 2);
	cout << end;
    cout << endl;
    
    return 0;
} 



thanks in advance
Last edited on
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.
is this on the right path? it doesent return anything though.


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
28
#include <iostream>
#include <string>
using namespace std;

int main()
{
    string s;
    int countString;
    int amount = 0;
    
    cout << "enter string: ";
    cin >> countString;
    while(countString--)
    {
        cin >> s;
        
        if (s.length() >= 2)
        {
            if (s[ s.length()-1]=='b' && s[s.length()-2]=='a')
                amount ++;
        }
    }
    
    cout << amount << endl;
    
    system("pause");
    return 0;
}

or maybe something like this ?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <string>
using namespace std;


int main()
{
	
    string s;
    
    cout << " enter string ";
    getline(cin, s);
    
 
    if (s.substr(s.length()-2, 2));
    {
        count++;
    }
 

    
    return 0;
}
Look up what a stringstream is ;)
we havent learned that in my class yet so i cant use it thats why i was trying to use a differetn method.

it is suppossed to be something like this but this code isnt complete and i cant figure out how to finish it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <string>
using namespace std;


int main()
{
	
    string s;
    
    cout << " enter string ";
    getline(cin, s);
    
 
    if (s.substr(s.length()-2, 2));
    {
        count++;
    }
 

    
    return 0;
}
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.

again thanks for your help.
Last edited on
michael25 wrote:
example:
sting= i love driving and fishing

that would become ilovedrivingandfishing
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.
Last edited on
oh ok that makes sense now. Am I on the right path now?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <string>
using namespace std;

int main()
{
    std::string word;
    std::size_t count = 0;
    
    cout << "enter string: ";
    getline(cin, word)
    
    while(std::cin >> word)
    {
        //for loop: remove characters which are not letters
        
        //if statement: check the last two characters -> ++count;
        if(word.substr(word.length()-2, 2) == 'ng')
        {
            count++;
        }
    }
    std::cout << count << std::endl;
}
Close, you need to use double quotes on line 17 though. Do you know how to do the for loop?
Last edited on
alright thanks. im not sure how to write the if statement would i use word.replace()?
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 :)
If you show your best attempt at the for loop, I'll show my solution.
im not sure what to put in the if statment within the for loop if thats even correct.

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
28
29
30
#include <iostream>
#include <string>
using namespace std;

int main()
{
    std::string word;
    std::size_t count = 0;
    
    cout << "enter string: ";
    getline(cin, word);
    
    while(std::cin >> word)
    {
        //for loop: remove characters which are not letters
        int i(0);
        for(i = 0; i <= word; i++)
            if(word[i] == " ")
            {
                str::erase(i, 1);
            }
        
        //if statement: check the last two characters -> ++count;
        if(word.substr(word.length()-2, 2) == "ng")
        {
            count++;
        }
    }
    std::cout << count << std::endl;
}
1
2
3
4
5
6
7
8
9
10
11
12
#include <cctype>

//...

        std::string w;
        for(std::size_t i = 0; i < word.length(); ++i)
        {
            if(std::isalpha(word[i]))
            {
                w += word[i];
            }
        }
interesing way, when i put it all together it dosent work am i missing something

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
28
29
30
31
32
33
#include <iostream>
#include <string>
#include <cctype>
using namespace std;

int main()
{
    std::string word;
    std::size_t count = 0;
    
    cout << "enter string: ";
    getline(cin, word);
    
    while(std::cin >> word)
    {
        //for loop: remove characters which are not letters
        std::string w;
        for(std::size_t i = 0; i < word.length(); ++i)
        {
            if(std::isalpha(word[i]))
            {
                w += word[i];
            }
        }
        
        //if statement: check the last two characters -> ++count;
        if(word.substr(word.length()-2, 2) == "ng")
        {
            count++;
        }
    }
    std::cout << count << std::endl;
}
It's not working because you added in line 12 when you shouldn't have.
oh alrihgt , thank you
Pages: 12