Skip to main content

Featured

C Program to Solve Two Sum Using Brute Force (With Algorithm & Output)

 Introduction The Two Sum problem is a popular coding interview question where we must find two indices of an array whose values add up to a given target. This program demonstrates a simple brute-force solution in C using nested loops and dynamic memory allocation. Problem Statement Given an integer array and a target value, return the indices of the two numbers such that they add up to the target. Each input has exactly one solution, and the same element cannot be used twice. The result should return the indices, not the values. If no solution exists, return NULL.  Algorithm / Logic Explanation Start the program. Traverse the array using a loop from index 0 to numsSize - 1 . Inside this loop, use another loop starting from i + 1 to numsSize - 1 . For every pair (i, j) , check if nums[i] + nums[j] == target . If condition becomes true: Allocate memory for 2 integers using malloc() . Store indices i and j . Set returnSize = 2 . Return the result poi...

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

🌙