C ++计数字符串中的字符 [英] C++ count chars in a string

查看:141
本文介绍了C ++计数字符串中的字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要计算输入句子中输入字符的数量。我很接近,但我一直得到这个错误:

I need to count the number of an input character there is in an input sentence. I am so close however I keep getting this error:

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;

任何人都可以借一只手吗?

Can anyone lend a hand?

推荐答案

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

After counting (please mark erroneous lines somehow in the future) one of the problems was this line:

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

(和C),您不能有空字符文字。

In C++ (and C) you can't have empty character literals.

当您读取字符你得到换行符,所以第一个检查应该 character =='\\\
'

When you read the character and nothing is entered you get the newline, so the first check should be character == '\n'.

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

As for the string, there is a very simple method of checking if a string is empty: std::string::empty:

sentence.empty()

因此,完整条件应为

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






至于其他错误,实际上有多个错误: c $ c> WordOccurenceCount 接受两个参数,一个字符串和一个整数。


As for the other errors, there are really multiple errors: To start with you declare WordOccurenceCount to take two arguments, a string and an integer. You then call it with three arguments, none of which are of the correct type.

然后在 WordOccurenceCount 与声明相比,您有不同的参数。

Then in the definition of WordOccurenceCount you have different arguments compared to the declaration.

最后,如果要计算某个字符的时间是在字符串中,那么您可能需要查看C ++中提供的标准算法,特别是 std :: count

Finally, if you want to count the number of time a certain character is in a string, then you might want to look at the standard algorithms available in C++, especially 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);

这篇关于C ++计数字符串中的字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