Program to Scan and Print String
- Write a program to Scan and Print String in C
- Write a program to Scan and Print String in C++
- Write a program to Scan and Print String in Python
- Write a program to Scan and Print String in PHP
- Write a program to Scan and Print String in Java
- Write a program to Scan and Print String in Java Script
- Write a program to Scan and Print String in C#
Explanation:
The logic to scan and print a string is a simple process that involves the following steps:
- Declare a String Variable:
- Create a variable to hold the string input.
- Scan (Input) the String:
- Use the appropriate input function or method in the programming language to get the string from the user.
- Print the String:
- Display the string using the respective output function or method.
Program to Scan and Print String
-
C
-
C++
-
Python
-
PHP
-
JAVA
-
Java Script
-
C#
#include <stdio.h>
int main() {
char str[100];
// Scan the string
printf("Enter a string: ");
fgets(str, sizeof(str), stdin); // Use fgets to handle spaces
// Print the string
printf("You entered: %s\n", str);
return 0;
}
#include <iostream>
#include <string>
int main() {
std::string str;
// Scan the string
std::cout << "Enter a string: ";
std::getline(std::cin, str); // getline allows spaces
// Print the string
std::cout << "You entered: " << str << std::endl;
return 0;
}
str = input("Enter a string: ")
# Print the string
print("You entered:", str)
<?php // Scan the string echo "Enter a string: "; $str = trim(fgets(STDIN)); // fgets to read input and trim to remove newline // Print the string echo "You entered: " . $str . "\n"; ?>
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// Initialize scanner object for user input
Scanner scanner = new Scanner(System.in);
// Scan the string
System.out.print("Enter a string: ");
String str = scanner.nextLine(); // nextLine reads the entire line
// Print the string
System.out.println("You entered: " + str);
}
}
const readline = require('readline');
// Create an interface for reading input
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
// Scan the string
rl.question("Enter a string: ", function(str) {
// Print the string
console.log("You entered: " + str);
rl.close();
});
using System;
class Program {
static void Main() {
// Scan the string
Console.Write("Enter a string: ");
string str = Console.ReadLine(); // Reads the entire line
// Print the string
Console.WriteLine("You entered: " + str);
}
}