ZeroToSky
C Programming Tutorial 97 of 206
Introduction & Setup
  • Introduction to C
  • History of C
  • Features of C
  • Applications of C
  • Advantages and Limitations of C
  • C Standards and Implementations
  • How C Programs Run
  • Setting Up C Development Environment
  • Your First C Program
  • Basic Syntax of C
  • Structure of a C Program
  • Compilation Process in C

All 12 lessons →

Tokens & Basics
  • Tokens in C
  • Keywords in C
  • Identifiers in C
  • Constants in C
  • Variables in C
  • Scope Rules in C
  • Storage Classes in C
  • Comments in C

All 8 lessons →

Data Types & I/O
  • Data Types in C
  • Primitive Data Types
  • Derived Data Types
  • Type Modifiers
  • Type Conversion in C
  • Type Casting in C
  • sizeof Operator
  • Input and Output in C
  • printf() in C
  • scanf() in C
  • Character I/O: getchar and putchar
  • String I/O: gets/fgets and puts
  • Format Specifiers in C

All 13 lessons →

Operators & Expressions
  • Operators in C
  • Arithmetic Operators
  • Relational Operators
  • Logical Operators
  • Bitwise Operators
  • Assignment Operators
  • Increment and Decrement Operators
  • Conditional Operator
  • Comma Operator
  • sizeof and Address Operators
  • Operator Precedence and Associativity
  • Expression Evaluation in C
  • Short-Circuit Evaluation

All 13 lessons →

Control Flow
  • Decision Making in C
  • if Statement
  • if-else Statement
  • else-if Ladder
  • Nested if
  • switch Statement
  • Loops in C
  • for Loop
  • while Loop
  • do-while Loop
  • Nested Loops
  • break Statement
  • continue Statement
  • goto Statement

All 14 lessons →

Functions
  • Functions in C
  • Function Declaration and Definition
  • Function Arguments
  • Return Values
  • Call by Value
  • Call by Reference Using Pointers
  • Parameter Passing Techniques
  • Main Function in C
  • Recursion in C
  • Types of Recursion
  • Inline Functions
  • Nested Functions
  • Function Pointers
  • Variadic Functions

All 14 lessons →

Arrays
  • Arrays in C
  • Declaration and Initialization of Arrays
  • Accessing Array Elements
  • Traversing an Array
  • Passing Arrays to Functions
  • Array Parameters and Pointers
  • One-Dimensional Arrays in C
  • Two Dimensional Arrays
  • Multidimensional Arrays in C
  • Passing 2D Arrays to Functions
  • Array of Pointers
  • Variable Length Arrays

All 12 lessons →

Strings
  • Strings in C
  • String Declaration and Initialization
  • String Length
  • String Traversal
  • String Comparison
  • String Copy
  • String Concatenation
  • Substring Search
  • String Tokenization
  • String Reverse
  • Palindrome String in C
  • String Pattern Searching
  • String Literals

All 13 lessons →

Pointers
  • Pointers in C
  • Address and Dereference Operators
  • Pointer Arithmetic
  • Pointers and Arrays
  • Pointer to Pointer
  • Void Pointers
  • Null Pointers
  • Dangling Pointers
  • Wild Pointers
  • Constant Pointers and Pointer to Constant
  • Function Pointers
  • Array of Pointers
  • Pointers to Functions
  • Pointers to Structures
  • Pointer Type Casting

All 15 lessons →

Structures & Unions
  • Structures in C
  • Structure Members
  • Array of Structures
  • Nested Structures
  • Pointers to Structures
  • Passing Structures to Functions
  • Structure Alignment and Padding
  • Self-Referential Structures
  • Bit Fields in Structures
  • Unions in C
  • Structure vs Union
  • Enumeration in C
  • typedef in C

All 13 lessons →

Dynamic Memory & Memory
  • Program Memory Layout in C
  • Stack and Heap in C
  • malloc()
  • calloc()
  • realloc()
  • free()
  • Dynamic Arrays
  • Memory Leaks
  • Dangling Memory
  • Double Free
  • Use After Free
  • Memory Safety Best Practices

All 12 lessons →

