This program tests whether a string is a palindrome or not. Can there be any improvements or shortcuts that can be used in the program. You can give any type of string as input. The program finds does it work.

#include <stdio.h>
#include <string.h>

A palindrome is a string that is same in both forward and backward reading.
   "a man a plan a canal panama"
You will write a program that will test if a given string is a palingdrome or not.
Your program will ask the user to input a string and if the string is a palindrome program
will just print "Yes, it is a Palindrome", otherwise will print "No, not a Palindrome".

Please note that:
1. Your you need to check in case-insensitive way, that means: Madam or madam both should be
detected as Palindrome.

2. There can be (any number of ) spaces in between the words.
    "A man a plan a canal panama"
    "A     man    a   pla n a cana l Panama"
    both the strings must be detected as Palindrome.
3.There can be punctuations in between the words, for this assignments,
we consider only 4 punctuations,   . ?  ! and ,

Your program will just need to ignore them (treat them as space).
    "Cigar? Toss it in a can. It is so tragic."
    Should be detected as palindrome.

 *** For this assignment I will not write any instructions or guidance, you are free
        to implement it with your own way, you can use the string.h functions

    Good luck.



    Created by Shaik Mohammed Zeeshan
    Date - 19 Aug 2018
    Objective - Checks whether a given string is palindrome or not


int main()
    char string[100];
    char string1[100];//the string without spaces and special characters will be stored in this

    printf("Enter a string: ");
    scanf("%[^\n]", string);

    int isPalindrome = 1;  // assign 0 to this if the string is a NOT palindrome

    // write code to test if string is a palindrome

    int index;
    int index1;//index for the second array

    for(index=0,index1=0;string[index] != '\0'; index++)  //to eliminate spaces, special characters and capital letters from the string
        if( (string[index] < 'A' || string[index] > 'Z' ) && (string[index] < 'a' || string[index] > 'z' ) ) // checks if the element is a special   
            continue;                                                                              // character or space and skips the iteration

        if(string[index] >= 'A' && string[index] <= 'Z' )
            string[index] += 32;

        string1[index1] = string[index];
    string1[index1] = '\0'; // assigning the last element with null character

    int i,stringlength = strlen(string1); // storing length after eliminating unecessary characters 

    for(i=0;i<=stringlength/2;i++) //starts the loop from first element to the middle element
        if(string1[i] != string1[stringlength-i-1]) //checks if the elements are true 
            isPalindrome = 0;   // checks the first  element with the first element from the last and second element with the penultimate element and so on

    // at the end you need to test
    if (isPalindrome)
        printf("Yes, it is Palindrome!\n");
        printf("No, not a Palindrome\n");

    return 0;

The amount of comments you use is more distracting than helpful. Good comments are those that explain what the overall logic of a block of code is, they don’t explain each line of code. Code is supposed to be self-documenting, that is, the code should explain itself. You accomplish this by using good variable names and good function names, which explain their own meaning.

Also, you need to use the functionality in the standard library. For example, to test whether a character is a normal letter or not, use isalpha rather than the four comparisons you’re using (against 'a', 'z', etc.)

  • \$\begingroup\$ I am still learning C language and this was an assignment. I did not know the isalpha function. Thanks for telling. \$\endgroup\$ – Shaik Mohammed Zeeshan Aug 20 '18 at 12:56

there are a LOT of problems in the posted code. Here is one of them:

scanf("%[^\n]", string);

There is no limit on the number of characters that the user can input. So the user can cause a buffer overflow, resulting in undefined behavior and possibly an abort of the code.

Suggest using:

scanf("%99[^\n]", string);

As that limits the total number of characters the user can input to 1 less than the length of the input buffer. It needs to be 1 less than the length of the input buffer because the %[...] and %s input format specifiers always append a NUL byte to the input

in the header file: ctypes.h are 'functions' tolower(), toupper(), isalpha(), etc etc. Strongly suggest you make use of them

  • \$\begingroup\$ I am still learning C language and this was an assignment. Our instructor told us that we can make use of only string.h. Thanks for telling the function names. \$\endgroup\$ – Shaik Mohammed Zeeshan Aug 20 '18 at 12:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.