How to write a programme which counts letter 'a'?

Hey guys, I'm rather new to programming, and I have an assignment to write a programme with
char a[20]
and the user needs to insert a sentance
the programme needs to count the letter 'a'

I've tried to solve this several times, but something always goes wrong!
Any help or suggestion is more than welcome!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
using namespace std;
int main(){
    char a[20];
    int i;
    cin>>a;
    for (i=0; i<=20; i++){
    if (a[i]=='a'){
                cout<<"Number of letter 'a' is: "<<a[i]<<endl;
                }
                
    else cout<<"The sentance doesn't contain letter 'a'"<<endl;
}
    
    
    system("PAUSE");
    return 0;
}

Last edited on
closed account (z05DSL3A)
If you post the code you have so far we will be more than happy to make suggestions on how to proceed. I have included a link to an article on posting code.


How to: Put code into your postings:
http://www.cplusplus.com/forum/articles/1624/
closed account (z05DSL3A)
How about:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
using namespace std;
int main()
{
    char a[20];
    int i;
    cin>>a;
    int aCount = 0;
    for(i=0; i<=20; i++)
    {
        if (a[i]=='a')
        {
            ++aCount;
        }        
    }
    cout << "The number of of letter 'a' is: " << aCount << endl;
    
    
    system("PAUSE");
    return 0;
}
Hey thank you so much! Now I get where did I go wrong on this one....
Topic archived. No new replies allowed.