i need help with this C programing question Write a function

i need help with this C programing question

Write a function is_palindrome() embedded in a program, that takes a pointer to the beginning of a string and returns a boolean value. That value is true if the string is a palindrome, false otherwise. The program is supposed to get a string from the user and pass it to the function. You can use gets() to accomplish this, or, alternatively and more secure, fgets(). A call to fgets() would look something like that: fgets(string, 80, stdin); Be aware though that this function includes the newline character at the end of the string, which you will want to remove. Hand in the C program file, commented *abundantly*.

Solution

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

#define TRUE 1
#define FALSE 0

int is_palindrome(char *str)
{
    int end = 0;
    int start = 0;
  
    //Maintain a pointer to the end of the string
    while(str[end])
        end++;
  
    //Point end to last character as it will be holding the length of the string
    end = end-1;

    //till the start and end pointer cross each other.Compare first and last character and if there is a mismatch return false.
    while(start<end)
    {
        if(str[start]!=str[end])
            return FALSE;
        start++;
        end--;
    }
  
    //if above loop completes without any mismatch then string is palindrome
    return TRUE;
}

int main()
{
    char str[80];
  
    //Get the string from the user
    printf(\"Please input a string : \");
    gets(str);

    //if the function returns true then print it is palindrome else not.
    if(is_palindrome(str))
        printf(\"String %s is palindrome.\",str);
    else
        printf(\"String %s is not palindrome.\",str);

    return 0;
}

i need help with this C programing question Write a function is_palindrome() embedded in a program, that takes a pointer to the beginning of a string and return

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site