Completely Solved C, C++ Programs Assignment.




C program to accept a string and count the number of small case letters,capital case letters,vowels,blank spaces and special characters.

Filed Under:

Problem Statement:
Write a program to accept a string and count the
number of small case letters,capital case letters,vowels,blank spaces and special characters.

Theory:
 Lower case,or small letters are the smaller form of letters,as opposed
to upper case or capital letters,as used in European alphabets (Greek,Latin,Cyrillic and Armenian).For example, the letter “a” is lower case while the letter “A” is upper case.
In phonetics, a vowel is a sound in spoken language ,such as English ah! [a:] ,pronounced with an open vocal tract so that there is no build up of air pressure at any point above the glottis. This contrasts with consonants, such as English sh!,where there is a constriction or closure at some point along the vocal tract. The vowels of the English language consists of the alphabets a,e,i,o,u.
A blank space is used to separate two words and special characters consists of the set of characters excluding the above , example !,@,#,$,%,: and so on. This program calculates the frequency of the above mention terms in bold.

Algorithm:
Begin
Set counters vowel←0, upper←0, lower←0, blank←0, special←0
Declare st[100]
Read st //user inputted string
for i←0 to less than(length of st)-1, increment i by 1
if st[i] is greater than equal to 65 and is less than equal to 90
increment upper by 1
if st[i] is equal to ‘A’ or ‘E’ or ‘I’ or ‘O’ or ‘U’
increment vowel by 1
else if st[i] is greater than equal to97 and is less than equalto122
increment lower by 1
if st[i] is equal to ‘a’ or ‘e’ or ‘i’ or ‘o’ or ‘u’
increment vowel by 1
else if st[i] is equal to a blank space
increment blank by 1
else
increment special by 1
print The string contains :
print vowel
print upper
print lower
print blank
print special
End


Problem Listing:
//program to count number of upper and lower case alphabets, vowels, blank spaces and special characters
#include<stdio.h>
#include<string.h>
void main()
{
int i,vowel=0,upper=0,lower=0,blank=0,special=0;
char st[100];
printf(“Enter a string \n”);
fgets(st,100,stdin);
for(i=0;i<strlen(st)-1;i++)
{
if(st[i]>=65 && st[i]<=90)
{
Upper++;
if(st[i]==’A’ || st[i]==’E’ || st[i]==’I’ || st[i]==’O’ || st[i]==’U’)
vowel++;
}
else if(st[i]>==97 && st[i]<=122)
{
lower++;
if(st[i]==’a’ || st[i]==’e’ || st[i]==’i’ || st[i]==’o’ || st[i]==’u’)
vowel++;
}
else if(st[i]==’ ‘)
blank++;
else
special++;
}
printf(“The string %s contains :\n”,st);
printf(“%d vowels \n”,vowel);
printf(“%d upper case alphabets \n”,upper);
printf(“%d lower case alphabets \n”,lower);
printf(“%d blank spaces \n”,blank);
printf(“%d special characters \n”,special);
}


Output:
Enter a string
How many are still left?
The string How many are still left?
Contains:
6 vowels
1 upper case alphabets
18 lower case alphabets
4 blank spaces
1 special characters

Discussion:
• While counting vowels both upper case as well as lower case alphabets should be considered.
• Anything other than English alphabets and the blank space is considered as a special character.(number included)
• The string should be traversed till the (Length of string-1)th position,so to avoid counting of the ‘\0’-Null character at the end of the string as a special character.




Back to main directory:  C++ Assignment    Software Practical


Get Free Programming Tutorials and Solved assignments