Вот и вы.
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
bool isVowel( char c)
{
const char *vowels = "aeiou";
return strchr( vowels, c ) != NULL;
}
void output_substring( const char *s, int n )
{
printf( "%*.*s\n", n, n, s );
}
int main(void)
{
char s[] = "aeixae";
size_t count = 0;
for ( const char *p = s; *p != '\0'; )
{
while ( *p != '\0' && !isVowel( *p ) ) ++p;
if ( *p != '\0' )
{
++count;
const char *q = p;
while ( *p != '\0' && isVowel( *p ) ) ++p;
output_substring( q, p - q );
}
}
printf( "There are %zu substrings\n", count );
return 0;
}
Вывод программы:
aei
ae
There are 2 substrings
Другой подход заключается в использовании стандартных C функций strcspn
и strspn
вместо ; loop.
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
void output_substring( const char *s, int n )
{
printf( "%*.*s\n", n, n, s );
}
int main(void)
{
char s[] = "aeixae";
size_t count = 0;
const char *vowels = "aeiou";
for ( const char *p = s; *p != '\0'; )
{
p += strcspn( p, vowels );
if ( *p != '\0' )
{
++count;
const char *q = p;
p += strspn( p, vowels );
output_substring( q, p - q );
}
}
printf( "There are %zu substrings\n", count );
return 0;
}
Вывод программы такой же, как показано выше.