Translate

Friday, May 22, 2015

Using C, Input a string and check that it is palindrome or not.

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

void main()
{

    char str[25], rev_str[25] = {'\0'};

    int i, len = 0, flag = 0;

    printf("Enter a string: ");

    gets(str);

    for (i = 0; str[i] != '\0'; i++){
        len++;
    }

    for (i = len - 1; i >= 0 ; i--){
        rev_str[len - i - 1] = str[i];
    }

    for (flag = 1, i = 0; i < len ; i++){
        if (rev_str[i] != str[i])
            flag = 0;
    }

    if (flag == 1)
       printf ("%s is a palindrome \n", str);

    else
       printf("%s is not a palindrome \n", str);

}


Download source code

0 comments:

Post a Comment