Thursday, 17 July 2014

C program to count vowels,consonant,spaces,sentences and words with spaces

/*
   Program to count total vowels,consonant,spaces, sentences and words with spaces
  and to erase more than one space between 2 successive words.
*/



#include<stdio.h>
#include<conio.h>
#include<string.h>

char str[50];
void count();
void remove_space();

void main()
{
 clrscr();
 printf("Enter a string:\n");
 gets(str);
 count();
 remove_space();
 getch();
}

void count()
{
 static int i,v,sp,c,sent,wordsp;
 for(;i<(strlen(str));i++)
    {
switch(str[i])
{
 case 'a':
 case 'e':
 case 'i':
 case 'o':
 case 'u':v++;
break;

 case' ' :sp++;
break;

 case '.':sent++;
break;

 default :c++;
break;
}
if(str[i]==' ' && str[i+1]==' ')
 wordsp++;
    }
  printf("\nTotal no of vowels are:%d",v);
  printf("\nTotal no of spaces are:%d",sp);
  printf("\nTotal no of sentences are:%d",sent);
  printf("\nTotal no of constants are:%d",c);
  printf("\nTotal no of words with spaces(more than one space):%d",wordsp);
 }

void remove_space()
{
 static int i,j,k,l,temp;
 temp=l=1;
 while(1 && l!=0)
{
 l=0;
 while(str[i]!='\0')
 {
  k=temp;
  j=i;
  if(str[i]== ' ')
{
if(str[i]==' ' && str[k]==' ')
  {
   while(str[j]!='\0')
   {
str[j]=str[k];
j++;
k++;
l++;
   }
  }

}
  i++;
  temp++;
 }
  i=0;
  temp=1;
}
 printf("\n\nString after removal of more than one blank space:\n%s",str);
}

No comments:

Post a Comment