Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/129.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+中字符数组中的单词数+;_C++_Algorithm_Function_Char_Word Count - Fatal编程技术网

C++ 如何计算C+中字符数组中的单词数+;

C++ 如何计算C+中字符数组中的单词数+;,c++,algorithm,function,char,word-count,C++,Algorithm,Function,Char,Word Count,我在尝试将字符数组中的字母接收到wordCount函数中以计算数组中每个项目的字数时遇到了问题。我认为我应该只操作这个函数,但不清楚如何将testCases数组中的单个字母输入单词计数函数 在此之后,我假设我将使用if语句来检查读入wordCount的字符是否为字母,以及何时结束将其作为单词进行计数 代码如下: #include <iostream> using namespace std; // Function Prototype int wordCount (char *us

我在尝试将字符数组中的字母接收到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;
}
#包括
使用名称空间std;
//功能原型
int字数(char*userEntry);
int main(){
//常数
const int MAX_LENGTH=150;
//局部变量
char testCases[][MAX_LENGTH+1]={“0”,
"    1   22    3333    44444 ",
“测试”,
“a”,
“一二三”,
“一二三”,
“测试11 222 3 4 5”,
“a b c d e f”};
int wCount=0;
//循环测试用例并显示每个测试用例中的字数
for(char*条目:testCases){
wCount=字数(条目);

C++ C++中的CUT

字符串为空结束,这意味着在其最后一个字符之后,有一个字符代码<代码> 0x00 < /COD>的“<代码> 0”<代码>字符。要读取字符串/字符数组的每个字符,只需使用索引运算符<代码> []/COD>。C++中的字符串(如其他数组)从0索引到N-1<P/P。 下面是一个循环示例,它将字符串中的一个字符读入一个字符变量

void iterate_through_characters(const char* aString) {
    // starting at n=0 check each n and make sure it is shorter than the
    // width of the array
    // and that it's not the null terminating character
    for (int n = 0; (n < MAX_LENGTH + 1) && (aString[n] != '\0'); n++) {
        // take the character out of the index in the string and store it in aCharacter
        char aCharacter = aString[n];
    }
}
void通过字符进行迭代(常量字符*aString){
//从n=0开始,检查每个n,确保其短于
//阵列宽度
//而且它不是空终止字符
对于(int n=0;(n
对于您的特殊情况,您还需要跟踪您是否已经在一个单词中,并且仅当您还没有在一个单词中时才计算一个新词。下面的函数实现了这一点

int wordCount(const char* input) {

    // this is true if we're in a word
    bool inWord = false; 

    // the number of words we've seen defaulting to 0, no words
    int result = 0;

    for (int n = 0; (n < MAX_LENGTH + 1) && (aString[n] != '\0'); n++) {
       // if this is a space we're not in a word
       if (aString[n] == ' ') {
           inWord = false; // if we were in a word, we aren't now
       } else if (!inWord) {
           inWord = true; // if we weren't in a word, we are now
           result ++; // increment the number of words we've seen 
       }
   }
   return result;
}
int字数(常量字符*输入){
//如果我们用一句话来说,这是真的
bool-inWord=false;
//我们看到的字数默认为0,没有字数
int结果=0;
对于(int n=0;(n< C++ > C++中的字符串为null结尾,这意味着在其最后一个字符之后,有一个字符代码<代码> 0x00 < /代码>的代码< >代码> 0代码'/COD>字符。
下面是一个循环示例,它将字符串中的一个字符读入一个字符变量

void iterate_through_characters(const char* aString) {
    // starting at n=0 check each n and make sure it is shorter than the
    // width of the array
    // and that it's not the null terminating character
    for (int n = 0; (n < MAX_LENGTH + 1) && (aString[n] != '\0'); n++) {
        // take the character out of the index in the string and store it in aCharacter
        char aCharacter = aString[n];
    }
}
void通过字符进行迭代(常量字符*aString){
//从n=0开始,检查每个n,确保其短于
//阵列宽度
//而且它不是空终止字符
对于(int n=0;(n
对于您的特殊情况,您还需要跟踪您是否已经在一个单词中,并且仅当您还没有在一个单词中时才计算一个新词。下面的函数实现了这一点

int wordCount(const char* input) {

    // this is true if we're in a word
    bool inWord = false; 

    // the number of words we've seen defaulting to 0, no words
    int result = 0;

    for (int n = 0; (n < MAX_LENGTH + 1) && (aString[n] != '\0'); n++) {
       // if this is a space we're not in a word
       if (aString[n] == ' ') {
           inWord = false; // if we were in a word, we aren't now
       } else if (!inWord) {
           inWord = true; // if we weren't in a word, we are now
           result ++; // increment the number of words we've seen 
       }
   }
   return result;
}
int字数(常量字符*输入){
//如果我们用一句话来说,这是真的
bool-inWord=false;
//我们看到的字数默认为0,没有字数
int结果=0;
对于(int n=0;(n