This C++ program is used to demonstrates printing given string on the screen input by the user.


#include <iostream>
using namespace std;

int main()
    /* variable definition and initialization */
    char stringArray[100];
    /* Take user input and assign to variable */
    cout << "Please write something:" << endl; //cin >> stringArray;
    /* Print */
    cout << "You enter the string:" << stringArray << endl;
    return 0; 

Reading strings with/without embedded blanks

To read string without blanks.


To read a string with blanks.




Program Output:



This program is written to print a given string input by the user. So, first of all, you have to include the iostream header file using the "include" preceding by # which tells that hat the header file needs to be process before compilation, hence named preprocessor directive. Now, for removing naming conflict you can use namespace statement within a program.

Next is int main(). As you know that all the execution of any C++ program starts from main() function. And if you do not assign any return type to main, by default, it will take 'int' and and you have to return an integer type value back to main() function. Now, you have to declare a character array char stringArray[100];

Like this and name that array, here 'stringArray' and requests for a memory location of 100 bytes fixed storage. Strings written within the double quotes of cout are used to print any message on the screen. The statement written after >> of 'cin' takes values / inputs from memory (here taking characters). Hen the statement Get line extracts characters from the stream as unformatted input and stores in 'stringArray' name character array.

The next cout<<". . . ."; statement displays the string that was entered earlier by the user. And finally the return 0; statement is used to return an integer type value back to main().

Found This Page Useful? Share It!
Get the Latest Tutorials and Updates
Join us on Telegram