Skip to main content

Featured

C Program to Check Prime Number Using Efficient Logic

  Introduction A prime number is a number that has exactly two distinct positive divisors: 1 and itself. In this program, we check whether a given number is prime or not using a simple and efficient logic. This type of program is commonly used in mathematics, competitive programming, and basic algorithm learning for beginners in C programming. Problem Statement The task is to write a C program that determines whether a given integer is a prime number or not. The program takes a single integer input from the user and analyzes its divisibility. If the number has no divisors other than 1 and itself, it should be identified as a prime number; otherwise, it is not prime. This problem is important in number theory and has practical relevance in areas such as cryptography, data validation, and algorithm design.  Algorithm / Logic Explanation To check whether a number is prime, we need to verify that it is not divisible by any number other than 1 and itself. The algorithm follows a si...

How to Check Palindrome String in C Programming (With Example)

 Intro

A palindrome string is a string that reads the same forwards and backwards. In this post, we will learn how to check whether a given string is a palindrome using a simple C program. This example is beginner-friendly and helps in understanding string handling and logical thinking in C programming.

Problem Statement

The program takes a string as input from the user. The input string may contain characters and spaces. The task is to check whether the given string is a palindrome or not. If the string reads the same from left to right and from right to left, the program should display that it is a palindrome; otherwise, it should display that it is not a palindrome. This problem helps beginners practice string manipulation and conditional logic in C programming.


Algorithm / Logic Explanation

To check whether a string is a palindrome, we use the two pointer technique, which is simple and efficient. First, the program takes a string input from the user. Two integer variables are used as pointers: left and right. The left pointer is initialized at the beginning of the string, while the right pointer is initialized at the end of the string using the length of the string.

The program compares the characters at the left and right positions. If both characters are the same, it means the string is matching from both ends. Then, the left pointer moves one step forward and the right pointer moves one step backward. This process continues until the two pointers meet in the middle of the string.

If at any point the characters do not match, the program immediately concludes that the string is not a palindrome. If all character comparisons are successful, the string is confirmed as a palindrome. This method works because palindrome strings have symmetrical characters around their center.

C Program to Check Palindrome String


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

void pal(char str[])
{
    int left = 0, right = strlen(str) - 1;
    while (left < right)
    {
        if (str[left] != str[right])
        {
            printf("No, Entered string is not palindrome:\n");
            return;
        }
        left++;
        right--;
    }
    printf("Yes, Entered string is palindrome:\n");
}

int main()
{
    char str[100];
    printf("Enter the string:\n");
    scanf(" %[^\n]", str);
    pal(str);
}

Sample Input / Output

Example 1

Input

madam

Output

Yes, Entered string is palindrome

Example 2

Input

hello

Output

No, Entered string is not palindrome

Edge Cases / Notes

An empty string or a single character is always considered a palindrome.
The program is case-sensitive, so uppercase and lowercase letters are treated as different characters.
Spaces and special characters are compared as they are entered.
This program is designed for learning purposes and works best with simple text inputs.

 Conclusion

In this post, we learned how to check whether a string is a palindrome using a simple C program. By applying the two pointer technique, we efficiently compared characters from both ends of the string. This logic is easy to understand and is widely used in beginner-level string problems.

FAQs

Q1: What is a palindrome string?
A palindrome string is a string that reads the same from left to right and from right to left. Examples include words like “madam”, “level”, and “radar”.

Q2: Why is the two pointer method used in this program?
The two pointer method helps compare characters from both ends of the string at the same time. This reduces unnecessary comparisons and makes the logic simple and efficient.

Q3: Does this program work with spaces in the string?
Yes, this program allows spaces in the input string and compares them as regular characters.

Q4: Is this program case-sensitive?
Yes, uppercase and lowercase letters are treated as different characters in this program.


Keywords

  • C palindrome string program
  • palindrome program in C
  • how to check palindrome in C
  • C string palindrome example
  • beginner C string programs
  • palindrome logic in C
  • C programming string examples
  • two pointer technique in C
  • C program with strings
  • C programming for beginners
Hashtags

#cprogramming
#clanguage
#coding
#programming
#stringprograms
#palindrome
#beginners
#learnc
#computerscience
#1printf

Related Posts

Related: C Program to Reverse String Using Pointers

Related: C Program to Check Palindrome Number

Related: C Program to Find Length of String Without Using strlen()

Comments

Popular Posts

🌙