Articles

C++ program to take input in string format with whitespaces

C++ program to take input in string format with whitespaces


In this chapter of CPP program tutorial, our task is to :

  • write a cpp program to take input in string format with whitespaces

CPP program to take input in string format with whitespaces.

Given below is the cpp program to take input in string format with whitespaces


/*  Program Name : CPP program to take input in string format with whitespaces.
    Program Author : Krushna Kulkarni
*/

#include<iostream>
#include<string> //for string class

using namespace std;

int main(){

//declaring string variable
string s; 

//using getline() function to read string input "Tutorials In Hand"
getline(s, cin);
 
//printing the output string
cout<< s <<endl; 

return 0;
}

INPUT

Tutorials In Hand

 OUTPUT

Tutorials In Hand

 

EXPLANATION :

In the above cpp program to take input in string format with whitespace:

  • std::getline() is a inbuilt function in cpp which takes input from user in the form of stream of characters.

  • This function keeps reading the input until it finds delim or newline character i.e. /n. or it reaches at the end of the file.

You can see the above code execution and output in codechef IDE:

how to take input string with spaces in c++

 


CPP Programs

Would you like to see your article here on tutorialsinhand. Join Write4Us program by tutorialsinhand.com

About the Author
Krushna Kulkarni
Page Views :    Published Date : Mar 11,2021  
Please Share this page

Related Articles

Like every other website we use cookies. By using our site you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and our Terms of Service. Learn more Got it!