The Program in C to check whether string inputted by user is palindrome or not is given below:
#include <stdio.h>
#include <string.h>
int main() {
char str[100];
int i, len, flag = 0;
printf("Enter the string: ");
scanf("%s", str);
len = strlen(str); //getting the length of the input string
for (i = 0; i < len/2; i++) {
if (str[i] != str[len-i-1]) { //checking if the characters at the beginning and end are not equal
flag = 1; //set flag to 1 if not equal
break;
}
}
if (flag == 0) {
printf("The string is a palindrome.");
} else {
printf("The string is not a palindrome.");
}
return 0;
}
Output:
Enter the string: kik
The string is a palindrome.
Pro-Tips💡
This program first declares a character array (string) of size 100 for storing the input string.
Then it uses the scanf() function to prompt the user to enter the string, which is stored in the str array.
The strlen() function is used to get the length of the input string.
Then, the program uses a for loop to check if the characters at the beginning and end of the string are equal or not.
If the characters are not equal, the flag variable is set to 1, and the loop breaks.
After the loop, the program checks the value of the flag variable. If it’s 0, the string is a palindrome, otherwise, it’s not.
Finally, the program uses the printf() function to print out whether the string is a palindrome or not.
Learn C-Sharp ↗
C-sharp covers every topic to learn about C-Sharp thoroughly.
Learn C Programming ↗
C-Programming covers every topic to learn about C-Sharp thoroughly.
Learn C++ Programming↗
C++ covers every topic to learn about C-Sharp thoroughly.