我一直被困在这个程序中。我终于觉得自己越来越近了。我必须在字符串中找到元音和字符的数量。然后在最后输出它们。但是,当我编译我的程序崩溃。我检查过语法,并整天看着我的书。如果有人可以帮助,我会非常感激!因为我有5个更类似的函数来编写操纵C字符串。谢谢!使用指针传递C字符串参数
#include <iostream>
#include <string>
using namespace std;
int specialCounter(char *, int &);
int main()
{
const int SIZE = 51; //Array size
char userString[SIZE]; // To hold the string
char letter;
int numCons;
// Get the user's input string
cout << "First, Please enter a string (up to 50 characters): " << endl;
cin.getline(userString, SIZE);
// Display output
cout << "The number of vowels found is " << specialCounter(userString, numCons) << "." << endl;
cout << "The number of consonants found is " << numCons << "." << endl;
}
int specialCounter(char *strPtr, int &cons)
{
int vowels = 0;
cons = 0;
while (*strPtr != '/0')
{
if (*strPtr == 'a' || 'A' || 'e' || 'E' || 'i' || 'I' || 'o' || 'O' || 'u' || 'U')
{
vowels++; // if vowel is found, increment vowel counter
// go to the next character in the string
}
else
{
cons++; // if consonant is found, increment consonant counter
// go to the next character in the string
}
strPtr++;
}
return vowels;
}
'* strPtr =='a'|| 'A'|| 'e'|| 'E'|| '我'|| '我'|| 'o'|| 'O'|| 'u'|| 'U''将永远是真实的,因为''A''不是0. – chris 2013-02-12 03:00:09
我不确定我是否理解,因为在检查'/ 0'时,空终止符。 – neganando 2013-02-12 03:02:14
你是否在寻找来自该表达式的任何特定'bool'条件?提示:'* strPtr =='a'|| * strPtr =='A'|| * strPtr =='e'.....'。当你完成所有的输入后,把它扔出来,把所有的测试字符串放在一个字符串常量中('const char vowels [] =“aAeEIIoOuU”;')并将你的表达式改为'if(strchr(元音, * strPtr))' – WhozCraig 2013-02-12 03:02:15