In programming, reading a line of input means accepting a string from the user. The input may contain multiple words separated by spaces.
For example, if the input is "GeeksforGeeks is a learning platform", the output is GeeksforGeeks is a learning platform.
Read a string s, that may contain multiple words separated by spaces.
Examples:
Input: s = "hello world"
Output: hello worldInput: s = "Welcome to GeeksForGeeks"
Output: Welcome to GeeksForGeeks
Try It Yourself
The idea is to read the complete line of input as a string, including spaces, and print it without modifying it.
Different Language Input Methods
- C++: getline(cin, s)
- C: fgets(s, sizeof(s), stdin)
- Java: Scanner.nextLine()
- Python (3.x): input()
- C#: Console.ReadLine()
- JavaScript: readline() (Node.js) / prompt() (Browser)
Working of Approach:
- Declare a string variable to store the input.
- Use the appropriate input method to read the complete line, including spaces.
- Print the string as it is.
#include <iostream>
#include <string>
using namespace std;
int main()
{
string s;
// Take input of 's' using getline
getline(cin, s);
// Print the complete string
cout << s << endl;
return 0;
}
#include <stdio.h>
#include <string.h>
int main()
{
char s[1000];
// Take input of 's' using fgets
fgets(s, sizeof(s), stdin);
printf("%s", s);
return 0;
}
import java.io.*;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
String s;
// Take input of 's' using nextLine
s = sc.nextLine();
System.out.println(s);
}
}
s = input()
print(s)
using System;
class GFG {
static void Main()
{
string s;
// Take input of 's' using Console.ReadLine()
s = Console.ReadLine();
Console.WriteLine(s);
}
}
let s = readLine();
console.log(s);
Output
Welcome to GeeksForGeeks