【问题标题】:While loop not seeing or finding terminating null character虽然循环没有看到或找到终止空字符
【发布时间】:2013-11-17 16:48:23
【问题描述】:

我正在尝试使用 while 循环使用 '\0' 作为终止条件来迭代 char 数组,但我的问题是它直到索引位置 481 才找到 '\0',数组被声明为 200很长,我看不出我做错了什么!在有人问之前,我不能为此使用字符串或任何形式的字符串函数。谁能帮忙??

#include <iostream>

using namespace std;

int main()
{


char fullString[200]={'\0'}; // Declare char string of 200, containing null characters
int alphaCount = 0;
int charCount = 0;
int wordCount = 0;

cin.getline(fullString,200); //
cout << "\n\n" << fullString;
cout << "\n\n\n";

int i=0;
int i2 = 0;
while(fullString[i]!='\0'){ //iterate through array until NULL character is found

    cout << "\n\nIndex pos : " << fullString[i]; //Output char at 'i' position


   while(fullString[i2]!= ' '){ //while 'i' is not equal to SPACE, iterate4 through array

        if(isalpha(fullString[i2])){

            alphaCount++; // count if alpha character at 'i'
        }
        charCount++; // count all chars at 'i'
        i2++;
    }

    if(charCount == alphaCount){ // if charCount and alphaCount are equal, word is valid

        wordCount++;
    }
   charCount = 0; // zero charCount and alphaCount
   alphaCount = 0;

   i=i2;// Assign the position of 'i2' to 'i'
   while(fullString[i] == 32){ //if spaces are present, iterate past them

        i++;
        cout << "\n\ntest1";

   }
    i2 = i; // assign value of 'i' to 'i2' which is the next position of a character in the array

    if(fullString[i] == '\0')
    {
        cout << "\n\nNull Character " << endl;
        cout << "found at pos: " << i << endl;
    }

}

cout << "\n\ni" << i;
cout << "\n\nWord" << wordCount;

return 0;

}

【问题讨论】:

    标签: c++ arrays while-loop char


    【解决方案1】:

    正如其他人指出的那样,您的问题出在内部循环上。您测试空格字符但不测试 NULL,因此它会迭代到最后一个单词的结尾,因为最后一个单词之后没有空格字符。

    通过改变你的while条件很容易解决这个问题:

    while(fullString[i2]!= ' ')
    

    ...对此:

    while(fullString[i2] && fullString[i2]!= ' ')
    

    这会将您的内部 while 循环更改为首先测试非 NULL,然后测试非空格。

    我没有更正您的其余代码,因为我认为这是一个类项目(看起来像一个),所以我将回答限制在您的问题范围内。

    【讨论】:

    • 这是完美的比尔,几周前才开始使用 C++,仍然习惯于语法。这完美地解决了我的问题。现在让整个程序运行起来!!
    【解决方案2】:

    你没有检查内循环

       while(fullString[i2]!= ' '){ //while 'i' is not equal to SPACE, iterate4 through array
    
            if(isalpha(fullString[i2])){
    
                alphaCount++; // count if alpha character at 'i'
            }
            charCount++; // count all chars at 'i'
            i2++;
        }
    
    ...
    i=i2;// Assign the position of 'i2' to 'i'
    

    下一个字符是否等于'\0'

    【讨论】:

      【解决方案3】:

      这是因为内部循环不检查终止,它们只是继续循环,甚至超过字符串的末尾。


      顺便说一句,如果你想计算单词、空格和非空格字符的数量,C++ 中有更简单的方法。参见例如std::count and std::count_if 用于空格和字符。例如:

      std::string input = "Some string\twith   multiple\nspaces in it.";
      int num_spaces = std::count_if(std::begin(input), std::end(input),
          [](const char& ch){ return std::isspace(ch); });
      

      统计字数可以使用std::istringstreamstd::vectorstd::copystd::istream_iteratorstd::back_inserter

      std::istringstream iss(input);
      std::vector<std::string> words;
      std::copy(std::istream_iterator<std::string>(iss),
                std::istream_iterator<std::string>(),
                std::back_inserter(words));
      

      在上面的代码之后,words向量的大小就是字数。

      如果您使用例如std::copy_if 那么你也可以将上面的代码用于其他情况(但std::count_if 更适合单字符类)。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-03-25
        • 1970-01-01
        • 1970-01-01
        • 2017-04-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多