Preprocessor & Headers
  • C Preprocessor
  • Header Files in C
  • #include Directive
  • #define and Macros
  • Function-like Macros
  • Conditional Compilation
  • #ifdef, #ifndef and #endif
  • Predefined Macros
  • Pragma Directive
  • Include Guards

All 10 lessons →

File Handling
  • File Handling in C
  • File Pointers
  • fopen() and fclose()
  • Reading a File
  • Writing to a File
  • Appending to a File
  • fgetc() and fputc()
  • fgets() and fputs()
  • fprintf() and fscanf()
  • Binary File Handling
  • EOF, getc() and feof()
  • Read/Write Structures to Files
  • Delete a File
  • File Error Handling

All 14 lessons →

Error Handling & Debugging
  • Error Handling in C
  • perror()
  • strerror()
  • errno
  • Assertions in C
  • Debugging C Programs
  • Common C Runtime Errors
  • Divide by Zero Handling
  • goto for Error Handling

All 9 lessons →

Date, Time & Misc
  • Date and Time in C
  • time() Function
  • clock() Function
  • Random Numbers in C
  • Linkage in C
  • External and Static Linkage
  • Storage Duration
  • Integer Overflow and Undefined Behavior
  • Implementation-Defined vs Undefined Behavior

All 9 lessons →

Advanced C
  • Generic Selection (_Generic)
  • Variadic Functions
  • Input/Output System Calls
  • Signals in C
  • Signal Handling
  • Socket Programming in C
  • Multithreading in C
  • Threads and Synchronization
  • Atomic Operations
  • Memory-Mapped Files
  • Low-Level Systems Programming

All 11 lessons →

Practice & Projects
  • C Programming Examples
  • C Exercises
  • C Fundamental Practice Problems
  • C Functions Practice Problems
  • C Compound Data Type Practice Problems
  • C Pattern Programs
  • C Array Programs
  • C String Programs
  • C Pointer Programs
  • C Structure and Union Programs
  • C File Handling Projects
  • C Mini Projects
  • C Interview Questions
  • Data Structures and Algorithms in C

All 14 lessons →

Palindrome String in C

Learn how to check whether a string is a palindrome in C using character comparison, loops, and simple examples.

Gaurav 2 min read Sign in to save
On this page

What Is a Palindrome String?

A palindrome string is a string that reads the same from left to right and right to left.

Examples:

madam
level
radar
racecar

For example:

madam

Reversed:

madam

Since both are the same, "madam" is a palindrome.


How to Check a Palindrome

One simple approach is:

  1. Find the length of the string.
  2. Compare the first character with the last character.
  3. Compare the second character with the second-last character.
  4. Continue until the middle is reached.
  5. If all corresponding characters match, the string is a palindrome.

C Program to Check a Palindrome String

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

int main()
{
    char str[100];
    int isPalindrome = 1;

    printf("Enter a string: ");
    scanf("%99s", str);

    int length = strlen(str);

    for (int i = 0; i < length / 2; i++)
    {
        if (str[i] != str[length - 1 - i])
        {
            isPalindrome = 0;
            break;
        }
    }

    if (isPalindrome)
        printf("Palindrome");
    else
        printf("Not a palindrome");

    return 0;
}

Example

Input:

madam

Output:

Palindrome

Input:

hello

Output:

Not a palindrome

How the Comparison Works

For:

madam

The comparisons are:

m == m
a == a

The middle character does not need to be compared with another character.

For an even-length string such as:

abba

the comparisons are:

a == a
b == b

If any comparison fails, the string is not a palindrome.


Time and Space Complexity

If the string contains n characters:

Time Complexity: O(n)

The algorithm checks at most half of the characters, which is still linear in terms of n.

Space Complexity: O(1) additional space, excluding the input string.


Important Note

The program above uses:

scanf("%99s", str);

This reads a single word and stops at whitespace.

Therefore, an input such as:

never odd or even

is not handled as a complete sentence.

For strings containing spaces, use fgets() and decide how spaces and letter case should be treated.


Key Takeaway

A palindrome string reads the same in both directions.

The basic technique is to compare characters from both ends toward the center:

First  ↔ Last
Second ↔ Second Last
...

This is a common beginner programming problem and is useful for practicing strings, loops, indexing, and character comparison.

Related

Found a problem on this page, or something that could be clearer?

Goes straight to the author. Not published anywhere.