gpt4 book ai didi

c - 如何在C中接受数组中的多个字符串(使用Turbo C++ V3.0)并根据每个字符串包含的元音数量对它们进行排序

转载 作者:行者123 更新时间:2023-11-30 21:30:52 24 4
gpt4 key购买 nike

我需要用户输入句子并将它们存储在一个数组中,然后计算数组每个索引的每个字符串中元音的数量,并根据元音的数量打印排序后的o/p

例如:

char z[10][10]  // this will store sentences in array

但我不知道如何将每个句子与元音相匹配

z[i]=='a' || (all vowels)

可以,但是如何使每个字符串都经过检查?

这是我一直在尝试的代码:

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

void main(){
int i, j;
int count;
char z[10][100];
clrscr();
for(i = 0; i < 3; i++)
{
printf("\nEnter a string: ");
//scanf("%[^\t\n]s", z[i]);
gets(z[i]);
}

for (i = 0; i < 10; i++)
{
count = 0;
for (j = 0; j < 100; j++)
{
if (z[i][j] == 'a')
{
count++;
}
}
}

printf("%d",count);
getch();
}

最佳答案

您可以简单地迭代数组的每个元素以及每个字符串的每个字符。

int totalVocalCount = 0;
int numberOfWords = 10;
int numberOfLetters = 100;
for(int i = 0; i < numberOfWords; i++)
{
int wordVocalCount = 0;
for(int j = 0; j < numberOfLetters; j++)
{
if(z[i][j] == 'a' || ... || z[i] == 'u')
wordVocalCount++;
totalVocalCount++;
}
printf("word %s has %d vocals",z[i], wordVocalCount);
}
printf("all words together have %d vocals",totalVocalCount);

关于c - 如何在C中接受数组中的多个字符串(使用Turbo C++ V3.0)并根据每个字符串包含的元音数量对它们进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23309757/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com