나는 하루 종일이 프로그램에 붙어 있습니다. 나는 마침내 내가 정말로 가까워지고있는 것처럼 느낀다. 문자열에서 모음 및 문자의 수를 찾아야합니다. 그런 다음 끝에 출력하십시오. 그러나 프로그램을 컴파일 할 때 충돌이 발생합니다. 나는 구문을 점검하고 하루 종일 내 책을 들여다 보았다. 누구든지 도와 주시면 감사하겠습니다! C 문자열을 조작하는 비슷한 함수를 5 개 더 작성했기 때문입니다. 감사! 난 당신이 std::string
또는 std::getline
를 사용하지 않도록하고 있음을 제한한다고 가정하겠습니다포인터를 사용하여 C-String 인수 전달
#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'|| 'u'|| ''A ''가 0이 아니기 때문에 'U'가 항상 참이 될 것입니다. – chris
잠시 동안 널 종료자인 '/ 0'을 확인하기 때문에 이해할 수 없습니다. – neganando
그 표현식으로부터 어떤 특정한 'bool' 조건을 찾고 있습니까? 힌트 :'* strPtr == 'a'|| * strPtr == 'A'|| * strPtr == 'e'.....'. 모든 것을 타이핑하고 나면, 모든 테스트 숯을 하나의 문자열 상수 ('const char vowels [] = "aAeEiIoOuU";')에 넣고 식을'if (strchr (모음, * strPtr))' – WhozCraig