Question
#19
19. A palindrome is a string that can be read backward and forward with the same result. For example, the following is a pali
unction to test if a string is a palindrome using a stack. You can push characters in the stack one by one. When you reach th

with the following instructions

Write a complete program for problem #19 on page 142 of your text. You are required to use a stack for this assignment. Submi
0 0
Add a comment Improve this question Transcribed image text
Answer #1

SOURCE CODE IN C:

stack.h

#include <stdlib.h>

#define MAX_SIZE 1000

struct stack

{

char characters[MAX_SIZE];

int size;

};

struct stack* create()

{

struct stack *st=(struct stack*)malloc(sizeof(struct stack));

st->size=0;

return st;

}

void push(struct stack *st, char c)

{

if(st->size==MAX_SIZE)

return;

st->characters[st->size]=c;

st->size+=1;

}

char pop(struct stack *st)

{

if(st->size==MAX_SIZE)

return '\0';

char data=st->characters[st->size-1];

st->size-=1;

return data;

}

main.c

#include <stdio.h>

#include "stack.h"

#include <string.h>

#include <ctype.h>

int main(void)

{

char str[1000];

printf("Enter the sentence: ");

fgets(str, 1000, stdin);

struct stack *st=create();

char original[1000]="";

for(int i=0; i<strlen(str)-1; i++)

{

char ch=tolower(str[i]);

if(ch>='a' && ch<='z')

{

strncat(original, &ch, 1);

push(st, ch);

}

}

char reverse[1000]="";

for(int i=0; i<strlen(str)-1; i++)

{

char ch=pop(st);

strncat(reverse, &ch, 1);

}

if(strcmp(reverse, original)==0)

printf("It is a palindrome!\n");

else

printf("It is not a palindrome!\n");

return 0;

}

OUTPUT:

Enter the sentence: Madam, Im Adam It is a palindrome!

Regards!

Add a comment
Know the answer?
Add Answer to:
#19 with the following instructions 19. A palindrome is a string that can be read backward...
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
  • 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....

  • In C++, write a stack-based application, assignment7.cpp, which can be used to recognize palindromes (strings spelled...

    In C++, write a stack-based application, assignment7.cpp, which can be used to recognize palindromes (strings spelled identically both backward and forwards). Your program should ignore spaces and punctuation, as well as alpha cases; so, for instance, the string “Madam I’m Adam” counts as a palindrome. You must supply your own push and pop operations. For this exercise (alone) you can use global variables for the stack and the stack pointer. Be sure that your program does not let the user...

  • Using C language Palindromes A palindrome is a word or phrase that reads the same backwards...

    Using C language Palindromes A palindrome is a word or phrase that reads the same backwards and forwards, ignoring punctuation, spaces, and anything that isn’t a letter. Examples of Palindromes Madam, I’m Adam. Kayak Racecar A man, a plan, a canal – Panama! Able was I, ere I saw Elba. Go hang a salami, I’m a lasagna hog! Write a program that does the following: opens a text file named “Proj3input.txt” containing multiple lines of text (1 or more) for...

  • 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...

  • 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...

  • USE C++ Create a Stack class that can handle characters. It should be able to automatically...

    USE C++ Create a Stack class that can handle characters. It should be able to automatically resize (to a larger size only) when full, becoming 1.5 times larger than it was. The stack class must contain the standard public functionality of push(), pop(), empty(). The only additional public functionality allowed isa peek() or top() method, depending upon your design choice. Do not use any of the STL (including the vector type) in your Stack. Test your Stack class by writing...

  • Write a python program that contains a hardcoded string and checks whether it is a palindrome...

    Write a python program that contains a hardcoded string and checks whether it is a palindrome (the same forwards as backwards). If it is then display “It is a palindrome”, otherwise display “It is not a palindrome”. A hardcoded string is simply a string defined in your program, for example: my_string = “Hello” Some sample palindromes to use are: A car, a man, a maraca Desserts, I stressed Madam, I’m Adam You will need to strip out any punctuation such...

  • 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...

  • Implement the Stack Class with an ArrayList instead of an array, including the following functions: -empty...

    Implement the Stack Class with an ArrayList instead of an array, including the following functions: -empty -push -peek -pop -overrided toString() function which returns all of the stack's contents Things to note: -You no longer need a size. -You no longer need to define a constant DEFAULT_CAPACITY. since ArrayLists grow dynamically. -Whenever possible, use ArrayList functions instead of the [ ] (index operator) to implement your stack functions Then write a driver program to do palindrome check. A string is...

  • I really need help with the code for this, in C++ please, I know there's a...

    I really need help with the code for this, in C++ please, I know there's a similar question but the answer isn't correct :[ A palindrome is a string that reads the same forwards and backwards (ignoring spaces, punctuation, and capitalization). Examples are the familiar ``If I had a hi-fi,'' the grander ``A man, a plan, a canal, Panama,'' and ``Some men interpret nine memos.'' So the goal: Design, implement, document, and test a program that reads a line of...

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