gpt4 book ai didi

c - 说出句子中出现的元音数量

转载 作者:行者123 更新时间:2023-11-30 20:11:58 26 4
gpt4 key购买 nike

程序必须说出用户输入的句子中出现的元音数量。你必须以一个观点来结束。问题是声明数组时有一个不正确的语句。

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

/*Program EJ004*/

const char vowels[11] = {'A','E','I','O','U',' ','‚','¡','ó','£',''};
char letgoods[93]= {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','0','1','2','3','4','5','6','7','8','9',
' ','.',',',':',';','(',')','-','¿',' ','‚','¡','ó','£',
' ','?',"",'!','"','%','/','<','>'};

//then the program is correct

char letter;
char phrase[80];
int n, t;

int main(){
int index, numvowels;
printf("Write your phrase, and ends with a point.");
index = 0;
numvowels = 0;
do{
letter = getchar();
for (t=0; t<93;t++){
if (letter == letgoods[t])
{
//to not save special characters.

index++;
printf("%c",letter);

phrase[index] = letter;
for (n=0; n<11;n++){
if (toupper(letter) == vowels[n]) { numvowels++;
}
}
}
}
}while ((index < 80) || (letter != '.'));
printf("\n\n");
printf("The phrase has %d vowels.",numvowels);
getch();
return 0;
}

最佳答案

代码存在这些问题。

const char vowels[11] = {'A','E','I','O','U',' ','‚','¡','ó','£',''};

您不能使用'' - 它是空的,并且不能使用空来初始化 char 值。您可以使用 0 来指示此处没有字符:

const char vowels[11] = {'A','E','I','O','U',' ','‚','¡','ó','£',0};

该数组有两个双引号

char letgoods[93]= {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','0','1','2','3','4','5','6','7','8','9',
' ','.',',',':',';','(',')','-','¿',' ','‚','¡','ó','£',
' ','?',
//What was intended here?
"",
//""
'!','"','%','/','<','>'};

当您迭代 char 数组时,您可以使用 <=而不是< ,这会让你遍历数组,因为 C 中的数组是从 0 开始索引的。你需要这样做

for (t=0; t<93;t++)
....
while ((index < 80) || (letter != '.'))

关于c - 说出句子中出现的元音数量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38174980/

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