C 比较字符串的字符

C++ comparing a character of a string

本文关键字:字符 字符串 比较      更新时间:2023-10-16

好的,因此输入是字符串。当我尝试编译以下代码时,我会得到

c.cpp:42:10: error: ISO C++ forbids comparison between pointer and integer [-fpermissive]

为什么?

if(input[i] != ' ')
{   
    char s = input[i];
    if(s == "+")
    {
         ...
    }
}

在此语句中使用单引号

if(s == "+")

在这里

if(s == '+')

作为char s是一个字符,因此只能将其与其他字符或ASCII值进行比较。双引号(" ")用于字符串,而字符(' ')则用于字符。