Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/156.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 数一数每个单词的元音_C++ - Fatal编程技术网

C++ 数一数每个单词的元音

C++ 数一数每个单词的元音,c++,C++,我得数一数给定课文中每个单词的元音。我的尝试: #include <iostream> #include <string.h> using namespace std; char s[255], *p, x[50][30]; int c; int main() { cin.get(s, 255); cin.get(); p = strtok(s, "?.,;"); int n = 0; while (p) {

我得数一数给定课文中每个单词的元音。我的尝试:

#include <iostream>
#include <string.h>

using namespace std;

char s[255], *p, x[50][30];
int c;

int main()
{
    cin.get(s, 255);
    cin.get();
    p = strtok(s, "?.,;");
    int n = 0;
    while (p)
    {
        n++;
        strcpy(x[n], p);
        p = strtok(NULL, "?.,;");
    }
    for (int i = 1; i <= n; i++)
    {
        c = 0;
        for (int j = 0; j < strlen(x[i]); j++)
            if (strchr("aeiouAEIOU", x[i][j]))
                c++;
        cout << c << " ";
    }
    return 0;
}

PS:我知道我的代码是C和C++之间的混合,但这是我在学校里教的。

< P>这是我的解决方案:

#include <iostream>
#include <string.h>
using namespace std;

int main()
{
    char s[255];
    int n,i,counter=0;
    cin.get(s,255);
    for(i=0; i<=strlen(s)-1; i++)

        if(s[i]=='a' || s[i]=='e' || s[i]=='i' || s[i]=='o' || s[i]=='u') counter++;

     cout<<counter;
    return 0;
}
如果你有一个元音a、e、i、o或u,那么你就是在计数器上加起来。
您也可以使用strhr,但这是一种更简单、更容易理解的方法。

在注释中关闭了Case

然而,为了好玩,我建议您使用另一个变体,它避免使用糟糕的strtok,不需要危险的strcpy,并且只处理一个输入字符

因为你是老师的混合风格,显然不应该使用C++字符串,我也尊重这个约束:

const char separators[]=" \t?.,;:"; // I could put them in the code directly
const char vowels[]="aeiouyAEIOUY"; //    but it's for easy maintenance
int vowel_count=0, word_count=0;
bool new_word=true; 
char *p=s;
cout << "Vowels in each word: ";
do  {
    if (*p=='\0' || strchr(separators,*p)) {
        if (!new_word) {   // here, we've reached the end of a word
            word_count++; 
            cout << vowel_count << " ";
            vowel_count = 0; 
            new_word=true; 
        }                 // else it's still a new word since consecutive separators
    } 
    else {               // here we are processing real chars of a word
        new_word=false;   // we have at least on char in our word
        if (strchr(vowels, *p))
            vowel_count++;
    }
} while (*p++);  // It's a do-while so not to repeat the printing at exit of loop
cout << endl<<"Words: "<<word_count<<endl; 

@GaneshChowdharySadanala这已经几十年没有出现过了。您是在寻求帮助解决此问题,还是对代码的反馈意见,或者别的什么?问题是我的代码计算了文本中所有的元音并打印出那个数字,而不是计算每个单词的元音并打印出来。@Rup非常感谢。我忘了添加空格作为分隔符。@GaneshChowdharySadanala:没错。仔细阅读答案。你可以使用ifs[i]=''counter=0这样每当两个字之间有空格时,计数器就会重置。是的,处理所有其他分隔符,并使用来降低。您还可以选择一个开关,以便更容易地枚举所有情况