STRING

 Chapter 3.

STRING






Q1.WAP in c to count total number of vowel and consonant in string.

#include<stdio.h>
#include<conio.h>
int main()
{
 char str[100],i,len=0,v=0,c=0;
 printf("enter the string:");
 gets(str);
 for(len=0; str[len]!='\0';len++);
 for(i=0;i<len;i++)
{
 if(str[i]=='a'||str[i]=='e'||str[i]=='i'||str[i]=='o'||str[i]=='u'||str[i]=='A'||str[i]=='E'||str[i]=='I'||str[i]=='O'||str[i]=='U')
 {
  v=v+1;
 }
 else
 {
  c=c+1;
 }
}
printf("vowel count is %d\n",v);
printf("consonant count is %d\n",c);
return 0;
}


Q2.WAP in c to count total number of alphabets, digits and special characters in a string.

#include<stdio.h>
#include<conio.h>
int main()
{
 char str[100],i,len=0,a=0,d=0,s=0;
 printf("enter the string:");
 gets(str);
 for(len=0; str[len]!='\0';len++);
 for(i=0;i<len;i++)
{
 if((str[i]>='A' && str[i]<='Z') ||(str[i]>='a' && str[i]<='z'));
 {
  a=a+1;
 }
 else if(str[i]>='0' && str[i]<='9')
 {
  d=d+1;
 }
 else
 {
  s=s+1;
 }
 }
 printf("total alphabet character is %d",a);
 printf("total digit count is %d",d);
 printf("total special character is %d",s);
return 0;
}


Q3.WAP in c to replace the spaces of string with '*' .

#include<stdio.h>
#include<conio.h>
int main()
{
 char str[100],i,len=0;
 printf("enter the string:");
 gets(str);
 for(len=0; str[len]!='\0';len++);
 for(i=0;i<len;i++)
{
   if(str[i]=' ')
   str[i]='*';
 
 }
 printf("the resultant string is %s",str);
 
return 0;
}


Q4.WAP to count total 'the' in a string.

#include<stdio.h>
#include<conio.h>
int main()
{
 char str[100],i,len=0,c=0;
 printf("enter the string:");
 gets(str);
 for(len=0; str[len]!='\0';len++);
 for(i=0;i<len;i++)
{
   if((str[i]=='t'||str[i]=='T')&&(str[i+1]=='h'||str[i+1]=='H')&&(str[i+2]=='E'||str[i+2]=='e')&&(str[i+3]==' '||str[i+3]=='\0'))
   {
      c=c+1;
   }
 
 }
 printf("the number of 'the' in a string is %d",c);
 
return 0;
}


Q5.WAP to count the total numbers of words in a string.

#include<stdio.h>
#include<conio.h>
int main()
{
 char str[100],i,len=0,c=0,ch=0;
 printf("enter the string:");
 gets(str);
 for(len=0; str[len]!='\0';len++);
 for(i=0;i<len;i++)
{
   if(str[i]==' ')
   c=c+1;
 }
 for(i=0;str[i]!='\0';i++)
 {
   ch=ch+1;
 }
 printf("the number of words in a string is %d\n",c+1);
  printf("the number of character in a string is %d\n",ch);
 
return 0;
}


























Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.