此程序计算特定字符在字符串中出现的次数。编译器向我显示一个不完整的输出,但没有显示字符在输入中显示的次数然后突然关闭
Enter a string (up to 50 characters): k;kl;kl;k;kljhh Enter a character and I will tell you how many times it appears in the string: k k appears Press any key to continue...
我正在使用VS社区。 p>
// This program demonstrates a function, countChars, that counts
// the number of times a specific character appears in a string.
#include <iostream>
using namespace std;
int countChars(char *, char); // Function prototype
int main()
{
const int SIZE = 51; // Array size
char userString[SIZE]; // To hold a string
char letter; // The character to count
// Get a string from the user.
cout << "Enter a string (up to 50 characters): ";
cin.getline(userString, SIZE);
// Get a character to count occurrences of within the string.
cout << "Enter a character and I will tell you how many\n";
cout << "times it appears in the string: ";
cin >> letter;
// Display the number of times the character appears.
cout << letter << " appears ";
cout << countChars(userString, letter) << " times.\n";
return 0;
}
//****************************************************************
// Definition of countChars. The parameter strPtr is a pointer *
// that points to a string. The parameter Ch is a character that *
// the function searches for in the string. The function returns *
// the number of times the character appears in the string. *
//****************************************************************
int countChars(char *strPtr, char ch)
{
int times = 0; // Number of times ch appears in the string
// Step through the string counting occurrences of ch.
while (*strPtr != '\0')
{
if (*strPtr == ch) // If the current character equals ch...
times++; // ... increment the counter
strPtr++; // Go to the next char in the string.
}
return times;
}
答案 0 :(得分:0)
无法在Ubuntu上重现。也许是一个Windows问题?
#include <iostream>
#include <iomanip>
class T590_t
{
std::stringstream ssin;
public:
T590_t() = default;
~T590_t() = default;
int exec(int , char** )
{
ssin << "k;kl;kl;k;kljhh\nk"; // for consistent behavior, use stringstream for input
// the letter ------------^
const int SIZE = 51; // Array size
char userString[SIZE]; // To hold a string
// Get a string from the user.
std::cout << "\n Enter a string (up to 50 characters): ";
(void)ssin.getline(userString, SIZE);
std::cout << "\n '" << userString << "'" << std::endl; // echo input
// Get a character to count occurrences of within the string.
std::cout << "\n Enter a character and I will tell you how many times it appears in the string: ";
char letter = '\0'; // The character to count, initialized
ssin >> letter;
std::cout << " '" << letter << "'" << std::endl; // echo input
// Display the number of times the character appears.
std::cout << "\n " << letter << " appears "
<< countChars(userString, letter) << " times.\n";
return 0;
}
private: // methods
int countChars(char *strPtr, char ch)
{
int times = 0; // Number of times ch appears in the string
// Step through the string counting occurrences of ch.
while (*strPtr != '\0')
{
if (*strPtr == ch) // If the current character equals ch...
times++; // ... increment the counter
strPtr++; // Go to the next char in the string.
}
return times;
}
}; // class T590_t
int main(int argc, char* argv[])
{
T590_t t590;
return t590.exec(argc, argv);
}
带输出:
Enter a string (up to 50 characters):
'k;kl;kl;k;kljhh'
Enter a character and I will tell you how many times it appears in the string: 'k'
k appears 5 times.
答案 1 :(得分:0)
您可以传递一个字符数组及其大小:
int GetCharCount(char[], const int, const char);
int main(){
char text[] = "Hello there!";
cout << GetCharCount(text, strlen(text), 'e') << endl;
cout << endl << endl;
cin.get();
return 0;
}
int GetCharCount(char pTxt[], const int size, const char c){
int count = 0;
for(int i(0); i != size; i++)
if(c == pTxt[i])
count++;
return count;
}