Write a C program that accepts a string from the user.The program is to determine if entered string is a palindrome or not

Dani AI

Generated

The OP asked for a C program to accept a string and determine whether it is a palindrome. The thread has a few quick replies and a code snippet from that uses several nonstandard or unsafe elements; correctly noted that such code may not compile on modern toolchains. A small, portable plan is safer and clearer:

  • Read input with fgets (avoid gets).
  • Remove the trailing newline.
  • Build a normalized buffer that keeps only alphanumeric characters and converts them to lowercase (so "Madam, I'm Adam" becomes "madamimadam").
  • Compare characters with a two-pointer loop from both ends.
  • Use int main(void) and standard headers only; avoid clrscr, getch, strrev, and other nonstandard functions.

A compact, safe example follows (C99/C11 compatible):

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

int main(void)
{
    char line[1024];
    if (!fgets(line, sizeof line, stdin)) return 1;

    size_t n = strlen(line);
    if (n && line[n-1] == '\n') line[n-1] = '\0';

    char norm[1024];
    size_t j = 0;
    for (size_t i = 0; line[i]; ++i) {
        unsigned char ch = (unsigned char)line[i];
        if (isalnum(ch)) norm[j++] = (char)tolower(ch);
    }
    if (j == 0) { puts("Empty (no alphanumeric chars)"); return 0; }

    size_t a = 0, b = j - 1;
    int is_pal = 1;
    while (a < b) {
        if (norm[a++] != norm[b--]) { is_pal = 0; break; }
    }
    puts(is_pal ? "Palindrome" : "Not palindrome");
    return 0;
}

Notes: always cast to unsigned char before isalnum/tolower to avoid UB on negative-valued char. The example does not handle UTF-8 multibyte graphemes; for Unicode-aware palindrome checks use a proper Unicode library (e.g., ICU) and operate on code points. Compile with warnings enabled (for example, gcc -std=c11 -Wall -Wextra) and test edge cases (empty input, long lines, punctuation). This addresses portability and safety issues raised in the thread and gives a practical, modern solution.

Recommended Answers

All 6 Replies

No thanks.

lol

OK, done. Now what?

commented: Haha +11

Write a C program that accepts a string from the user.The program is to determine if entered string is a palindrome or not

Why ?

#include<stdio.h>
#include<string.h>
#define size 25

void main()
{
char strsrc;
char strtmp;

clrscr();
printf("\n Enter String:= "); gets(strsrc);

strcpy(strtmp,strsrc);
strrev(strtmp);

if(strcmp(strsrc,strtmp)==0)
printf("\n Entered string \"%s\" is palindrome",strsrc);
else
printf("\n Entered string \"%s\" is not
palindrome",strsrc);
getch();
}

commented: We don't give code. We help them write their own. -3
commented: And more importantly, don't give crappy code.. -1

And now try compiling this on something that isn't outdated and non-standard and see if it compiles....

Furthermore, even dribble like this should be placed in code tags. Learn how to use them.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.