C++ Program to Illustrate Usage of String Streams

This C++ program illustrates usage of string streams. The std::stringstream is a stream class to operate on strings whose instantiations use string buffer that contain sequence of characters. This sequence of characters can be directly accessed as a string object. The characters can be extracted and inserted using any operation allowed on input and output streams.

Here is the source code of the C++ program which illustrates usage of string streams. The C++ program is successfully compiled and run on a Linux system. The program output is also shown below.

/*

* C++ Program to illustrate usage of string streams

*/

#include <iostream>

#include <sstream>

#include <string>

usingnamespace std;

int main (){

stringstream ss;

string s;

cout<<"Enter your first name\n";

cin>> s;

ss.str(s);

cout<<"Enter your last name\n";

cin>> s;

ss.str(ss.str()+" "+ s);

cout<<"Hello "<< ss.str()<<"!\n";

}

advertisements

$ gcc test.cpp
$ a.out
Enter your first name
George
Enter your last name
Harrison
Hello George Harrison!

Sanfoundry Global Education & Learning Series – 1000 C++ Programs.

If you wish to look at all C++ Programming examples, go to C++ Programs.