M3-R4: July 2013 (O-Level)
Q. 5(a):
Write a program to calculate number of vowels (a,e,i,o,u) separately in an entered string.
Ans:
Calculating number of vowels separately in a string:
#include <stdio.h>
#include <conio.h>
#include <string.h>
void main(){
char str[80];
int i, len, count[5];
printf("\nEnter a string: ");
gets(str);
len = strlen(str);
for(i=0; i<len; i++){
switch(str[i]){
case 'a':
case 'A':
count[0]++;
break;
case 'e':
case 'E':
count[1]++;
break;
case 'i':
case 'I':
count[2]++;
break;
case 'o':
case 'O':
count[3]++;
break;
case 'u':
case 'U':
count[4]++;
break;
}
}
printf("\nNumber of 'a': %d", count[0]);
printf("\nNumber of 'e': %d", count[1]);
printf("\nNumber of 'i': %d", count[2]);
printf("\nNumber of 'o': %d", count[3]);
printf("\nNumber of 'u': %d", count[4]);
getch();
}
Q. 5(a):
Write a program to calculate number of vowels (a,e,i,o,u) separately in an entered string.
Ans:
Calculating number of vowels separately in a string:
#include <stdio.h>
#include <conio.h>
#include <string.h>
void main(){
char str[80];
int i, len, count[5];
printf("\nEnter a string: ");
gets(str);
len = strlen(str);
for(i=0; i<len; i++){
switch(str[i]){
case 'a':
case 'A':
count[0]++;
break;
case 'e':
case 'E':
count[1]++;
break;
case 'i':
case 'I':
count[2]++;
break;
case 'o':
case 'O':
count[3]++;
break;
case 'u':
case 'U':
count[4]++;
break;
}
}
printf("\nNumber of 'a': %d", count[0]);
printf("\nNumber of 'e': %d", count[1]);
printf("\nNumber of 'i': %d", count[2]);
printf("\nNumber of 'o': %d", count[3]);
printf("\nNumber of 'u': %d", count[4]);
getch();
}
No comments:
Post a Comment