Question

In: Computer Science

solution using stack Reversing a word or a sentence: Write an algorithm that will display a...

solution using stack

Reversing a word or a sentence:

Write an algorithm that will display a given word in a reverse order.

- e.g. "Data Structures" will be "serutcurtS ataD".

Solutions

Expert Solution

1). ANSWER :

GIVENTHAT :

To Write an algorithm that will display a given word in a reverse order e.g. “Data Structures” will be “serutcurtS ataD”.

Algorithm to display the reverse string

1.Start.
2.Declare all the variables ( integer(i,j) and character (arrString [100], temp) )
3.Enter the string to be reversed.
4.Find the length of string.
5.Initialize the variables i,j (i=0,j= stringlength-1)
6.Swap the position of array element using the loop((arrString [i] is interchanged with (arrString[j]).
7.Increment i
8.Decrement j
9.If i < j then continue loop and goto step 6
10.Print the reversed string to user.
11.Stop.

Example C Program

#include<stdio.h>
#include<string.h>
int main()
{
// variable declarations
char arrString [100], temp;
int i, j = 0;
printf(" Enter the string to be reversed:");
gets(arrString ); // input string

//Initialize i,j
i = 0;
j = strlen(arrString ) - 1;
while (i < j)
{
//Swap characters
temp = arrString [i];
arrString [i] = arrString [j];
arrString [j] = temp;
i++;
j--;
}
printf("\nThe Reversed string is :%s", arrString ); // Output
return (0);
}

The above program Output :-


Related Solutions

in Java language, in most simple algorithm Using a stack class, write a static method called...
in Java language, in most simple algorithm Using a stack class, write a static method called parse that parses a String for balanced parentheses. we seek only to determine that the symbol ‘{‘ is balanced with ‘}’. parse accepts a single String parameter and returns an int. If parse returns a minus 1, then there are no errors, otherwise, parse should return the position within the String where an error occurred. For example parse(“{3 + {4/2} }”)   would return -1...
Write a java program that read a line of input as a sentence and display: ...
Write a java program that read a line of input as a sentence and display:  Only the uppercase letters in the sentence.  The sentence, with all lowercase vowels (i.e. “a”, “e”, “i”, “o”, and “u”) replaced by a strike symbol “*”.
I have to write an initial algorithm and a refined algorithm for the following problem: Display...
I have to write an initial algorithm and a refined algorithm for the following problem: Display the square of numbers from 0 to some user inputted value. For example if the user enters 3 then the program will need to display the square of 0, 1, 2 and 3. Must use a counter loop. Must use 2 methods. One method that gets the number from the user and returns it. A second method is passed that number as a parameter...
Display the shortest words in a sentence in JAVA Write a method that displays all the...
Display the shortest words in a sentence in JAVA Write a method that displays all the shortest words in a given sentence. You are not allowed to use array In the main method, ask the user to enter a sentence and call the method above to display all the shortest words in a given sentence. You must design the algorithms for both the programmer-defined method and the main method.
In PYTHON: Write a function that receives a sentence and returns the last word of that...
In PYTHON: Write a function that receives a sentence and returns the last word of that sentence. You may assume that there is exactly one space between every two words, and that there are no other spaces at the sentence. To make the problem simpler, you may assume that the sentence contains no hyphens, and you may return the word together with punctuation at its end.
I am trying to work on LCD display using VHDL. I want to display the word...
I am trying to work on LCD display using VHDL. I want to display the word “TECH” and make it blink on LCD. I need the complete source code (If anyone helps me and the code works I will offer bonus cash for it )
Write an algorithm that uses a stack, reads from a stream of objects (operations and values)...
Write an algorithm that uses a stack, reads from a stream of objects (operations and values) in prefix order, and prints the result of that evaluation. Trace 2 examples to demonstrate that your algorithm is valid. Analyze your algorithm with respect to n (the number of objects in the stream)
Given a stack S and an element x, write a recursive algorithm that returns True if...
Given a stack S and an element x, write a recursive algorithm that returns True if x is in S, or False otherwise. Note that your algorithm should not change the content in S. What is the time complexity of your algorithm?
Given a stack S and an element x, write a recursive algorithm that returns True if...
Given a stack S and an element x, write a recursive algorithm that returns True if x is in S, or False otherwise. Note that your algorithm should not change the content in S. What is the time complexity of your algorithm?
Write a class to accept a sentence from the user and prints a new word in...
Write a class to accept a sentence from the user and prints a new word in a terminal formed out of the third letter of each word. For example, the input line “Mangoes are delivered after midnight” would produce “neltd”. Program Style : Basic Java Programming
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT