Write a program to check whether a given string is palindrome or not.
Example:
Input: str[]={'H','e','e','l','e','e','H'} Output: String is palindrome
Approach
C
#include <stdio.h>int main(){char str[] = {'H', 'e', 'e', 'l', 'e', 'e', 'H'};int n = sizeof(str) / sizeof(str[0]);int flag = 0;for (int i = 0; i < n / 2; i++){if (str[i] != str[n - 1 - i]){flag = 1;break;}}if (flag == 0)printf("String is palindrome ");elseprintf("String is not palindrome ");return 0;}
Java
public class CheckStringPalindrom {public static void main(String[] args) {String str = "HeeleeH";int flag = 0;int n = str.length();for (int i = 0; i < n / 2; i++) {if (str.charAt(i) != str.charAt(n - 1 - i)) {flag = 1;break;}}if (flag == 0) {System.out.println("String is palindrome ");} else {System.out.println("String is not palindrome ");}}}
No comments:
Post a Comment