Part 2 of 5 in Reading input and writing output  

Echo by harsha_s

Problem statement

Read a word from the console and write it back to the console

Input

A single word consisting only of non-whitespace characters, with a maximum length of 50. Sample input:

helloworld!

Output

The same word that you read from the input. Sample output:

helloworld!

We can break this problem down into 4 parts.

  1. Storing a string
  2. Reading from the console
  3. Identify whitespace
  4. Writing to the console

In the editorial for the Hello world problem you can see how to write output to the console. So we will only focus on the first 3 parts in this editorial.

char word[51];
scanf("%s", word);
printf("%s", word);

In C, strings can be easily represented by character arrays. Since the maximum length of the word is 50, we need to declare a character array with size greater than 50.

We can use the scanf function in C to read strings easily. scanf breaks at whitespace automatically. Therefore, we don't need to bother checking for whitespace in our code. You can read more about using scanf for different data types here.

string word;
cin >> word;
cout << word;

In C++, we can represent strings either using character arrays or using the class string provided by the standard template library. Since you've already seen how to use character arrays in the C example, we've used the string class in the code above.

We will use the extraction operator (>>) on the cin object to read a string from the console. The way cout refers to console output, the cin object refers to input from the console. Just like scanf in C, the extraction operator in C++ breaks at whitespace by default. So we do not need to handle it explicitly. You can read more about using >> operator to read different data types here.

Scanner input = new Scanner(System.in);
String word = input.next();
System.out.print(word);

Character arrays are present in Java, but we always use the built-in String class to represent strings. Like C++, Java also exposes the console input as a stream. We can refer to it using System.in. However, reading strings directly from the console can be cumbersome. So we use the Scanner class present in the java.util package.

A Scanner wraps around another input stream and exposes more usable methods for us to use. So we wrap it around the console input stream and then use the next method. Like scanf in C and >> operator in C++, Scanner.next breaks automatically at whitespace making it unnecessary to check explicitly for whitespace. Read more about the Scanner class here.

Line approach
string line = Console.ReadLine();
string parts = line.Split();
string word = parts[0];
Console.Write(word);
Character approach
string word = String.Empty;
while(true)
{
    int nextCharacter = Console.Read();
    if (nextCharacter < 0 ||
            Char.IsWhiteSpace((char)nextCharacter))
        break;
    word += (char)nextCharacter;
}
Console.Write(word);

Character arrays are present in C# as well, but like Java we always use the built-in String class to represent strings. The String class can also be referred to using the keyword string.

We can use the Console class to read input from the console. However, it only allows us to read a single character using Read or read an entire line using ReadLine. There is no method that reads characters until whitespace is found. We can work around this in many ways. Two common approaches are:

  • Line approach: Read the entire line and then split it into multiple parts, divided by whitespace. The word we need is the first part.
  • Character approach: Read character by character and store it in a string. Break when a whitespace is found or there are no more characters to read

Both approaches are valid. The character approach reads minimally from the stream but is longer to code.

The line approach reads more input than necessary but is shorter to code. Splitting a string using String.Split with default options automatically splits on whitespace. This makes it unnecessary for us to explicitly specify whitespace characters. You can read more about how String.Split works at this link.

editorial written by harsha_s

#include<iostream> // To get references to cin and cout
#include<string>   // To get a reference to the string class

// We're importing items in the std namespace
// So we don't need to refer to them as "std::cin", "std::string"...
// Instead we can refer to them as "cin", "string", ...
using namespace std;

int main() {
    // Declare a string
    string word;
    
    // Use the insertion operator to read the string
    cin >> word;
    
    // Print it out
    cout << word;
    
    // Return 0 to indicate normal termination
    return 0;
}

featured solution by harsha_s



To try out your code



Sign in

Sign up