Palindrome String C Program Step-by-Step Guide [ 2024 ]
Palindrome String C Program
In this tutorial, we’ll walk you through the steps to create a C program that checks if a given string is a palindrome using Code::Blocks.
Palindrome String
A palindrome string is a sequence of characters that reads the same forward and backward, ignoring spaces, punctuation, and capitalization.
Examples
For example, the following strings are palindrome strings
aba
madam
racecar
The following strings are NOT palindrome strings:
abcd
testingdocs
Prerequisites
Before we begin, ensure you have the following:
CodeBlocks IDE installed on your machine
Basic understanding of C programming.
Step-by-Step Guide
Step: Create a New project.
Step: Launch CodeBlocks IDE.
Step: Create a New File. for example, palindrome.c
Step: Write the palindrome C program code.
Palindrome String C Program
Code your C program in the editor.
/*
* C Program to check palindrome string.
* C Tutorials - www.TestingDocs.com
*/
#include <stdio.h>
#include <string.h>
int main() {
char str[100], reversedStr[100];
int length, i, j;
printf("Enter a string: ");
gets(str);
length = strlen(str);
// Reverse the string
for(i = length - 1, j = 0; i >= 0; i--, j++) {
reversedStr[j] = str[i];
}
reversedStr[j] = '\0'; // Null-terminate the reversed string
// Check if the original string is equal to the reversed string
if(strcmp(str, reversedStr) == 0) {
printf("%s is a palindrome.\n", str);
} else {
printf("%s is not a palindrome.\n", str);
}
return 0;
}
Program Screenshot
Build and Run
Step: Build and Run the Program
Build the Program
Click on the Build gear icon or
Go to Build > Build to compile the code.
Run the Program
Click on the Run icon (Play button) or
Go to Build > Run.
Test Your Program
Step: Run Test cases on the program to verify the program output. After running the program, a console window will appear.
Enter a string and press Enter.
The program will output whether the string is a palindrome or not.
Code Explanation
Let’s break down the code to understand what each part does:
Include Libraries:
The #include <stdio.h> and #include <string.h> lines include standard libraries for input/output and string manipulation.
Main Function: The int main() function is the program’s entry point.
String Input: gets(str) takes a string input from the user.
String Length: length = strlen(str) calculates the length of the string.
Reverse the String: A for loop reverses the input string and stores it in reversedStr.
Null-Terminate: reversedStr[j] = ‘\0’ adds a null character at the end of the reversed string.
Compare Strings: strcmp(str, reversedStr) compares the original and reversed strings.
Output Result: Based on the comparison, the program prints whether the input string is a palindrome.
That’s it. You’ve successfully written a C program to check for palindrome strings using CodeBlocks IDE. This exercise helps solidify your understanding of string manipulation and basic programming constructs. Keep practicing and exploring more programs to enhance your coding skills.
Code::Blocks Tutorials
Code::Blocks Tutorials on this website can be found at:
For more information on Code::Blocks IDE, visit the official website: