Question

In: Computer Science

In c++, using stack structure, write a program that will take a sequence of characters (string)...

In c++, using stack structure, write a program that will take a sequence of characters (string) and determine whether it is a palindrome. Use the linked version of the stack.

Solutions

Expert Solution

#include <malloc.h>

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

char* stack;

int top = -1;

// push function

void push(char ele)

{

    stack[++top] = ele;

}

// pop function

char pop()

{

    return stack[top--];

}

// Function that returns 1

// if str is a palindrome

int isPalindrome(char str[])

{

    int length = strlen(str);

    // Allocating the memory for the stack

    stack = (char*)malloc(length * sizeof(char));

    // Finding the mid

    int i, mid = length / 2;

    for (i = 0; i < mid; i++) {

        push(str[i]);

    }

    // Checking if the length of the string

    // is odd, if odd then neglect the

    // middle character

    if (length % 2 != 0) {

        i++;

    }

    // While not the end of the string

    while (str[i] != '\0') {

        char ele = pop();

        // If the characters differ then the

        // given string is not a palindrome

        if (ele != str[i])

            return 0;

        i++;

    }

    return 1;

}

// Driver code

int main()

{

    char str[] = "madam";

    if (isPalindrome(str)) {

        printf("Yes");

    }

    else {

        printf("No");

    }

    return 0;

}


Related Solutions

Using c++, write a program that reads a sequence of characters from the keyboard (one at...
Using c++, write a program that reads a sequence of characters from the keyboard (one at a time) and creates a string including the distinct characters entered and displays the string on the screen. The input terminates once the user enters a white-space character or the user has entered 50 distinct characters. Do not use C-Strings. 2. Use the following function to append character β€œch” to the string β€œs”: s.push_back(ch); 3. Read the input characters one by one, i.e. do...
Write a C program with a struct that contains a string of 12 characters and two...
Write a C program with a struct that contains a string of 12 characters and two integers: number1 and number2. Read all 3 values in from the keyboard using scanf. Print the string and the sum of the two integers using the fully qualified struct names.
write a simple c program to reverse pairs of characters in a string. For exaple, "notified"...
write a simple c program to reverse pairs of characters in a string. For exaple, "notified" becomes "edfitino". The reversal should be in this order in a simple easy to understand c program.
Write a C++ program which reads a string, less than 10 characters long. This string represents...
Write a C++ program which reads a string, less than 10 characters long. This string represents an integer expressed in roman numbers. Let a function convert the number from roman to arabic form (i.e., our standard digits). Let then the main program writes out both forms. The roman numbers are written according to: M = 1000, D = 500, C =100, L=50, X=10, V=5, I=1. Examples: LXXXVII = 87 CCXIX = 219 MCCCLIV = 1354 MMDCLXXIII = 2673
String Manipulator Write a program to manipulate strings. (Visual Studio C++) In this program take a...
String Manipulator Write a program to manipulate strings. (Visual Studio C++) In this program take a whole paragraph with punctuations (up to 500 letters) either input from user, initialize or read from file and provide following functionalities within a class: a)   Declare class Paragraph_Analysis b)   Member Function: SearchWord (to search for a particular word) c)   Member Function: SearchLetter (to search for a particular letter) d)   Member Function: WordCount (to count total words) e)   Member Function: LetterCount (ONLY to count all...
JAVA Write a class for a Stack of characters using a linked list implementation. Write a...
JAVA Write a class for a Stack of characters using a linked list implementation. Write a class for a Queue of characters using a linked list implementation. Write a class for a Queue of integers using a circular array implementation.
Write a function that will take a string containing only alphanumeric characters that are in lowercase...
Write a function that will take a string containing only alphanumeric characters that are in lowercase (if you think your logic requires you to use more than one argument, please go ahead). Your task is to see if the string becomes a palindrome if only one character is removed from anywhere in the string.
IN PYTHON Given a string with duplicate characters in it. Write a program to generate a...
IN PYTHON Given a string with duplicate characters in it. Write a program to generate a list that only contains the duplicate characters. In other words, your new list should contain the characters which appear more than once. Suggested Approach Used two nested for loops. The first for loop iterates from 0 to range(len(input_str)). The second for loop iterates from first_loop_index + 1 to range(len(input_str)). The reason you want to start at first_loop_index + 1 in the nested inner loop...
Write a program that prints out the characters of a string each on a line and...
Write a program that prints out the characters of a string each on a line and counts these characters.  (Do not use the length function len() ).
write this program in C++ Write a program that prompts a user for three characters. The...
write this program in C++ Write a program that prompts a user for three characters. The program must make sure that the input is a number 10 - 100 inclusive. The program must re prompt the user until a correct input is entered. Finally output the largest and the lowest value. Example 1: Input : 10 Input : 20 Input : 30 The largest is 30. The lowest is 10. Example 2: Input : 100 Input : 50 Input :...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT