String Palindrome

 String Palindrome 

PROBLEM STATEMENT :

A string S is passed as input to the program. The program must print "yes" if the string S is a palindrome and must print "no" if the string is NOT a palindrome. 

Boundary Condition(s): 

1<=length(S)<=10000

Input Format: 

The first line will contain the value of the string S. 

Output Format:

The first line in the output should contain "yes" or "no" based on the input.

Example Input/Output 1: 
Input: () 
malayalam
Output: yes

Example Input/Output 2: 
Input: () 
manager
Output: no



SOLUTION :

C (Programming Language)


      

#include<stdio.h> #include<stdlib.h> int main() { char s[1001],r[1001],t; int l,i=0; scanf("%s%n",s,&l); strcpy(r,s); l-=1; while(i<l) { t=s[i]; s[i]=s[l]; s[l]=t; i++; l--; } if(strcmp(s,r)==0) printf("yes"); else printf("no"); }



C++ (CPP)

       

#include <iostream> #include<algorithm> //For reverse function using namespace std; int main(int argc, char** argv) { string s,r; cin>>s; r=s; reverse(s.begin(),s.end()); if(s==r) cout<<"yes"; else cout<<"no"; }



JAVA

       

import java.util.*; public class Hello { public static void main(String[] args) { Scanner sc=new Scanner(System.in); String s=sc.next(); String rev=new StringBuilder(s).reverse().toString(); if(s.compareTo(rev)==0) System.out.println("yes"); else System.out.println("no"); } }



PYTHON

       

s=input() rev=s[::-1] print("yes" if s==rev else "no")



Never Stop Learning !!


Comments

Popular posts from this blog

DP (1) - Count number of ways to cover a distance

Zero Insert After K Times One

Left Number Twice Right