Write a program to check String is Palindrome without using loop?

devquora
devquora

Posted On: Jan 11, 2021

 

C program to Check String is Palindrome without using a loop is as follows:

/*
 * C program to find the length of a string without using the
 * built-in function also check whether it is a palindrome
 */
#include <stdio.h>
#include <conio.h>
 
void main()
{
    char string[25], reverse_string[25] = {'\0'};
    int i, length = 0, flag = 0;
 
    printf("Enter a string: ");
    gets(string);
    /*  keep going through each character of the string till its end */
    for (i = 0; string[i] != '\0'; i++)
    {
        length++;
    }
    printf("The length of the string '%s' = %d\n", string, length);
    for (i = length - 1; i >= 0 ; i--)
    {
        reverse_string[length - i - 1] = string[i];
    }
   /*  Check if the string is a Palindrome */
 
    for (flag = 1, i = 0; i < length ; i++)
    {
        if (reverse_string[i] != string[i])
            flag = 0;
    }
    if (flag == 1)
       printf ("%s is a palindrome \n", string);
    else
       printf("%s is not a palindrome \n", string);
}

Output:
Enter a string: how  are you
The length of the string 'how  are you' = 12
how  are you is not a palindrome
 
Enter a string: madam
The length of the string 'madam' = 5
madam is a palindrome
 
Enter a string: mam
The length of the string 'mam' = 3
mam is a palindrome

    Related Questions

    Please Login or Register to leave a response.

    Related Questions

    Cyient Java developer Interview Questions

    List types of storage classes in java?

    There are basically four types of storage classes in Java:Automatic storage class: When a variable that is used in the coding is defined within a function and that also with the auto specifier the...

    Cyient Java developer Interview Questions

    Write a java program to generate Fibonacci series?

    A JAVA program to generate Fibonacci series is as follows: import java.util.Scanner; public class Fibonacci { public static void main(String[] args) { int n, a = 0, b = 0, c = ...

    Cyient Java developer Interview Questions

    How does the garbage collector work in Java?

    Garbage collection is the process of automatically managing memory in java. It is helpful to finds the unused objects that are no longer used by the program and deletes or remove them to free up the m...