to find if a given string is palindrome or is not palindrome

前端 未结 9 577
悲哀的现实
悲哀的现实 2020-11-30 15:36

I made a program to find if a entered string is palindrome or not palindrome but it always says that its not a palindrome

#include  
#include          


        
9条回答
  •  南笙
    南笙 (楼主)
    2020-11-30 15:57

    Here you want something like...

        if(str[i]!=str[strlen (str) - i - 1])
        {
            flag = 0;
            break;
        }
    

    The break needs to go in the if block otherwise it will always get executed. Initialising flag at some point would be a good idea, too. If I might be permitted an observation, ALWAYS enclose the if-block and else block in curly brackets even if there is only one statement; it would save you several of the problems you've got here.

    Later - edited per Mr Rodriguez' comments below.

提交回复
热议问题