In this article, we are going to learn how to print or output a string using different languages. Strings are considered a data type in general and are typically represented as arrays of bytes (or words) that store a sequence of characters. Strings are defined as an array of characters.
Topics:
How to Read or Input a String in C |
Scanf() |
---|---|
How to Read or Input a String in C++ |
cin>> |
How to Read or Input a String in Java |
Scanner Class |
How to Read or Input a String in C# |
Console.ReadLine() |
How to Read or Input a String in Python |
input() |
Language Used |
Functions or Classes |
How to Read or Input a String in C
We can use the scanf() function to read a string in C language. It takes the string as an argument in double quotes (” “) with the help of “%s”.
Below is the code for more reference:
#include <stdio.h> int main()
{ char str[13];
// Reading string using scanf()
printf ("Input String: ");
scanf ("%s", str);
printf ("\nEntered String: ");
// %s is used to print a string
printf ("\"%s\"", str);
return 0;
} |
Output:
How to Read or Input a String in C++
To Read the string, just place the string (the variable that stores the string’s value) after cin>>, as shown here in the following program.
Below is the code for more reference:
#include <iostream> using namespace std;
//Driver code int main()
{ string str;gfg
cout << "Input String: ";
// Reading input string using cin
cin >> str;
// Printing string using cout
cout << "String: ";
cout << str;
return 0;
} |
Output:
How to Read or Input a String in Java
In Java, we can use Scanner Class or InputStream to Read the string.
In below code, we will try to use the Scanner class instance to read the string.
/*package whatever //do not write package name here */ import java.io.*;
import java.util.*;
class GFG {
public static void main(String[] args)
{
String str;
// Declare the object and initialize with
// predefined standard input object
Scanner sc = new Scanner(System.in);
// String input
str = sc.nextLine();
// Printing string
System.out.print("String: ");
System.out.println(str);
}
} |
Output:
How to Read or Input a String in C#
A string is represented by class System.String. The “string” keyword is an alias for System.String class and instead of writing System.String one can use String which is a shorthand for System.String class.
Now to Read the String in C#, we need to use the Console.ReadLine() method, as shown below:
using System;
public class GFG {
static public void Main()
{
String str;
// use ReadLine() to read the entered line
str = Console.ReadLine();
Console.Write("String: {0}", str);
}
} |
Output:
How to Read or Input a String in Python
Python input() function first takes the input from the user and converts it into a string. The type of the returned object always will be <class ‘str’>.
# Reading String using input() method name = input (" Input String: ")
# Printing string using print() method print ("String: ", name)
|
Output:
Reading Text Input in JavaScript
JavaScript provides various methods for capturing user input, especially when dealing with text. Here, we’ll explore two common approaches: using the prompt function for simplicity and creating an HTML input element for a more interactive user experience.
1. Using prompt for Basic Input:
The prompt function allows you to gather simple text input from the user in a browser environment.
// Using prompt to get text input let userInput = prompt( "Enter Text:" );
// Displaying the input console.log( "User Input:" , userInput);
|
Output:
GeeksforGeeks