Question:4
C program to take input for a string and display it and also print the following:
(Using inbuilt functions):
* total length i.e. total characters
* total alphabets
* total upper case alphabet
* total lower case alphabets
* total digits
* total spaces
* total special characters
Sol:
/* Using for loop */
#include<stdio.h>
#include<ctype.h>
int main()
{
char n[20];
int i=0,a=0,ua=0,la=0,d=0,sp=0,spl=0;
printf("Enter any string ");
gets(n);
for(i=0;n[i]!='\0';i++)
{
printf("%c\n",n[i]);
if(isalpha(n[i]))
{
a++;
if(isupper(n[i]))
ua++;
else
la++;
}
else
if(isdigit(n[i]))
d++;
else
if(isspace(n[i]))
sp++;
else
spl++;
}
printf("Len = %d\n",i);
printf("Total alphabets = %d upper alphabets = %d lower alphabets = %d\n",a,ua,la);
printf("digits = %d spaces = %d special characters = %d\n",d,sp,spl);
return(0);
}
/* Using while loop */
#include<stdio.h>
#include<ctype.h>
int main()
{
char n[20];
int i=0,a=0,ua=0,la=0,d=0,sp=0,spl=0;
printf("Enter any string ");
gets(n);
while(n[i]!='\0')
{
printf("%c\n",n[i]);
if(isalpha(n[i]))
{
a++;
if(isupper(n[i]))
ua++;
else
la++;
}
else
if(isdigit(n[i]))
d++;
else
if(isspace(n[i]))
sp++;
else
spl++;
i++;
}
printf("Len = %d\n",i);
printf("Total alphabets = %d upper alphabets = %d lower alphabets = %d\n",a,ua,la);
printf("digits = %d spaces = %d special characters = %d\n",d,sp,spl);
return(0);
}
/* Output */ Enter any string He324 %^llo H e 3 2 4 % ^ l l o Len = 11 Total alphabets = 5 upper alphabets = 1 lower alphabets = 4 digits = 3 spaces = 1 special characters = 2
Question:5
C program to take input for a string display the string, also count and print total alphabets, vowels and consonants in the string.
Sol:
/* Using for loop */
#include<stdio.h>
#include<ctype.h>
int main()
{
char n[20];
int i=0,a=0,v=0,c=0;
printf("Enter any string ");
gets(n);
for(i=0;n[i]!='\0';i++)
{
printf("%c\n",n[i]);
if((n[i]>='A' && n[i]<='Z') || (n[i]>='a' && n[i]<='z'))
{
a++;
if(n[i]=='A' || n[i]=='a' || n[i]=='E' || n[i]=='e' || n[i]=='I' || n[i]=='i' || n[i]=='O' || n[i]=='o' || n[i]=='U' || n[i]=='u')
v++;
else
c++;
}
}
printf("Len = %d\n",i);
printf("Total alphabets = %d\n",a);
printf("Total vowels = %d consonents = %d \n",v,c);
return(0);
}
/* Output */ Enter any string computer c o m p u t e r Len = 8 Total alphabets = 8 Total vowels = 3 consonents = 5
Question:6
C program to take input for a string, display the string in such a manner that all upper case alphabets are displayed as lower case, and rest are displayed as it is.
Sol:
/* Using for loop */
#include<stdio.h>
#include<ctype.h>
int main()
{
char n[20];
int i=0;
printf("Enter any string ");
gets(n);
for(i=0;n[i]!='\0';i++)
{
if(n[i]>='A' && n[i]<='Z')
printf("%c",n[i]+32);
else
printf("%c",n[i]);
}
return(0);
}
/* Using while loop */
#include<stdio.h>
#include<ctype.h>
int main()
{
char n[20];
int i=0;
printf("Enter any string ");
gets(n);
while(n[i]!='\0')
{
if(n[i]>='A' && n[i]<='Z')
printf("%c",n[i]+32);
else
printf("%c",n[i]);
i++;
}
return(0);
}
/* Output */ Enter any string COMPuter23 computer23




