C Examples

WAP in C to check whether string inputted by user is palindrome or not

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.

Codeauri is Code Learning Hub and Community for every Coder to learn Coding by navigating Structurally from Basic Programming to Front-End Development, Back-End Development to Database, and many more.

Related Posts

C# Program to Find Sum of Rows & Columns of a Matrix

The Program in C# Program to Find Sum of Rows & Columns of a Matrix is given below: Output: Hello Codeauri Family,enter the number of rows and columns…

C# Program to Calculate Determinant of Given Matrix

The Program in C# Program to Calculate Determinant of Given Matrix is given below: Output: Hello Codeauri Family, enter the number of rows and columns of the matrix…

C# Program to Find Sum of right Diagonals of a Matrix

The Program in C# Program to Find Sum of right Diagonals of a Matrix is given below: Output: Hello Codeauri Family, enter the number of rows and columns…

C# Program to Find Transpose of Given Matrix

The Program in C# Program to Find Transpose of Given Matrix is given below: Output: Hello Codeauri Family, enter the number of rows and columns in the matrix:22Enter…

C# Program for Multiplication of two square Matrices

The Program in C# Program for Multiplication of two square Matrices is given below: Output: Hello Codeauri Family, enter the number of rows/columns in the matrices:2Enter the elements…

C# Program to Delete Element at Desired position From Array

The Program in C# Program to Delete Element at Desired position From Array is given below: Output: Hello Codeauri Family, enter the number of elements in the array:4Enter…

Leave a Reply

Your email address will not be published. Required fields are marked *

Your Journey into Code Begins Now: Discover the Wonders of Basic Programming

X