C++ String Explode

Everything todo with programming goes HERE.
Post Reply
User avatar
Light
Reverse Outside Corner Grinder
Posts: 1664
Joined: Thu Oct 20, 2011 2:11 pm

C++ String Explode

Post by Light »

Well, I'll start off by saying I don't know C++ and I'm pretty bad at it. I am just having some fun. My question is about exploding the string by a delimiter, which is a space in this case. Is this an okay way to do it? It seems like there would be a better way, and that this would be slower than .. something.

I did see about using vector, but it seemed a bit complicated. But, I did make a working version, the only way I knew how, using substr to grab char by char and check it's not a space. :P

Code: Select all

#include <iostream>
#include <string>

using namespace std;

int main()
{
	string line;
	
	while (getline(cin, line))
	{
		string input[20];
		int chars = line.length(), p = 0;
		
		for (int i = 0; i < chars; i++)
		{
			string c (line.substr(i, 1));
			
			if (c != " ")
				input[p] += c;
			else
				p++;
		}
		
		if (input[0] == "INVALID_COMMAND")
			cout << "0xffccaaInvalid command \"0xaaccdd" << input[1] << "0xffccaa\" entered by 0xaaccdd" << input[2] << "0xffccaa." << endl;
	}
	
	return 0;
}

User avatar
Z-Man
God & Project Admin
Posts: 11439
Joined: Sun Jan 23, 2005 6:01 pm
Location: Cologne
Contact:

Re: C++ String Explode

Post by Z-Man »

Looks fine. The only real issue is the fixed size input array:
string input[20];
with no bounds check on the index p later on. More words than you expect is an error, right? Then just throw one if p >= 20. Otherwise, make it
std::vector<string> input;
and use input.push_back() to append parsed strings.

And, of course, for really fancy solutions check here:
http://stackoverflow.com/questions/2361 ... tring-in-c
Nothing wrong with coding your own to get your feet wet, though.

User avatar
LOVER$BOY
Match Winner
Posts: 725
Joined: Thu Jan 24, 2008 12:46 pm
Contact:

Re: C++ String Explode

Post by LOVER$BOY »

Can be tricky when you are a totally new person in the world of programming.

Here's something I found in my brief search and it actually works!

Code: Select all

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

using namespace std;

//  source: http://www.zedwood.com/article/106/cpp-explode-function
vector<string> explode( const string &delimiter, const string &str)
{
    vector<string> arr;

    int strleng = str.length();
    int delleng = delimiter.length();
    if (delleng == 0)
        return arr;//no change

    int i = 0;
    int k = 0;
    while( i<strleng )
    {
        int j = 0;
        while (i + j < strleng && j < delleng && str[i + j] == delimiter[j])
            j++;

        if (j == delleng)//found delimiter
        {
            arr.push_back(str.substr(k, i - k));
            i += delleng;
            k = i;
        }
        else
        {
            i++;
        }
    }
    arr.push_back(str.substr(k, i - k));
    return arr;
}

int main()
{
   std::string line;

   while (getline(cin, line))
   {
        /* original code
        string input[20];
        int chars = line.length(), p = 0;

        for (int i = 0; i < chars; i++)
        {
            string c (line.substr(i, 1));

            if (c != " ")
                input[p] += c;
            else
                p++;
        }

        if (input[0] == "INVALID_COMMAND")
         cout << "0xffccaaInvalid command \"0xaaccdd" << input[1] << "0xffccaa\" entered by 0xaaccdd" << input[2] << "0xffccaa." << endl;
        */

        //  exit the script
        if (line == "exit")
            return 0;

        //  getting the exploded value
        std::vector<std::string> list;
        list = explode(" ", line);

        //  displaying the storied list
        for(int i=0; i < list.size(); i++)
            std::cout << list[i] << "\n";
   }

   return 0;
}

It's actually pretty neat and all ;)

Although I programmed something similar within the +ap code but I think I'll base it off this since the one I made is a bit too complicated to easily explain away :P
Image

User avatar
Light
Reverse Outside Corner Grinder
Posts: 1664
Joined: Thu Oct 20, 2011 2:11 pm

Re: C++ String Explode

Post by Light »

Thank you both for the responses. I'll try playing around a little more with this now. Oh, and Z-Man. I didn't check that because I was thinking I chose a value larger than the log would be, but I forgot chat and invalid command logs. :X

I also tried to not use a fixed size, but it seemed pretty inefficient the way I tried to do it.

Thanks again.

User avatar
LOVER$BOY
Match Winner
Posts: 725
Joined: Thu Jan 24, 2008 12:46 pm
Contact:

Re: C++ String Explode

Post by LOVER$BOY »

std::string[] doesn't work because it needs a size limit. That's the bad side of using them. Server code is using them when it comes to hashing and encrypting them.

How do I know? Well, I would know since I spent months pouring through the whole code trying to implement sha1 method.
Image

Post Reply