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

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

  • 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 i need programs in C 32) Write a function that, given a string, a width,...

    Please i need programs in C 32) Write a function that, given a string, a width, and an empty string for output, centers the string in the output, centers the string in the output area. The function is to return 1 if the formating is successful and 0 if any errors, such as string length greater than width, are formed. 35) Write a function called newStrCmp that does the same job as strcmp. The declaration for your functions is to...

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