7 Replies - 2675 Views - Last Post: 09 December 2012 - 12:05 PM Rate Topic: -----

#1 bcnafegar  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 13-May 11

changing elements in array

Posted 08 December 2012 - 10:43 AM

I have two arrays of strings, and I want to compare string A (which contains names of states) and array B(which is an article of varying length). What i want to do is compare the two and if the article has any states in it, then the state name changes with <location> Texas<location> in it, for example using texas. I know i can use Boost but I want to use this way first. Any ideas? If need be i can supply code of what i have
Is This A Good Question/Topic? 0
  • +

Replies To: changing elements in array

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4425
  • View blog
  • Posts: 12,293
  • Joined: 18-April 07

Re: changing elements in array

Posted 08 December 2012 - 05:48 PM

Well boost is just a set of libraries to make the job hopefully easier. I don't think you need it for this however. All you need to do is setup a nested for loop where the outer loop loops through each state and the inner loop looks through all the items in array B looking for the state (for this try using the find() method of the string class). This will let you know if the state is in the article and where.

Next all you have to do then is tack on the <location> onto the front and end of the string (sandwich the state as required...for this you can use a number of functions but append() may be of some use).

Store the new sandwich string in a new array or save it in array B.

for each item in array A
    for each item in array B
        string.find A in B
        if found
           set item in B to <location> Item in A <location>
    end for
end for



Some pseudo code for you to play with.

:)
Was This Post Helpful? 0
  • +
  • -

#3 bcnafegar  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 13-May 11

Re: changing elements in array

Posted 08 December 2012 - 11:38 PM

I've tried the above suggestion but can't get it to find the states.
It seems to find words like as, or, an or it will replace words with states that arent in the original article...I've got the code if interested
Was This Post Helpful? 0
  • +
  • -

#4 jimblumberg  Icon User is online

  • member icon


Reputation: 4278
  • View blog
  • Posts: 13,441
  • Joined: 25-December 09

Re: changing elements in array

Posted 09 December 2012 - 09:21 AM

Please post your code that illustrates your problem. Also include samples of any input provided to the program.

Jim
Was This Post Helpful? 0
  • +
  • -

#5 bcnafegar  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 13-May 11

Re: changing elements in array

Posted 09 December 2012 - 09:42 AM


for (int  i=0; i < count;i++)
	{
		for(int j=0;j<count2;j++)
		{
			//if(word[i].state == word2[i].city)
			if ((word2[j].location.find(word[i].state)) == true)
			{
				word2[j].location.append ("<location>" + word2[j].location+"<location/n>");
				//word2[j].city=word[i].state;
				//f<<word[i].state;
				cout <<word2[j].location<< " ";
			}
			
		}
		
	} 







this is the part I'm working on, with both arrays created dynamically. count is the number of states plus territories and count2 is much much larger, around 1500 words from a news article.
It will either not output anything or go into an infinite loop, highlight words such as or,as,an,on, or insert states into the article that werent there in the first place..very confused. Never worker with find() or append() before
Was This Post Helpful? 0
  • +
  • -

#6 Xupicor  Icon User is offline

  • Nasal Demon
  • member icon

Reputation: 249
  • View blog
  • Posts: 582
  • Joined: 31-May 11

Re: changing elements in array

Posted 09 December 2012 - 10:14 AM

When in doubt - read the docs!

string::find doesn't return bool but size_t signifying a position at which sequence was found. If it's not found, string::npos is returned.
Was This Post Helpful? 0
  • +
  • -

#7 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3662
  • View blog
  • Posts: 11,472
  • Joined: 05-May 12

Re: changing elements in array

Posted 09 December 2012 - 10:15 AM

If you've never used the functions you mentioned before, it would make sense to research how they work.

find() returns the position where a string is found, not a boolean as you are using above:
http://en.cppreferen...sic_string/find

append() just appends to the end of the current string: http://en.cppreferen...c_string/append

Unfortunately, I think you took the advice of using append() too literally. You should look at other string manipulation functions.

Also consider that you are modifying the string that you are searching in. If it so happens that your list A contains "Texas", and "ca", and one of your article's in list B contains: "In West Texas, recalcitrant cattle are shot."

On first pass, the string "Texas" is found and replaced. So the article becomes:
"In West <location>Texas</location>, recalcitrant cattle are shot."

On the second pass, the string "ca" is found and replaced. So the article becomes:
"In West <lo<location>ca</location>tion>Texas</lo<location>ca</location>tion>, re<location>ca</location>lcitrant <location>ca</location>ttle are shot."
which is not what you want.
Was This Post Helpful? 1
  • +
  • -

#8 bcnafegar  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 13-May 11

Re: changing elements in array

Posted 09 December 2012 - 12:05 PM

I beleieve i got it. Ended up not using find or append.works pretty well, thanks for everyones help
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1