Read a String with spaces

Problem statement:- Program to Read a String with spaces.

Data requirement:-

   Input Data:- str

  Output Data:-str

Program in C

Here is the source code of the C Program to  Read a String with spaces.

Code:

#include<stdio.h>
int main()
{
    char str[30];
    printf("Enter the String:");
    scanf("%[^\n]",str);
    printf("Your Enter String is :%s",str);

    /*****Another Method******
      char str[30];
    printf("Enter the String:");
    gets(str);
    printf("Your Enter String is:");
    puts(str);*/
}

Input/Output:
Enter the String:csinfo 360
Your Enter String is:csinfo 360

Program in C++

Here is the source code of the C++ Program to  Read a String with spaces.

Code:

#include<iostream>
#include<string>
using namespace std;
int main()
{
    string str;
    cout<<"Enter the String:";
    getline(cin,str);
    cout<<"Your Enter String is : "<<str;
}

Input/Output:
Enter the String:csinfo 360 com
Your Enter String is: csinfo 360 com

Program in Java

Here is the source code of the Java Program to  Read a String with spaces.

Code:

import java.util.Scanner;
public class p1 {

public static void main(String[] args) {
Scanner cs=new Scanner(System.in);
String str;
System.out.println("Enter your String:");
str=cs.nextLine();
System.out.println("Your Enter String is: "+str);
cs.close();
}
}

Input/Output:
Enter your String:
Coronavirus 2020
Your Enter String is: Coronavirus 2020

Program in Python

Here is the source code of the Python Program to  Read a String with spaces.

Code:

str=input("Enter the String:")
print("Your Enter String is:", str)

Input/Output:
Enter the String:COVID-19 COVID-19
Your Enter String is: COVID-19 COVID-19

Post a Comment

0 Comments