Program to check String is Palindrome

Description

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
A string is called palindrome if reverse of the string is same as string.
To check string is palindrome traverse string from beginning and compare
character of string from ith position to corresponding (length-i-1)th position.
if characters are same then string will be palindrome.
A string is called palindrome if reverse of the string is same as string. To check string is palindrome traverse string from beginning and compare character of string from ith position to corresponding (length-i-1)th position. if characters are same then string will be palindrome.
A string is called palindrome if reverse of the string is same as string. 
To check string is palindrome traverse string from beginning and compare
character of string from ith position to corresponding (length-i-1)th position.
if characters are same then string will be palindrome.

C/C++

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
/* C Program to check Palindrome */
//Save it as PalindromeString.c
#include<stdio.h>
int main(){
char str[100];
int i, length, flag=0;
printf("Enter a string : ");
gets(str);
length = strlen(str);
for(i=0;i<length;i++){
if(str[i] != str[length-i-1]){
flag = 1;
break;
}
}
if(flag==1){
printf("%s is not palindrome",str);
}else{
printf("%s is palindrome",str);
}
return 0;
}
/* C Program to check Palindrome */ //Save it as PalindromeString.c #include<stdio.h> int main(){ char str[100]; int i, length, flag=0; printf("Enter a string : "); gets(str); length = strlen(str); for(i=0;i<length;i++){ if(str[i] != str[length-i-1]){ flag = 1; break; } } if(flag==1){ printf("%s is not palindrome",str); }else{ printf("%s is palindrome",str); } return 0; }
/* C Program to check Palindrome */
//Save it as PalindromeString.c

#include<stdio.h>
int main(){

    char str[100];

    int i, length, flag=0;

    printf("Enter a string : ");
    gets(str);

    length = strlen(str);

    for(i=0;i<length;i++){
        if(str[i] != str[length-i-1]){
            flag = 1;
            break;
        }
    }

    if(flag==1){
        printf("%s is not palindrome",str);
    }else{
        printf("%s is palindrome",str);
    }

    return 0;
}
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
Input:
Enter a string : madam
Output:
madam is palindrome
Input: Enter a string : madam Output: madam is palindrome
Input:
Enter a string : madam

Output:
madam is palindrome

Java

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
/* Java Program to check Palindrome */
//Save it as PalindromeString.java
import java.io.*;
import java.util.Scanner;
public class PalindromeString {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String str;
int i, length, flag=0;
System.out.println("Enter a string : ");
str = scanner.nextLine();
length = str.length();
for(i=0;i<length;i++){
if(str.charAt(i) != str.charAt(length-i-1)){
flag = 1;
break;
}
}
if(flag==1){
System.out.println(str + " is not palindrome");
}else{
System.out.println(str+" is palindrome");
}
}
}
/* Java Program to check Palindrome */ //Save it as PalindromeString.java import java.io.*; import java.util.Scanner; public class PalindromeString { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); String str; int i, length, flag=0; System.out.println("Enter a string : "); str = scanner.nextLine(); length = str.length(); for(i=0;i<length;i++){ if(str.charAt(i) != str.charAt(length-i-1)){ flag = 1; break; } } if(flag==1){ System.out.println(str + " is not palindrome"); }else{ System.out.println(str+" is palindrome"); } } }
/* Java Program to check Palindrome */
//Save it as PalindromeString.java

import java.io.*;
import java.util.Scanner;

public class PalindromeString {

    public static void main(String[] args) {
        
            Scanner scanner = new Scanner(System.in);

            String str;

        int i, length, flag=0;

        System.out.println("Enter a string : ");
        str = scanner.nextLine();

        length = str.length();

        for(i=0;i<length;i++){
            if(str.charAt(i) != str.charAt(length-i-1)){
                flag = 1;
                break;
            }
        }

        if(flag==1){
            System.out.println(str + " is not palindrome");
        }else{
            System.out.println(str+" is palindrome");
        }
    }
}
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
Input:
Enter a string :
madam
Output:
madam is palindrome
Input: Enter a string : madam Output: madam is palindrome
Input:
Enter a string : 
madam

Output:
madam is palindrome

Related Programs

1) Program to find largest and smallest character in a String
2) Program to swap two strings
3) Program to Check Whether a Character is a Vowel or Not
4) Program to Find ASCII value of a character
5) Program To Print Unique Word Of A String Using Set
6) Print All Unique Words Of A String
7) Find Occurrence Of Each Word In a Sentence
8) Count Number of Words in a String
9) Program to Sort set of strings in alphabetical order
10) check strings are equal or not after concatenation of array of strings with other array of strings
Share Me

Leave a Reply