代码之家  ›  专栏  ›  技术社区  ›  Harsh Joshi

我想检查字符串是否为回文[闭合]

  •  -3
  • Harsh Joshi  · 技术社区  · 8 年前
    char s[100];
    //char t[100];
    int count = 1;
    int j=0;
    int x,i;
    cin >>s;
    x=strlen(s);
    //cout <<x <<endl;
    cout <<s[j] <<endl;
    i=x-1;
    cout <<s[i] <<endl;
    for (int i = x-1; i <= 0; i--)
    {
        if (s[j] != s[i])
        {
            count = 0;
        }
        j++;
    
    }
    if ( count  )
    {
        cout <<"YES";
    }
    else
    {
        cout <<"NO";
    }
    return 0;
    

    1 回复  |  直到 8 年前
        1
  •  0
  •   warl0ck    8 年前

    你的代码从来没有输入过 for loop 自条件 i = x-1 and i <=0 不会是真的很好 assuming your string is not empty

    #include <iostream>
    
    using namespace std;
    
    int main() {
        char s[100];
        int x,i,j=0;
        cin >>s;
        x=strlen(s);
        i = x-1;
        cout <<s[0] <<endl;
        cout <<s[i] <<endl;
        for (int i = x-1; i >= 0; i--)
        {
            if (s[j] != s[i])
            {
                cout <<"NO";
                return 0;
            }
            j ++;
        }
        cout <<"YES";
        return 0;
    }