c++ string/array

Im trying to write a program that would see how many of the strings entered in an array end with the the letters ab

any sugestions on where to start
work out the size of the string then use
http://www.cplusplus.com/reference/string/string/substr/

then you can compare that substring against "ab".
Last edited on
so i got it to read and output the end of the string. However i wasnt it to count how many words in the string end with ab and then output that number. I know i have to use a if statement but not sure where to start.

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


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;
}  
If what you want is to count the number of words ending in ab, use this code..
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
#include <iostream>
using namespace std;

int main()
{	
    string s;
    int countString;
    int amount = 0;
    
    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;
}  


cronos
its not working for me for some reason. it keeps returning the amount as zero.
what the input you are using?
i just enter a string. My goal is to enter a string and count how many words in that string end with ab. would i have to use a getline function to read the string?
still dont understand it. but thanks for you help.
Topic archived. No new replies allowed.