count how many times a letter shows up

the program will ask the user to enter a sentence and it will count the amount of occurrences of the lowercase 'a' in that sentence.


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>
using namespace std;
int main ()

{
    
    int sent;
    
    cout<<"Enter a sentence. ";
    cin>>sent;
    
    
    int letter = 0, count;
    for (count=0; count<=sent; ++count)
    {
        letter++;
    }
    
    
    
    cout<<"The number of a's that occur in your sentence is "<<letter;
    
    system("pause");
    return 0;
    
}



i don't know I'm really stuck on this code. I just can't figure it out..im pretty new at this if you haven't noticed.
Last edited on
You need to have a statement that looks for the character 'a', then increments the letter variable.

Right now you have nothing that actually looks for the letter A, just increments the letter variable.

Also, a data-type of int cannot store an array of characters. To do this, you need to make this data-type either a string, a character array.

1
2
 char[256];
           string sentence;
So i tried something different and so here is my code. i just don't know what do next or if I'm going in the right direction


#include <iostream>
using namespace std;
int main ()

{

char sent='a';
int letter=0;

cout<<"Enter a sentence. ";
cin.get(sent);

while (sent!='\n')
{
cin.get(sent);

}
cout<<"The number of a's that occur in your sentence is "<<letter;

system("pause");
return 0;

}

Last edited on
Topic archived. No new replies allowed.