StringStream

I took a look at the StringStream HackerRank challenge. There are two items that one needs to deal with. The first is to parse the coma separated string of integers after it has been assigned to a string stream as suggested by the challenge. Of course one could solve this challenge just parsing the string directly. The second task is to allocate a vector in which to return the integers. At first I just allocated an empty array which I resized each time a new integer had to be inserted. In my final approach I just determined the number of integers by counting the ‘,’s in the string.

The solution was written in C++ (not in Java as explained in More Emphasis on C++) using Visual Studio 2017 Professional Edition IDE.

I used some sample input I used to test my solution. The first one was provided in the challenge. Following is a screen capture of the console generated by the IDE when running sample data:

23,4,56
main <<< str ==>23,4,56<==
23
4
56

-1,-2,-3
main <<< str ==>-1,-2,-3<==
-1
-2
-3

-1,0,1
main <<< str ==>-1,0,1<==
-1
0
1

-1,0,1,2,3,4,5,6,7,8,9,10
main <<< str ==>-1,0,1,2,3,4,5,6,7,8,9,10<==
-1
0
1
2
3
4
5
6
7
8
9
10

You may note that I decided to display the string just to make sure all was well reading the input. I know this was not needed in such a simple case, but I always develop code using a TDD approach. It may take a lit=le longer but in general it is robust.

The solution and modified test code follows:

#include <sstream>
#include <vector>
#include <iostream>

using namespace std;

/*
*/
vector<int> parseInts(string str) {

	// **** count the number of integers in the string (using the ',') ****

	int count = 1;
	for (int i = 0; i < str.size(); i++) {
		if (str[i] == ',') {
			count++;
		}
	}

	// **** instantiate a vector ****

	vector<int> vals = vector<int>(count);

	// **** assign the string to the stringstream ****

	stringstream ss(str);

	// **** parse the stream populating the vector ****

	char	ch;
	int		i = 0;
	while (!ss.eof()) {

		// **** get the next integer and put it in the vector ****

		ss >> vals[i++];
		if (ss.fail()) {
			return vals;
		}

		// **** skip the ',' ****

		ss >> ch;
	}

	// **** ****

	return vals;
}

/*
Test code.
*/
int main() {

	// **** read a string of , separated integers ****

	string str;
	cin >> str;
	cout << "main <<< str ==>" << str << "<==" << endl;

	// **** parse the string extracting the integers ****

	vector<int> integers = parseInts(str);

	// **** display the integer(s) ****

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

	// **** all done ****

	return 0;
}

As you can see I also like to develop code documenting sections. The code is the ultimate source of documentation for any software. In this case documentation might not be needed. Once again, this is how I like to design, implement and test software.

The solution passed all six test cases.

If you have comments or questions, please do not hesitate and post them in this blog. I will replay as soon as possible.

Enjoy;

John

Follow me on Twitter:  @john_canessa

Leave a Reply

Your email address will not be published. Required fields are marked *