Tutorialspoint
Problem
Solution
Submissions

Read and Display the Contents of Input

Certification: Basic Level Accuracy: 85.71% Submissions: 14 Points: 10

Write a C++ program that reads and displays the contents from user input. The program should take input from the user and display the contents.

Example 1
  • Input: string = "Hello World"
  • Output: "Hello World"
  • Explanation:
    • Step 1: Allocate memory for storing the input string.
    • Step 2: Read characters from the standard input stream using pointers.
    • Step 3: Store the characters in the allocated memory.
    • Step 4: Display the stored string to the standard output stream.
Example 2
  • Input: string = "C++ Programming"
  • Output: "C++ Programming"
  • Explanation:
    • Step 1: Allocate memory for storing the input string.
    • Step 2: Read characters from the standard input stream using pointers.
    • Step 3: Store the characters in the allocated memory.
    • Step 4: Display the stored string to the standard output stream.
Constraints
  • Input size ≤ 10^6 characters
  • Input contains printable ASCII characters
  • Time Complexity: O(n) where n is the number of characters in the input
  • Space Complexity: O(n) for storing the input
Functions / MethodsFile Handling MicrosoftPwC
Editorial

Login to view the detailed solution and explanation for this problem.

My Submissions
All Solutions
Lang Status Date Code
You do not have any submissions for this problem.
User Lang Status Date Code
No submissions found.

Please Login to continue
Solve Problems

 
 
 
Output Window

Don't have an account? Register

Solution Hints

  • Use string stream to handle input instead of file handling.
  • Read the input character by character or line by line.
  • Display the contents of the input.

Steps to solve by this approach:

 Step 1: Define a function displayContents that takes a constant string reference as parameter.

 Step 2: Create an input string stream with the provided string.
 Step 3: Iterate through the stream character by character using get().
 Step 4: Print each character to the console.
 Step 5: In main, prompt the user to enter content.
 Step 6: Use getline to read the input into a string variable.
 Step 7: Call displayContents with the input string.

Submitted Code :