5 Replies - 269 Views - Last Post: 26 October 2013 - 04:46 PM Rate Topic: -----

#1 BasiCoding  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 23-November 12

Rearranging a string with parsing or something else?

Posted 26 October 2013 - 03:55 PM

Hello everyone! I'm having some trouble with rearranging strings for cleaner output. My code reads in a text file that has a bunch of lines, each with a start point, an end point, the time between the points, the distance between the points, and the description of the path between the points. For the purposes of this particular program, all of these aforementioned variables are strings. My issue lies with the description of the path. The format of the text file is such that the descriptions are written like so:

"Rt 31, Silver Glen to McLean"

When the output is cleaned up, it should read like so:

"...from Silver Glen and Rt 31 to McLean and Rt 31..."

My initial thought was to create a function that would take the description string as a parameter and rearrange it like an array of characters:

void splitDesc(string myDesc)
{
string sub, join, final;
	int marker = 0;

	for(int i = 0; i < myDesc.length(); i++)
	{
		if(myDesc[i] == ',') //A comma separates the "join" point from the others in the text file
		{
			marker = i;
		}
	}

	join = " and " + myDesc.substr(0, marker);
	sub = myDesc.substr(marker, myDesc.length() - 1); //This is intended to be the description without the first few characters up to one space past the comma

	for(int i = 0; i < sub.length(); i++)
	{
		if(sub[i] == ' ')
		{
			//Replace whitespace with join
		}
	}
}



Unfortunately, I can see that this solution is far from perfect. On top of the character/string conversion error (trying to replace the whitespace character in sub with the string join), I know that every space will be replaced this way, which isn't what I want at all. I'm at a loss at this point...if anyone can suggest other methods that might work more aptly, I would wholeheartedly appreciate it. Thanks!

Is This A Good Question/Topic? 0
  • +

Replies To: Rearranging a string with parsing or something else?

#2 jimblumberg  Icon User is offline

  • member icon


Reputation: 3993
  • View blog
  • Posts: 12,323
  • Joined: 25-December 09

Re: Rearranging a string with parsing or something else?

Posted 26 October 2013 - 04:06 PM

Please provide a sample of your input file, how exactly you want the information displayed and what the program is currently producing.

Jim
Was This Post Helpful? 0
  • +
  • -

#3 BasiCoding  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 23-November 12

Re: Rearranging a string with parsing or something else?

Posted 26 October 2013 - 04:19 PM

The input file is arranged like so:


5 6 2 1.3 "Randall, South St. and Bowes"


My intent is to take the last piece (which I can already do) and rearrange it so that it reads:


South St. and Randall to Bowes and Randall


instead of:


Randall, South St. and Bowes


I already know that the code I have (particularly where I commented in the second for loop) won't be sufficient to rearrange the strings like this. I have a feeling that there's a method I haven't heard about that could make the parsing easier, but for now, I'm unsure of what exactly that is. If possible, I'd like some suggestions on other methods that might work out better than the one I'm using; to clarify, these are things I would explore and tool around with. If my method might actually work, please let me know how I can tweak it a little bit. Thanks!

To clarify, the program doesn't have any output yet because I know that any output I get at this point will be erroneous (since the function is incomplete anyway).
Was This Post Helpful? 0
  • +
  • -

#4 eker676  Icon User is offline

  • Software Engineer
  • member icon

Reputation: 378
  • View blog
  • Posts: 1,833
  • Joined: 18-April 09

Re: Rearranging a string with parsing or something else?

Posted 26 October 2013 - 04:21 PM

Are certain things guaranteed to be in the input?

If the comma and the word 'and' are always in the input, just search for the comma and pull out the substring. Then search for 'and' and split the string.

http://www.cplusplus...ng/string/find/
Was This Post Helpful? 0
  • +
  • -

#5 BasiCoding  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 23-November 12

Re: Rearranging a string with parsing or something else?

Posted 26 October 2013 - 04:43 PM

Yes, some things like the comma and 'and' will always be in these strings. However, with regards to 'and,' I'm not sure how to pull it out so that it doesn't show up in another part of the string and is split there (i.e. Randall --> R and all).
Was This Post Helpful? 0
  • +
  • -

#6 eker676  Icon User is offline

  • Software Engineer
  • member icon

Reputation: 378
  • View blog
  • Posts: 1,833
  • Joined: 18-April 09

Re: Rearranging a string with parsing or something else?

Posted 26 October 2013 - 04:46 PM

Search for " and ".

If there are some street names that have " and " embedded in them, maybe there needs to be a better file format.

This post has been edited by eker676: 26 October 2013 - 04:47 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1