【发布时间】:2019-08-27 22:27:36
【问题描述】:
我在尝试将字符数组中的字母接收到我的 wordCount 函数以计算数组中每个项目中的单词数时遇到问题。我相信我应该只操作该函数,但不清楚如何将 testCases 数组中的单个字母放入字数统计函数中。
在那之后,我假设我会使用 if 语句来检查读入 wordCount 的字符是否是字母,以及何时结束将它们计为一个单词。
代码如下:
#include <iostream>
using namespace std;
// Function Prototype
int wordCount (char *userEntry);
int main() {
// Constants
const int MAX_LENGTH = 150;
// Local variables
char testCases[][MAX_LENGTH + 1] = { "0",
" 1 22 3333 44444 ",
" testing ",
"a",
"onetwothree",
"one two three",
" testing a 11 222 three 4 five ",
"a b c d e f" };
int wCount = 0;
// loop through test cases and display number of words in each
for (char *entry : testCases) {
wCount = wordCount(entry);
cout << "\nNumber of words in the test case '" << entry << "' is: "
<< wCount << endl;
}
return EXIT_SUCCESS;
}
/*
Function Name: wordCount
This function counts the # of space-delimited words
in a character string, and returns the count to the
caller.
NOTE: A word is defined as one or more alphabetic
characters separated by one or more spaces,
unless it is the only alphabetic character(s).
*/
int wordCount (char *userEntry) {
return 0;
}
【问题讨论】:
标签: c++ algorithm function char word-count