首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用strlen错误

使用strlen错误
EN

Stack Overflow用户
提问于 2014-11-14 01:30:50
回答 1查看 2.3K关注 0票数 0

我一直在查找关于如何使strlen工作的例子和教程,但是没有什么是有效的。我正在做一个小程序,让你键入你的句子,你可以搜索一个特定的字母在句子中。

该错误如下:

19:23: error: cannot convert âstd::string {aka std::basic_string<char>}â to âconst char*â for argument â1â to âsize_t strlen(const char*)â

代码语言:javascript
复制
#include <iostream>
#include <string>
#include <cstring>

using namespace std;

int main() {
    char letter;
    string sentence;
    int count;

    cout << "Enter a character to count the number of times it is in a sentence: ";
    cin >> letter;

    cout << "Enter a sentence and to search for a specified character: " << endl;
    getline(cin, sentence);

    for(int i = 0; i < strlen(sentence); i++){
            if(sentence[i] == letter){
                    count++;
            }
    }
    cout << letter << " was found " << count << " times." << endl;
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-11-14 01:33:47

因为sentencestd::string,所以您应该使用sentence.length()strlen(sentence.c_str())

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26921530

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档