Develop a C program to check whether the given string is palindrome or not
Code
using string functions
#include <stdio.h>#include <string.h>int main(){char inputArray[100], reversedArray[100];printf("Enter the string for palindrome check \n");scanf("%s", inputArray);/* Copy input string and reverse it*/strcpy(reversedArray, inputArray);/* reverse string */strrev(reversedArray);/* Compare reversed string with inpit string */if(strcmp(inputArray, reversedArray) == 0 )printf("%s is a palindrome.\n", inputArray);elseprintf("%s is not a palindrome.\n", inputArray);return 0;}
without using string functions
#include <stdio.h>#include <string.h>int main(){char inputString[100];int leftIndex, rightIndex, length = 0;printf("Enter a string for palindrome check\n");scanf("%s", inputString);/* Find length of input string */while(inputString[length] != '\0')length++;/* If length of string is less than 1, ERROR */if(length < 1)return 1;/* Initialize leftIndex and rightIndex to first andlast character of input string */leftIndex = 0;rightIndex = length -1;/* Compare left and right characters, If equal thencontinue otherwise not a palindrome */while(leftIndex < rightIndex){if(inputString[leftIndex] != inputString[rightIndex]){printf("%s is not a Palindrome \n", inputString);return 0;}leftIndex++;rightIndex--;}printf("%s is a Palindrome \n", inputString);return 0;}
Output
0 Comments
Post a Comment