Jump to content

C++ splitting string into array.

Can anyone help me? i wanted to split a user input strings into array. seperating them by "," (comma) and will ignore curly braces and spaces. the user must input {abc,def,ghi} (including the curly braces).
the result should be like:
 

array[0] = abc
array[1] = def
array[2] = ghi

 

i'm so lost and really confuse splitting string.
here is my code

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

int main(){
    string line;
    cin >> line;
    string arr[4];
    int i = 0;
    stringstream ssin(line);
    while (ssin.good() && i < 4){
        ssin >> arr[i];
        ++i;
    }
    for(i = 0; i < 4; i++){
        cout << arr[i] << endl;
    }
}

 

Link to comment
https://linustechtips.com/topic/845379-c-splitting-string-into-array/
Share on other sites

Link to post
Share on other sites

I'd be using (the probably extremely inefficient solution) of strcmp, keeping track of the location of the last comma and then when I find another I'd add that string in between to the array.

Join the Appleitionist cause! See spoiler below for answers to common questions that shouldn't be common!

Spoiler

Q: Do I have a virus?!
A: If you didn't click a sketchy email, haven't left your computer physically open to attack, haven't downloaded anything sketchy/free, know that your software hasn't been exploited in a new hack, then the answer is: probably not.

 

Q: What email/VPN should I use?
A: Proton mail and VPN are the best for email and VPNs respectively. (They're free in a good way)

 

Q: How can I stay anonymous on the (deep/dark) webzz???....

A: By learning how to de-anonymize everyone else; if you can do that, then you know what to do for yourself.

 

Q: What Linux distro is best for x y z?

A: Lubuntu for things with little processing power, Ubuntu for normal PCs, and if you need to do anything else then it's best if you do the research yourself.

 

Q: Why is my Linux giving me x y z error?

A: Have you not googled it? Are you sure StackOverflow doesn't have an answer? Does the error tell you what's wrong? If the answer is no to all of those, message me.

 

Link to post
Share on other sites

51 minutes ago, Ranz said:

<snip>

One would probably want to use regex for this, which has been added to the C++ 11 standard. For a normal solution, maybe something along these lines?

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

int main() 
{
	/* Read input string from user */
	std::string input;
	if (!std::getline(std::cin, input))
	{
		std::cout << "Failed to read input string!\n";
		return 1;
	}

	/* find position of open brace { and closing brace }...
	   display error message if one or both braces not found... */	
	auto openBracePos = input.find('{');
	auto closeBracePos = input.find('}');
	if (openBracePos == std::string::npos
	||  closeBracePos == std::string::npos)
	{
		std::cout << "Input string format error, {} braces not found!\n";
		return 2;
	}
	
	/* Create a stringstream from the substring between the braces {} */
	std::stringstream instream(input.substr(openBracePos + 1, closeBracePos - openBracePos - 1));

	/* Create the vector to store the result. */
	std::vector<std::string> arr;

	/* Use getline to get each substring using ',' as delimiter. */
	std::string s;
	while (std::getline(instream, s, ','))
	{
		s.erase(0, s.find_first_not_of(' '));   //remove leading spaces
		arr.push_back(s);
	}

	/* Print result. */
	std::cout << arr.size() << " strings read.\n";
	for (const auto& s : arr)
	{
		std::cout << s << '\n';
	}


	return 0;
}

 

Link to post
Share on other sites

38 minutes ago, Unimportant said:

One would probably want to use regex for this, which has been added to the C++ 11 standard. For a normal solution, maybe something along these lines?


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

int main() 
{
	/* Read input string from user */
	std::string input;
	if (!std::getline(std::cin, input))
	{
		std::cout << "Failed to read input string!\n";
		return 1;
	}

	/* find position of open brace { and closing brace }...
	   display error message if one or both braces not found... */	
	auto openBracePos = input.find('{');
	auto closeBracePos = input.find('}');
	if (openBracePos == std::string::npos
	||  closeBracePos == std::string::npos)
	{
		std::cout << "Input string format error, {} braces not found!\n";
		return 2;
	}
	
	/* Create a stringstream from the substring between the braces {} */
	std::stringstream instream(input.substr(openBracePos + 1, closeBracePos - openBracePos - 1));

	/* Create the vector to store the result. */
	std::vector<std::string> arr;

	/* Use getline to get each substring using ',' as delimiter. */
	std::string s;
	while (std::getline(instream, s, ','))
	{
		s.erase(0, s.find_first_not_of(' '));   //remove leading spaces
		arr.push_back(s);
	}

	/* Print result. */
	std::cout << arr.size() << " strings read.\n";
	for (const auto& s : arr)
	{
		std::cout << s << '\n';
	}


	return 0;
}

 

Thank you very much! such an effort ty

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×