C Program to Check Palindrome string.

 

 C PROGRAM TO CHECK PALINDROME (string) 



Palindrome: 

A palindrome is a word, phrase, or sentence that reads the same in both backward and forward directions for example "level". In other words, string is said to be a palindrome if the string read from left to right is equal to the string read from right to left.



#include <stdio.h>

#include <string.h>

int main() { 

    int i, l, rev = 0;

    char string[250];

    printf("Enter a string: ");

   scanf("%s", string);

    l = strlen(string);

    for( i = 0; i < l; i++) {

        if(string[i] != string[l-i-1]){

           rev = 1;

            break;

        }

  }

if(rev == 1){

      printf("Entered string is not a Palindrome.");


}

else{

     printf("Entered string is a Palindrome.");

}

    return 0;


}

Output

Enter a string: solos

Entered string is a Palindrome.






How the program works?


  • The program takes input from the scanf() function.
  • Then using strlen() function length of the entered string is stored in ' l '.
  • Then in for loop, the first character (i=0) is compared with the last character (i=l-i-1) if it matches it check the same with the second and second-last character this continues until for loop get executed. If all things go well it returns the rev value unchanged and the else part is get executed giving output "Entered string is a Palindrome".
  • If at any point in for loop the respective characters do not match, the rev becomes equal to 1, the break statement is called and the loop gets closed, and the 'if' statement gets executed giving output as "Entered string is not a Palindrome.

// This program is case sensitive i.e., for "solos" it will show it as a palindrome but for "Solos" it will show as not a palindrome.


You can try this program out on the above linkhttp://bit.ly/3p9m63A






Comments

Popular posts from this blog

ATM solution in C language.- Codechef

MOTIVATION SOLUTION IN C LANGUAGE | CODECHEF | IMDB

APPLE AND ORANGE | HACKERRANK | PROBLEM SOLVING | SOLUTION IN C