Question

C Program: 6.31 (Palindromes) A palindrome is a string that's spelled the same way forward and...

C Program:

6.31 (Palindromes) A palindrome is a string that's spelled the same way forward and backward.
Some examples of palindromes are:

"radar"
"able was i ere i saw elba"

and, if you ignore blanks:

"a man a plan a canal panama"

Write a recursive function testPalindrome that returns 1 if the string stored in the array is
a palindrome and 0 otherwise. The function should ignore spaces and punctuation in the string.
Use the function in a complete program which first asks the user for the size of the string
and then asks for the string itself. Do not change the original string in your testPalindrome
function. You can assume that capital and smaller case letters are not equal.

Input Validation: if the user enters a value less than or equal to zero for string size, print
an error message and exit.

inputs:

27, a·man·a·plan·a·canal·panama

Output: Enter the size of your string: Enter your string to check if it is a palindrome: "a man a plan a canal panama" is a palindrome.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

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

int testPalindrome(char str[]){
  
// initializing lower and upper with start point and end point of the string
int lower = 0;
int upper = strlen(str) - 1;
  
// checking the string till lower is smaller than and equal to upper
while (lower <= upper) {
// if there is any other character than lowercase or uppercase alphabet, increment the lower index
if (!(str[lower] >= 'a' && str[lower] <= 'z') && !(str[lower] >= 'A' && str[lower] <= 'Z'))
lower++;
// if there is any other character than lowercase or uppercase alphabet, decrement the upper index
else if (!(str[upper] >= 'a' && str[upper] <= 'z') && !(str[upper] >= 'A' && str[upper] <= 'Z'))
upper--;
// if alphabets at lower and upper index are equal, increment the lower index and decrement the upper index
else if (str[lower] == str[upper])
lower++, upper--;
// if alphabets at lower and upper index are not equal, return 0, as the string is not palindrome
else
return 0;
}
  
return 1;
}

int main()
{
int size;

printf("Enter the size of the string: \n");
scanf("%d",&size);

char str[50];
char temp;

// checking if the size of the string is negative
if(size<=0){
printf("Error! Size must be a positive number.\n");
}
else{
printf("Enter your string to check if it is a palindrome: ");
scanf("%c",&temp); // temp statement to clear buffer
   scanf("%[^\n]",str); //[^\n] considers the spaces also in the string, and stops reading input, if it encounters new line.
  
  
// calling and checking if the given string is palindrome or not
if(testPalindrome(str)){
printf("\n%s is a palindrome", str);
}
else{
printf("\n%s is not a palindrome", str);
}
}

return 0;
}

Add a comment
Know the answer?
Add Answer to:
C Program: 6.31 (Palindromes) A palindrome is a string that's spelled the same way forward and...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Problem 1: (Palindromes) A palindrome is a string that's spelled the same way forward and backward....

    Problem 1: (Palindromes) A palindrome is a string that's spelled the same way forward and backward. Some examples of palindromes are; radar, able was i ere i saw elba; and, if you ignore blanks, a man a plan a canal panama .Write a recursive function testPa1indrome that returns 1 if the string stored in the array is a palindrome and 0 otherwise. The function should ignore spaces and punctuation in the string. Please I need it in C program and...

  • C++ Programming Palindrome Detector: A palindrome is any word, phrase, or sentence that reads the same...

    C++ Programming Palindrome Detector: A palindrome is any word, phrase, or sentence that reads the same forward and backward. Here are some well-known palindromes: Able was I, ere I saw Elba A man,a plan, a canal, Panama Desserts, I stressed Kayak Write a book function that uses recursion to determine if a string argument is a palindrome. The function should return true if the argument reads the same forward and backward. Demonstrate the function in a program, which continues to...

  • A Palindrome is a string that is spelled the same way forward and backward (example: radar)....

    A Palindrome is a string that is spelled the same way forward and backward (example: radar). Write a Java program that asks the user to input a string and tests whether the string is a Palindrome or not. Display the message: "The string is a Palindrome" if it is, or "The string is NOT a Palindrome" if it is not. Assume that the user will enter a string without any spaces. The string can be any length. The String can...

  • A palindrome is a word that is spelled the same forward and backward. For example, rotor...

    A palindrome is a word that is spelled the same forward and backward. For example, rotor and redder are palindromes, but motor is not. Write a recursive function that takes a word (string) and check if it is palindrome or not (The function should return True if the word is palindrome otherwise it should return False). USING PYTHON

  • Palindrome Detector C++ A palindrome is any word, phrase, or sentence that reads the same forward...

    Palindrome Detector C++ A palindrome is any word, phrase, or sentence that reads the same forward and backward. Here are some well-known palindromes: Able was I, ere I saw Elba A man, a plan, a canal, Panama Desserts, I stressed Kayak Write a program that determine whether an input string is a palindrome or not. Input: The program should prompt the user "Please enter a string to test for palindrome or type QUIT to exit: " and then wait for...

  • A palindrome is any word, phrase, or sentence that reads the same forward and backward. Here...

    A palindrome is any word, phrase, or sentence that reads the same forward and backward. Here are some well-known palindromes: Able was I, ere I saw Elba A man, a plan, a canal, Panama Desserts, I stressed Kayak Write a program that determine whether an input string is a palindrome or not. Input: The program should prompt the user "Please enter a string to test for palindrome or type QUIT to exit: " and then wait for the user input....

  • C++ A palindrome is a string that reads the same backward as forward. For example, the...

    C++ A palindrome is a string that reads the same backward as forward. For example, the words mom, dad, madam and radar are all palindromes. Write a class Pstring that is derived from the STL string class. The Pstring class adds a member functionbool isPalindrome( )that determines whether the string is a palindrome. Include a constructor that takes an STL string object as parameter and passes it to the string base class constructor. Test your class by having a main...

  • Please Code in Java and follow the directions given Thanks Program required Directions .A palindrome is a str...

    Please Code in Java and follow the directions given Thanks Program required Directions .A palindrome is a string that reads the same forward and backward, i.e., the letters are the same whether you read them from right to left or from left to right. Examples: 3 a) radar à is a palindrome b)Able was I ere I saw Elba à is a palindrome e good à not a palindrome Write java program to read a line of text and tell...

  • This is a java question A palindrome is a word or phrase that reads the same...

    This is a java question A palindrome is a word or phrase that reads the same forward and backward, ignoring blanks and punctuations, and considering uppercase and lowercase versions of the same letter to be equal. For example, the following are palindromes: warts n straw radar Able was I ere I saw Elba tacocat Write a program named Palindrome.java that will accept a file (file name) from the command argument list and decide whether each line in the file is...

  • C++ A palindrome is a string that reads the same backward as forward. For example, the words mom, dad, madam and radar are all palindromes. Write a class Pstring that is derived from the STL string cl...

    C++ A palindrome is a string that reads the same backward as forward. For example, the words mom, dad, madam and radar are all palindromes. Write a class Pstring that is derived from the STL string class. The Pstring class adds a member functionbool isPalindrome( )that determines whether the string is a palindrome. Include a constructor that takes an STL string object as parameter and passes it to the string base class constructor. Test your class by having a main...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT