我想从给定的字符串中获取所有元音子字符串。给定的字符串是' auiouxaeibaou ',从给定的字符串中获取子字符串,例如[ auiou,aei,aou ]。
在这里,我尝试了类似的方法,但是没有得到确切的输出。
bool isVowel(char c) {
return (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u');
}
void substr(char str[], int low, int high)
{
printf("%.*s \n\n ", high-low+1, (str+low));
}
int main(int argc, const char *argv[]) {
char str[] = "aeixae";
int length = strlen(str);
int start_index = 0, end_index = 0;
for (int x=0; x<length; x++) {
char c = str[x];
if (isVowel(c) == false) {
end_index = x;
substr(str, start_index, end_index - 1 );
start_index = end_index + 1;
}
}
return 0;
}
您的尝试接近了。我只是添加了includes,并确保字符串的最后部分也已打印。请参见for循环的终止以及if在哪里检查元音。
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
bool isVowel(char c) {
return (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u');
}
void substr(char str[], int low, int high)
{
printf("%.*s \n\n", high-low+1, (str+low));
}
int main(int argc, const char *argv[]) {
char str[] = "aeixae";
int length = strlen(str);
int start_index = 0, end_index = 0;
for (int x=0; x<=length; x++) {
if (x == length || !isVowel(str[x])) {
end_index = x;
substr(str, start_index, end_index - 1 );
start_index = end_index + 1;
}
}
return 0;
}
这是输出:
gcc main.c && ./a.out
aei
ae
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句