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

时间:2016-07-03 22:46:54

标签: c

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

#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;
}  

1 个答案:

答案 0 :(得分:2)

代码存在这些问题。

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 != '.'))