Question

Write a program that reverses a string and prints it on the screen. Sample input/output: Enter a string to be reversed: Testing the reverse string program reversed string: margorp gnirts esrever eht gnitseT
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Sample Output:

gcc version 4.6.3 Enter a string to be reversed: Testing the reverse string program reversed string: margorp gnirts esrever eht gnitseT

Code to Copy:

#include <iostream>

#include <bits/stdc++.h>

using namespace std;

//Define the function to reverse the string.

void reverse_string(string &input)

{

//Define the temporary variable.

string b;

//Form the reverse of string in variable b;

for(int i=input.size()-1;i>=0;i--)

b+=input[i];

//Set the reverse string in the original

//variale

input=b;

}

//Define the main function.

int main()

{

//Define the variable

string input;

//Prompt user for input.

cout <<"Enter a string to be reversed:"<<endl;

getline(cin,input);

//Display the message and reverse string.

cout<<"reversed string: ";

reverse_string(input);

cout<<input<<endl;

//Return the value 0.

return 0;

}

Note: The language has not been specified so the solution is provided in c++.

Add a comment
Know the answer?
Add Answer to:
Write a program that reverses a string and prints it on the screen. Sample input/output: Enter...
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
  • C++ Write a recursive function that reverses the given input string. No loops allowed, only use...

    C++ Write a recursive function that reverses the given input string. No loops allowed, only use recursive functions. Do not add more or change the parameters to the original function. Do not change the main program. #include <iostream> #include <string> using namespace std; //Write a recursive function 'void reverse(string &str)' that reverses the given input string. void reverse(string &str) { /*Code needed*/ } int main() {    string name = "sherry";    reverse(name);    cout << name << endl; //should...

  • Your program must prompt the user to enter a string. The program must then test the...

    Your program must prompt the user to enter a string. The program must then test the string entered by the user to determine whether it is a palindrome. A palindrome is a string that reads the same backwards and forwards, such as "radar", "racecar", and "able was I ere I saw elba". It is customary to ignore spaces, punctuation, and capitalization when looking for palindromes. For example, "A man, a plan, a canal. Panama!" is considered to be a palindrome....

  • Write a C++ program that reverses a file. More specifically, given an input file (input.txt) your...

    Write a C++ program that reverses a file. More specifically, given an input file (input.txt) your program will reverse every line in the input. This program does not have an output file, as it modifies the input file directly. You may assume that your input file has 1000 lines or less, if this helps. sample input: Hello World! I study C++ following output: !dlroW olleH ++C yduts I

  • In c programming, I need to write a program that reverses each of the lines in...

    In c programming, I need to write a program that reverses each of the lines in a file. The program accepts two command line arguments: The first one is the name of the input file The second is the name of the output file If the user didn't give two filenames, display an error message and exit. The program reads in each line of the input file and writes each line in reverse to the output file. Note, the lines...

  • Using C, Write a function reverse which takes a string as an argument, reverses the string...

    Using C, Write a function reverse which takes a string as an argument, reverses the string and returns the reversed string. Note; you should not return a string that you created inside the reverse function! For example: Test char str[]-"hello" printf("%s", reverse (str)); Result olleh

  • In C Programming Language, write a program Character Pointers and Functions. Keyboard input to enter one...

    In C Programming Language, write a program Character Pointers and Functions. Keyboard input to enter one character string. Using a single dimension array, populate the array with the character string, call a function using pointers to reverse order the character string, pass back to the main the output and total_count_of_characters. (maybe use a global variable for the total count). Print display the reversed char string and total_count.

  • In C Programming Language, write a program Character Pointers and Functions. Keyboard input to enter one...

    In C Programming Language, write a program Character Pointers and Functions. Keyboard input to enter one character string. Using a single dimension array, populate the array with the character string, call a function using pointers to reverse order the character string, pass back to the main the output and total_count_of_characters. (maybe use a global variable for the total count). Print display the reversed char string and total_count.

  • Overview These exercises will allow you to have some practice with basic Java file Input/Output. In...

    Overview These exercises will allow you to have some practice with basic Java file Input/Output. In addition, you will also have an opportunity to have more practice with methods and arrays.   Objectives Practice with programming fundamentals Variables - Declaration and Assignment Primitive types Arithmetic Expressions Simple keyboard input and text display output Branching - if-elseif-else syntax Loops - simple while loops, nested while loops Methods - functions and procedures ArrayLists - collections of variables File I/O Works towards the following...

  • Write a Java program which takes a string as a user input. Create 2 functions. The...

    Write a Java program which takes a string as a user input. Create 2 functions. The first function expects a string as argument and returns void. It converts all upper case characters to lower case and converts all lower case characters to upper case. It then prints out the converted string to a console. The second function also expects a string as argument and returns void. It will find first charactor that is repeated exactly 2 times in the string....

  • Write a JAVA program that has 2 methods with the same name: printCaps(String input) prints the...

    Write a JAVA program that has 2 methods with the same name: printCaps(String input) prints the input in capital letters, printCaps(String input, int num) prints in capital letters the specified number of times on a new line: public static void printCaps(String input) public static void printCaps(String input, int num)

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