Tuesday, 13 September 2016

Hacker Rank Solution for StringStream

Problem:- Hacker Rank Solution Program In C++ For " StringStream " or Hacker Rank Solution Program In C++ For StringStream or StringStream solution hacker rank or Hacker rank solution for c++ domain or Hacker rank solution for StringStream sub domain or StringStream solution in c++ of hacker rank.

Check This- Hacker rank Solution for C++ Sub Domain Introduction, Classes, STL, Inheritance.

Logic:- For this problem first step is to take a user input and after taking a user input implement the function vector parseInts(string str), user input separated integer. So basically in this problem, we are going to use vector and string stream Below is the explanation of the problem in step by step with an example. For solving this problem we have to know about some basics related to string stream

Operator >> Extracts formatted data.
Operator << Inserts formatted data.
Method str() Gets the contents of underlying string device object.
Method str(string) Sets the contents of underlying string device object.

Explanation:- So as we discuss above first we take a user input in a single line, the input will be separated by the commas. As shown below we declare string str.

string str;
cin >> str;

vector<int> integers = parseInts(str);

for(int i = 0; i < integers.size(); i++) 
{
cout << integers[i] << "\n";
}

Above we also pass the user input separated by commas to vector function. Now the input is taken, next step is to print the integers. if you don't know the how to push back element work, then you have to take a tour with STL (Standard Template Library for C++). Below is the scenario of a vector function.

Recommended:- Hacker rank solution for STL

vector<int> parseInts(string str) 
{
statemants.............1
statemants.............2
statemants.............3
.
.
.
.
.
statemants.............n
}

Now the implementation of the vector function is described below with all needed action to perform and run the code or run the solution correctly.

stringstream ss(str); 
vector<int> out;

char ch;
int temp;

while(ss >> temp) // Extracts formatted data.

out.push_back(temp);
ss >> ch; 
}

That's it our code is working successfully.

Also check- Geeksforgeeks solution for School, Basic, Easy, Medium, Hard Domain.

Copy the full solution or colored code and paste into the hacker rank editor and click to Run Code if the code runs successfully then click to Submit Code. Before pasting the code into the editor make sure you have chosen c++ editor in the top right drop down option.

Submit your solution here:- Click here

Tip:- Before copy the solution I recommended please read this full article, this will help you to build your own logic. If you have any query feel free to share with me, and if you like my work please share, comments and subscribe for our new and latest post related to programming this will help you update all about new post this Article.


Solution:-

#include <sstream>
#include <vector>
#include <iostream>
using namespace std;

vector<int> parseInts(string str)
{

stringstream ss(str);
vector<int> out;

char ch;
int temp;

while(ss >> temp)
{
out.push_back(temp);
ss >> ch;
}
return out;
}

int main()
{


string str;
cin >> str;

vector<int> integers = parseInts(str);

for(int i = 0; i < integers.size(); i++)
{
cout << integers[i] << "\n";
}

return 0;
}

Output:-


No comments:
Write comments

Recommended Posts × +