C Program to make a Palindrome String Checker

Introduction

The C programming language is a procedural programming language. Between 1969 and 1973, Dennis Ritchie worked on the project. It was created primarily as a system programming language for developing operating systems. Low-level memory access, a small collection of keywords, and a clean style are all qualities that make C language excellent for system programming, such as operating system or compiler development.

Why Learn C Programming Language?

Because it mixes the characteristics of high-level languages with the functionalism of assembly language, C is often referred to as a middle-level computer language. The manipulation of bits, bytes, and addresses in C gives the programmer more control over how the program will behave and more direct access to the underlying hardware mechanisms.

Working programmers influenced, influenced, and field-tested C. As a result, C provides the programmer with exactly what he or she want. C++ is a more advanced version of the C programming language. C++ incorporates all of the features of C, as well as support for object-oriented programming (OOP). Furthermore, C++ includes numerous enhancements and features that make it a “better C,” regardless of whether or not it is used for object-oriented programming.

Palindrome Strings

If a string reads the same backward as forward, it is said to be palindrome. For example, “dad”, “bad”, “hannah” are a palindrome since reading it backwards produces the same result as reading it forwards. One way to check this is to iterate through the string until you reach the middle and compare a character back and forth. But the word “follow” and “tutorials” aren’t a palindrome. 

Palindrome String Checker

The best way to learn is to do. Hence, what better way to learn C programming language (if you know the basics) than to code your first mini project. In this tutorial we are going to build a Palindrome String Checker using c programming language. Keep in mind this is a mini project for absolute beginners and intermediate programmers as well.

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>

// string header is added in order to input strings
#include <string.h>

int main(){
    char string1[20];
    int i, length;
    int flag = 0;
    
    printf("Enter a string to check palindrome or not: ");
    scanf("%s", string1);
    
    length = strlen(string1);
    
    for(i=0;i < length ;i++){
        if(string1[i] != string1[length-i-1]){
            flag = 1;
            break;
   }
}
    // flag is set to 1 and this message is returned 
    if (flag) {
        printf("%s is not a palindrome", string1);
    }
   // flag is set to 0 as default and this message is returned     
    else {
        printf("%s is a palindrome", string1);
    }
    return 0;
}

Explanation for the Output

  • Let’s say our input string is “hannah”.
  • String1 is the same as the 5th character in the same string as the 0th character in the char array.
  • The first and fourth characters are the same.
  • The second and third characters are the same.
  • In general, our approach is ith character is same as ‘length-i-1’th character.
  • If any of the conditions above fails, flag is set to true(1), indicating that the string is not a palindrome.
  • The value of flag is false by default (0). As a result, the string is a palindrome if all of the conditions are met.

Output

SHARE C Program to make a Palindrome String Checker

You may also like...

Leave a Reply

Your email address will not be published.

Share