写一函数 将两个字符串中的元音字母和其他字母分开成两个字符串,然后在主函数中输出
写一函数 将两个字符串中的元音字母和其他字母分开成两个字符串,然后在主函数中输出
思路:用两个数组保存,一个保存元音字母,另一个保存不是元音字母的字符串。
程序代码:#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define N 100
int main()
{
char s1[N] = { 0 };
char s2[N] = { 0 };
char res_yuan[N*2] = { 0 };
char res_fu[N*2] = { 0 };
char yuan[] = "aeiou";
int i = 0,j = 0,count_yuan = 0, count_fu = 0;
printf("请输入两个字符串:\n");
scanf("%s%s",s1,s2);
for (i = 0; i < strlen(s1); i++)
{
for (j = 0; j < strlen(yuan); j++)
{
if (s1[i] == yuan[j])
{
res_yuan[count_yuan] = s1[i];
count_yuan++;
}
}
if (j == strlen(yuan))
{
res_fu[count_fu] = s1[i];
count_fu++;
}
}
for (i = 0; i < strlen(s2); i++)
{
for (j = 0; j < strlen(yuan); j++)
{
if (s2[i] == yuan[j])
{
res_yuan[count_yuan] = s2[i];
count_yuan++;
}
}
if (j == strlen(yuan))
{
res_fu[count_fu] = s2[i];
count_fu++;
}
}
printf("元音字母有:%s\n", res_yuan);
printf("辅音字母有:%s\n", res_fu);
}
程序代码:#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define N 100
int main()
{
char s1[N] = { 0 };
char s2[N] = { 0 };
char res_yuan[N*2] = { 0 };
char res_fu[N*2] = { 0 };
char yuan[] = "aeiou";
int i = 0,j = 0,count_yuan = 0, count_fu = 0;
printf("请输入两个字符串:\n");
scanf("%s%s",s1,s2);
for (i = 0; i < strlen(s1); i++)
{
for (j = 0; j < strlen(yuan); j++)
{
if (s1[i] == yuan[j])
{
res_yuan[count_yuan] = s1[i];
count_yuan++;
break;
}
}
if (j == strlen(yuan))
{
res_fu[count_fu] = s1[i];
count_fu++;
}
}
for (i = 0; i < strlen(s2); i++)
{
for (j = 0; j < strlen(yuan); j++)
{
if (s2[i] == yuan[j])
{
res_yuan[count_yuan] = s2[i];
count_yuan++;
break;
}
}
if (j == strlen(yuan))
{
res_fu[count_fu] = s2[i];
count_fu++;
}
}
printf("元音字母有:%s\n", res_yuan);
printf("辅音字母有:%s\n", res_fu);
}