小编Rhe*_*ett的帖子

c ++在比较字符串时忽略前置空格,例如:"str1"compare.("str2")= TRUE

嗨,我想知道什么是使字符串str1看起来等于字符串str2的最短方法

str1 = "Front Space";
str2 = " Front Space";

/*Here is where I'm missing some code to make the strings equal*/

if (str1.compare(str2) == 0) { // They match 
    cout << "success!!!!" << endl; // This is the output I want
}
Run Code Online (Sandbox Code Playgroud)

所有我需要它str1等于str2我该怎么办?

我做了多次尝试,但它们似乎都没有正常工作.我认为这是因为字符串中的字符数,即:str1的字符数少于str2.

for (int i = 1; i <= str1.length() + 1; i++){
    str1[str1.length() - i ] = str1[str1.length() - (i + 1)];
}
Run Code Online (Sandbox Code Playgroud)

任何帮助赞赏

c++ string compare char

1
推荐指数
1
解决办法
3621
查看次数

标签 统计

c++ ×1

char ×1

compare ×1

string ×1