Program to count "the" word?
| princemoon (19) |
|
I made this program to count vowels in given words now what should I change in it to count word only 'the' in this program?
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
|
#include <iostream.h>
#include<conio.h>
int main()
{
char ar[20];
int vowc=0,i;
cout<<("Enter the array\n");
gets(ar);
for(i=0;i<20;i++)
{
switch(ar[i])
{
case 'a' :
case 'A' :
case 'e' :
case 'E' :
case 'i' :
case 'I' :
case 'o' :
case 'O' :
case 'u' :
case 'U' :
vowc++;
break;
default : break;
}
}
cout<<("%d",vowc);
return 0;
}
|
|
|
|
| vlad from moscow (3662) |
|
Use C function strstr or you can change the program the following way
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 36 37 38
|
#include <iostream>
#include <cstdio>
using namespace std;
int main()
{
char ar[20];
unsigned int found = 0;
unsigned int count = 0;
cout << "Enter the array\n";
gets( ar );
for ( char *p = ar; *p; ++p )
{
switch ( *p )
{
case 't' : case 'T' :
found = 1;
break;
case 'h' : case 'H' :
if ( found ) ++found;
break;
case 'e' : case 'E':
if ( found == 2 ) count++;
found = 0;
break;
default:
found = 0;
break;
}
}
cout << count;
return 0;
}
|
EDIT: Also I think you do not understand what happens in the statement of your original program
cout<<("%d",vowc);
Though this statement works however it has no any sense that is there is no any sense to use the comma operator. You should write simply
cout << vowc;
|
|
Last edited on
|
| princemoon (19) |
|
|
Thanx alot dear you did it right & I will keep in mind the comma use next time.
|
|
|
| vlad from moscow (3662) |
|
However the suggested code contains a bug! :) Shall be
1 2 3 4 5 6 7 8
|
case 'h': case 'H':
if ( found == 1 ) ++found;
else found = 0;
break;
case 'e': case 'E':
if ( found == 2 ) count++;
found = 0;
break;
|
|
|
Last edited on
|
| vlad from moscow (3662) |
|
Also instead of gets it is better to use cin.getline( ar, sizeof( ar ) );
|
|
|