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 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...
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 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 :...
Write the code for postfix expression in C++ using a linked stack that can take numbers...
Write the code for postfix expression in C++ using a linked stack that can take numbers bigger than 9 (any size the user gives) and pushes the final result onto the top of the stack
write a Program in C++ Using a structure (struct) for a timeType, create a program to...
write a Program in C++ Using a structure (struct) for a timeType, create a program to read in 2 times into structures, and call the method addTime, in the format: t3 = addTime(t1, t2); Make sure to use add the code to reset and carry, when adding 2 times. Also, display the resultant time using a function: display(t3);
Write a C program that will read a character string and then encrypt the string based...
Write a C program that will read a character string and then encrypt the string based on one of the 3 different encryption methods. The type of encryption is to be selected by the user. Encryption method 1: Swapping by position. Characters in the array are swapped with the opposite characters based on their position in the string. Example: Input string – apple. Encrypted string – elppa Method: The first character ‘a’ and the last character ‘e’ – swap their...
USING C# 1. Write a program that takes outputs a string, an integer and a floating-point...
USING C# 1. Write a program that takes outputs a string, an integer and a floating-point number separated by commas. Sample output: Bob Marley, 20, 5.2 2. 2. Write a program that asks the user for a string of letters of any size (no spaces), and finally a special character (values 33 to 47 in the Ascii table, or ‘!’ to ‘/’). Generate a random number of any size, integer or floating point, and combine those three pieces of information...
Write a C program that counts the number of repeated characters in a phrase entered by...
Write a C program that counts the number of repeated characters in a phrase entered by the user and prints them. If none of the characters are repeated, then print “No character is repeated” For example: If the phrase is “full proof” then the output will be Number of characters repeated: 3 Characters repeated: f, l, o Note: Assume the length of the string is 10. ###Note: the output should print exactly as it is stated in the example if...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT