c++ - 错误 : Expression must be a modifiable lvalue using strings

标签 c++ visual-studio

我正在尝试创建一个函数来计算特定字符在字符串中的使用次数。给我错误的部分是:

s[i] = tolower(s[i]);

代码:

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

unsigned howMany(char c, const string & s);
string changeCase(const string & s);

int main() {
    char z;
    string name;
    cout << "Enter char and string: ";
    cin >> z;
    getline(cin, name, '\n');
    cout << howMany(z, name) << endl;
    cout << changeCase(name) << endl;
}

unsigned howMany(char c, const string & s) {
    unsigned count = 0;
    for (unsigned i = 0; i < s.size(); i++)
    {
        if (c == s[i]) { count++; }

            if (isupper(s[i])){ s[i] = tolower(s[i]);
            count++; 
        }
    }
    return count;
}

最佳答案

您将 s 声明为 const。你无法改变它。

关于c++ - 错误 : Expression must be a modifiable lvalue using strings,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34215855/

相关文章:

c++ - ASIO 中存在单独的接受器类背后的设计原理

c++ - 基于指针的查询

c# - 在C#类中调用List<string>[] Select(string query)

visual-studio - 需要来自 Visual Studio Team Service 的 IP 范围才能在 AWS 上创建 SG

c++ - Visual Studio : "str() is not a member of std::ostringstream"

c++ - 将 char[] 转换为 off_t

c++ - 如何在 C++ 中通过引用调用一个类的变量

c++ - 通过引用将数组从 Delphi 7 传递到 C++ Dll

c# - 获取 .Net XML 文档以返回字典的特定类型

node.js - 如何在 CentOS 6.6 vps 上部署使用 Visual Studio 开发的 Node JS 应用程序