1. Introduction
A palindrome is a word, phrase, number, or another sequence of characters that reads the same forward as backward (ignoring spaces, punctuation, and capitalization). The word "radar" and the phrase "A man, a plan, a canal, Panama!" are famous examples of palindromes.
In this tutorial, we will guide you through a C program to determine if a given string is a palindrome without using library functions.
2. Program Overview
The program will:
1. Take a string input from the user.
2. Check if the string is a palindrome.
3. Display the result accordingly.
3. Code Program
#include <stdio.h> // Include standard I/O library for input-output operations.
int main() { // Begin main function.
char str[100], temp[100]; // Define arrays to store the string and its copy.
int start = 0, end, length = 0; // Define start and end indices and length variable for the string.
printf("Enter a string: "); // Prompt user for input.
scanf("%s", str); // Store user input in 'str' array.
// Find the length of the string.
for (end = 0; str[end] != '\0'; end++) {
length++;
}
end = length - 1; // Set 'end' to the last character of the string.
// Reverse the string into 'temp'.
for (int i = 0; i < length; i++) {
temp[i] = str[end];
end--;
}
temp[length] = '\0'; // Append null character to the end of 'temp'.
// Check if 'str' and 'temp' are the same.
for (int i = 0; i < length; i++) {
if (str[i] != temp[i]) {
printf("The string is not a palindrome.\n");
return 0; // Exit the program if the strings are different.
}
}
printf("The string is a palindrome.\n"); // Display the result.
return 0; // End the program.
}
Output:
Enter a string: radar The string is a palindrome.
4. Step By Step Explanation
1. Header Inclusion: The standard input-output library #include <stdio.h> provides us with essential functions like printf and scanf.
2. Main Function: Every C program execution starts from the main() function.
3. Variable Declaration:
- str and temp are character arrays to store the original string and its reversed copy, respectively.
- start, end, and length are integer variables to keep track of positions and string length.
4. Collecting Input: We use printf to prompt the user for input and scanf to read the string into str.
5. Calculating String Length: A for loop calculates the string length, and the end variable points to the last character in the string.
6. Reversal Process:
- Another for loop is used to reverse the string from str into temp.
- The null character is then added at the end of temp to ensure it`s a valid string.
7. Palindrome Check:
- A loop compares each character of str with the corresponding character of temp.
- If theres a mismatch, the program prints that the string isnt a palindrome and exits.
- If the loop completes without finding a mismatch, the program concludes that the string is a palindrome.
8. Displaying Result: The result, whether the string is a palindrome or not, is displayed using printf.
This method, though not the most optimized, offers clarity and helps beginners grasp the fundamental logic behind palindrome checking.
Comments
Post a Comment
Leave Comment