Sep 23, 2013 at 4:35pm UTC
This is not the way to get the string from the user.
cin>>si[size];
In fact it is accessing an element just outside the array which is not valid.
Instead try:
The for loop should use the actual length of the string entered by the user:
for (int i=0; i<strlen(si); i++)
Last edited on Sep 23, 2013 at 4:35pm UTC
Sep 23, 2013 at 4:38pm UTC
i am not using string..its just char array.
Sep 23, 2013 at 4:58pm UTC
but this is giving eror
cin.getline (si, size);
Sep 23, 2013 at 4:59pm UTC
plz check the code i thnk its problm with count
Sep 23, 2013 at 5:24pm UTC
In order to use
strlen()
you need to include the <cstring> header
Your original code had an error at line 18,
else if (si[2]=='o' || si[i]=='O' )
-
si[2]
should be
si[i]
I made minimal changes to your code. But I made a new loop to output the results:
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 34 35
#include <iostream>
#include <cstring> // for strlen
#include <conio.h>
using namespace std;
int main()
{
const int size=50;
char si[size];
int count[5]={0};
cout << "please enter the string" << endl;
cin.getline (si, size);
for (int i=0; i<strlen(si); i++)
{
if (si[i]=='a' ||si[i]=='A' )
count[0]++;
else if (si[i]=='i' || si[i]=='I' )
count[1]++;
else if (si[i]=='o' || si[i]=='O' )
count[3]++;
else if (si[i]=='u' || si[i]=='U' )
count[4]++;
}
for (int i=0; i<5; i++)
{
cout << count[i] << endl;
}
getch();
}
Output:
please enter the string
The cat sat on the mat
3
0
0
1
0
Notice that the letter 'e' which occurs twice is not counted. That was an error in the original code, I leave that for you to fix.
Last edited on Sep 23, 2013 at 5:29pm UTC