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...
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...
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 “*”.
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.
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)
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 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
write a python program that reads a sentence and identifies a word from an existing glossary...
write a python program that reads a sentence and identifies a word from an existing glossary and states the meaning of the word
python: Write a program that turns a sentence into camel case. The first word is lowercase,...
python: Write a program that turns a sentence into camel case. The first word is lowercase, the rest of the words have their initial letter capitalized, and all of the words are joined together. For example, with the input "fOnt proCESSOR and ParsER", your program will output "fontProcessorAndParser". Optional: can you do this with a list comprehension? Optional extra question: print a warning message if the input will not produce a valid variable name. You don't need to be exhaustive...
Make an algorithm in the form of a sentence and flowchart from the following cases: Algorithm...
Make an algorithm in the form of a sentence and flowchart from the following cases: Algorithm for calculating the amount of net salary if known: the amount of base salary, allowances, melting money, taxes, and credit deductions.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT