Thursday, October 15, 2015

Program for String Operations

void main()
{
char str1[20],c;
int i,j,len,vow=0,a=0, the=0;
clrscr();
printf("*********STRING OPERATION PROGRAM********");
printf("\n\tEnter the String: ");
gets(str1);     //gets() inputs a string & saves into str1 variable

len=strlen(str1);  //strln() is used to calculate length of string
printf("\n\tLength of String is: %d",len);
//Below code will find vowels in a string str1

for(i=0;i<len;i++) //this will check string str1 from index 0 to its length
{
if(str1[i]=='a'|| str1[i]=='i'|| str1[i]=='e'|| str1[i]=='o'|| str1[i]=='u')
vow++;
}
if(vow==0)
printf("\n\tString contains no vowels");
else
printf("\n\tNo. of Vowels in string str1: %d",vow);
       //End of finding vowels
       //Below code will find number of occurance of 'a'
       for(i=0;i<len;i++)
       {
if(str1[i]=='a')
a++;

       }
       if(a==0)
       printf("\n\tString co ntains no 'a' character");
       else
       printf("\n\tNo. of 'a' in String: %d",a);
       //End of finding character 'a'
       //Below code will find 'the' in string
       for(i=0;i<len;i++)
       {
 if(str1[i]=='t')
 { i++;
   if(str1[i]=='h')
   {
    i++;
    if(str1[i]=='e')
    the++;
   }
  }
       }
       printf("No. of occurance of 'the': %d",the);
       //End of finding 'the'
       getch();

//End of program

}

No comments:

Post a Comment