【问题标题】:C++ count chars in a stringC ++计算字符串中的字符
【发布时间】:2013-11-12 12:53:40
【问题描述】:

我需要计算输入句子中输入字符的数量。我是如此接近,但我不断收到此错误:

countchar.cpp:19:19: error: empty character constant
countchar.cpp: In function â:
countchar.cpp:26:75: error: could not convert â from â to â



#include <string> 
#include <fstream>
#include <iostream>
#include <algorithm>
using namespace std;
void WordOccurenceCount(string, int);
int main()
{
    char character;
    string sentence;
    char answer;
    string cCount;
    while(1) {

                cout << "Enter a char to find out how many times it is in a sentence: ";                       
        cin >> character;
        cout << "Enter a sentence and to search for a specified character: ";
        cin >> sentence;
        if(character == '' || sentence == "" )
    {
            cout << "Please enter a valid answer:\n";
            break;

    }
    else {
        cCount = WordOccurenceCount(sentence.begin(), sentence.end(), character);
        cout << "Your sentence had" << cCount << character 
             << "character(s)"; 
     }

cout << "Do you wish to enter another sentence (y/n)?: ";
cin >> answer;
if (answer == 'n'){
    break;
    }
}
return 0;
}

int WordOccurrenceCount( string const & str, string const & word )
{
   int count;
   string::size_type word_pos( 0 );
   while ( word_pos!=string::npos )
   {
           word_pos = str.find(word, word_pos );
           if ( word_pos != string::npos )
           {
                   ++count;

     // start next search after this word 
                   word_pos += word.length();
           }
   }

   return count;

有人可以帮忙吗?

【问题讨论】:

  • 您的标题和问题显示“count chars”,但您的代码看起来像是在计算 words
  • 您的意思是某些文本中“字符串”的数量?或字符串中的字符数??
  • @ArneMertz 您发布的线程与 Java 而非 C++ 相关
  • @stellarossa 刚刚注意到,谢谢。所以这里有其他人:stackoverflow.com/questions/7363257/…stackoverflow.com/questions/8870263/…

标签: c++ string character


【解决方案1】:

没有空字符。

随便写

if (sentence == "")
{
        cout << "Please enter a valid answer:\n";
        break;
}

【讨论】:

    【解决方案2】:

    此代码的问题: 1. C++ 不接受空字符:if(character == '') 2. 函数WordOccurrenceCount 的参数与您的声明不匹配。 3、sentence.begin()是String_iterator类型,不能转成字符串。 (正如您的 WordOccurrenceCount 函数所期望的那样) 4. 同样,sentence.end 也是 String_iterator 类型,不能转换为 int(如您的函数声明所期望的那样)或 string(如您的函数定义所期望的那样)。

    【讨论】:

    • 当您将 char 作为输入时,用户从不输入任何内容(我相信这是 if 语句的原因)。即使用户没有输入任何内容并按下回车,他仍然按下回车,这是一个字符。尝试检查用户给出的字符是否为 '\n'。
    【解决方案3】:

    计数后(请在将来以某种方式标记错误的行)其中一个问题是这一行:

    if(character == '' || sentence == "" )
    

    在 C++(和 C)中,不能有空字符。

    当您阅读character 并且没有输入任何内容时,您会得到换行符,因此第一个检查应该是character == '\n'

    关于字符串,有一个非常简单的方法来检查字符串是否为空:std::string::empty:

    sentence.empty()
    

    所以完整的条件应该是

    if (character == '\n' || sentence.empty()) { ... }
    

    至于其他错误,确实有多个错误:首先声明WordOccurenceCount 接受两个参数,一个字符串和一个整数。然后你用三个参数调用它,没有一个是正确的类型。

    那么在WordOccurenceCount 的定义中,与声明相比,你有不同的参数。


    最后,如果您想计算某个字符在字符串中出现的次数,那么您可能需要查看 C++ 中可用的standard algorithms,尤其是std::count

    std::string sentence;
    std::cout << "Enter a sentence: ";
    std::getline(std::cin, sentence);
    
    char character;
    std::cout << "Enter a character to be found: ";
    std::cin >> character;
    
    long count = std::count(std::begin(sentence), std::end(sentence), character);
    

    【讨论】:

    • @user2939276 更新答案
    猜你喜欢
    • 2017-04-19
    • 2019-09-14
    • 2023-04-06
    • 1970-01-01
    • 2020-08-13
    • 1970-01-01
    • 1970-01-01
    • 2023-03-28
    相关资源
    最近更新 更多