Join Regular Classroom : Visit ClassroomTech

Accenture Coding Question | String Palindrome | CodeWindow

String Palindrome

Write a function to find if the given string string is palindrome or not. Return 1 if the input string is a palindrome, else return 0.

Input specification:
Input 1: A string of characters.

Output Specification:
0 or 1 depending on whether the string is a palindrome or not.

Example 1:
Input 1: level

Output: 1
Explanation:
The reverse of string ‘level’ is ‘level’. As they are same hence the string is a palindrome.

Example 2:
Input 1: abcd

Output: 0
Explanation:
The reverse of string ‘abcd’ is ‘dcba’. As they are not the same hence the string is not a palindrome.

Solution in Python 3:

#https://codewindow.in
# Join our Telegram Channel

s=input()

s_rev=list(reversed(s)) #returns a list of the reversed string

revStr=''.join(s_rev) #joins the elements of the reversed list to a string

#print(revStr)

if s==revStr:
    print('1')
else:
    print('0')
    
# Telegram @codewindow

Solution in C :

//https://codewindow.in
//join our telegram channel @codewindow

#include <stdio.h>
#include <string.h>

int main()
{
    char a[100];
    scanf("%s",a);
    int flag = 1;
    for(int i=0;i<=strlen(a)/2+1;i++)
        if(a[i] != a[strlen(a)-i-1]) {
            flag = 0;
            break;
        }
    printf("%d",flag);
    return 0;
}

Solution in C++ :

//https://codewindow.in
//join our telegram channel @codewindow

#include <stdio.h>
#include <string.h>

int main()
{
    char a[100];
    scanf("%s",a);
    int flag = 1;
    for(int i=0;i<=strlen(a)/2+1;i++)
        if(a[i] != a[strlen(a)-i-1]) {
            flag = 0;
            break;
        }
    printf("%d",flag);
    return 0;
}

Solution in JAVA :

//https://codewindow.in
//join our telegram channel @codewindow

import java.io.*;
import java.lang.*;
import java.util.*;

class CodeWindow {
    public static void main(String[] args) throws Exception
    {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String str = br.readLine();
        int flag = 1;
        for(int i=0;i<=str.length()/2+1;i++)
            if(str.charAt(i) != str.charAt(str.length()-i-1)) {
                flag = 0;
                break;
            }
        System.out.println(flag);
    }
}

Output:

level
1
abcd
0
Recent Posts
Pages