I have a problem where I have to print a string read from the console after the following rule: After two successive vowels or consonants I need to print the character _ but not if there are two vowels or two consonants at the end of the string, then I have to print the number of the substrings separated by each _ character. The string is read until EOF.
I have run into two problems:
The program will not terminate unless I type EOF (Ctrl-Z) on a new line. If I just add it at the end of the string, it will continue.
I need a way to check if the last character printed is '_', and to remove it if it is at the end of the printed string.
Here's my code:
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
int main()
{
char vow[]="aeiouAEIOU", c,x;
int nr=1;
x=getchar();
putchar(x);
while((c=getchar())!=EOF)
{
if(isalpha(c))
{
if (((strchr(vow,x)&&strchr(vow,c))||(!strchr(vow,x)&&!strchr(vow,c)))&&x!=0)
{
putchar(c);
putchar('_');
nr++;
x=0;
}
else
{
putchar(c);
x=c;
}
}
}
printf("\n%d",--nr);
return 0;
}