坚持无限循环和空格检测c ++

时间:2016-10-21 17:39:20

标签: c++

这是需要实施的问题:

  

编写一个C ++程序,在句点出现时停止读取一行文本   输入并以正确的间距和大小写显示句子。对于此程序,正确的间距意味着单词之间只有一个空格,除第一个字母外,所有字母都应小写。例如,如果用户输入文本“我要去看电影。”,显示的句子应该是“我要去看电影。”

我写了一段看起来像这样的代码:

// Processing a sentence and verifying if it is grammatically correct or not (spacing and capitalization)
//#include <stdio.h>
//#include <conio.h>
#include <iostream>
#include <string>

using namespace std;

int main()
{
string sentence;
cout << "Enter the sentence: ";
getline(cin, sentence);
int len = sentence.length();

// Dealing with capitalizations
for (int j = 0; j <= len; j++)
{
     if (islower(sentence[0]))
            sentence[0] = toupper(sentence[0]);
     if(j>0)
            if(isupper(sentence[j]))
                sentence[j] = tolower(sentence[j]);
}

 int space = 0;
do
{
    for (int k = 0; k <= len; k++)
    {
        if(isspace(sentence[k]))
        {
            cout << k << endl;
            int n = k+1;
            if(sentence[n] == ' ' && n <=len)
                {
                    space++;
                    cout << space <<endl;
                    n++;
                    cout << n <<endl;
                }
            if(space!= 0)
                sentence.erase(k,space);
            cout << sentence <<endl;
        }

    }
    len = sentence.length();
    //cout << len <<endl;
 } while (space != 0);

}

有了这个,我能够处理大写问题但是当我试图检查两个单词之间的多个空格时会出现问题。在do循环中,我不知何故陷入无限循环。

就像当我尝试在do-while循环中的第一行中打印字符串(len/len1)的长度时,它会继续在无限循环中运行。类似地,当我尝试在for循环之后打印k的值时,它再次进入无限循环。我认为这与我使用do-while循环有关,但我无法理解它。

这是我收到的输出。 Check this out

2 个答案:

答案 0 :(得分:0)

此代码存在一些不同的问题,但我相信下面的代码可以解决这些问题。希望这段代码足够可读,你可以学习一些技巧。例如,不需要将循环中的第一个字母大写,只需执行一次并完成它。

无限循环的常见问题是从不满足循环终止条件 - 确保无论循环中发生什么都会满足它。

#include <iostream>
#include <string>
using namespace std;

int main() {
    string sentence;
    cout << "Enter the sentence: ";
    getline(cin, sentence);
    int len = sentence.find(".", 0) + 1;  // up to and including the period

    // Dealing with capitalizations
    if (islower(sentence[0]))
        sentence[0] = toupper(sentence[0]);
    for (int j = 1; j < len; j++)
        if(isupper(sentence[j]))
            sentence[j] = tolower(sentence[j]);

    // eliminate duplicate whitespace
    for (int i = 0; i < len; i++)
        if (isspace(sentence[i]))
            // check length first, i + 1 as index could overflow buffer
            while (i < len && isspace(sentence[i + 1])) {
                sentence.erase(i + 1, 1);
                len--;  // ensure sentence decreases in length
            }

    cout << sentence.substr(0, len) << endl;
}

答案 1 :(得分:0)

这是

std::string sentence;
std::string new_sentence;
std::cout << "Enter the sentence: ";
std::getline(std::cin, sentence);

bool do_write = false; // Looking for first non-space character

bool first_char = true;
// Loop to end of string or .
for (unsiged int i = 0; i < sentence.length() && sentence[i] != '.'; ++i) {
   if (sentence[i] != ' ') { // Not space - good - write it
     do_write = true;
   }
   if (do_write) {
      new_sentence += (first_char ? toupper(sentence[i]) : tolower(sentence[i]);
      first_char = false;
   }
   if (sentence[i] == ' ') {
     do_write = false; // No more spaces please
   }
}
if (i < sentence.length()) {  // Add dot if required
  new_sentence += '.';
}