Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

A palindrome is a word, phrase, number, or other sequence of characters which re

ID: 3816859 • Letter: A

Question

A palindrome is a word, phrase, number, or other sequence of characters which reads the same backward as forward, such as madam or racecar. Sentence-length palindromes may be written when allowances are made for to capital letters, punctuation, and word dividers, such as "A man, a plan, a canal, Panama Was it a car or a cat I saw?" or "No "x' i Nixon Wiki Definition write a program that takes any user input as a string and checks whether it is a palindrome The evaluation is based on letters and numbers only in a string. All other characters are ignored. Case differences among letters are also ignored. Validate palindrome examples include: "babab" "aaaaa" "A Aaa "1234321" "A 121 a "A man, a plan, a canal, Panama!" (empty string) (string without letters and numbers) Design considerations: v How to accept inputs of sentence? What is the procedure of comparing? How to avoid comparing non-alphabet and non-digit characters? How to eliminate case differences? When the program is executed, the program keeps asking users to provide test cases unt users input "-99" to terminate the program.

Explanation / Answer

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

// A function to check if a string str is palindrome
int isPalindrome(char str[])
{
    // Start from leftmost and rightmost corners of str
    int l = 0;
    int h = strlen(str) - 1;

    // Keep comparing characters while they are same
    while (h > l)
    {
        while (l <h && (! isalpha(str[l]) || isdigit(str[l])))
        {
            l++;
        }
        while (l < h && ! (isalpha(str[h]) || isdigit(str[h])))
        {
            h--;
        }
        if(isalpha(str[l] && !(isalpha(str[h]))))
        {
           return 0;
        }
       
        if(!(isalpha(str[l]) && isalpha(str[h])))
        {
           return 0;
        }
       
        if(isalpha(str[l] && isalpha(str[h])))
        {
           if (tolower(str[l++]) != tolower(str[h--]))
           {
               return 0;
           }
        }
        else
        {
            if(str[l++] != str[h--])
            {
                return 0;
            }
        }
    }
    return 1;
}

void remove_newline_ch(char *line)
{
    int new_line = strlen(line) -1;
    if (line[new_line] == ' ')
        line[new_line] = '';
}

// Driver program to test above function
int main()
{
    char str[1000];
    while(1)
    {
        printf("Input a string to test or -99 to end: ");
        fgets(str, 500, stdin);
        remove_newline_ch(str);
        if(strcmp(str, "-99") == 0)
        {
            break;
        }
        if (isPalindrome(str))
        {
            printf("<%s> is a Palindome. ", str);
        }
        else
        {
            printf("<%s> is Not a Palindome. ", str);
        }
        printf("------------------------ ");
    }
    return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote