i am trying to create a function that i can put in my program but i have got myself stuck.

basically i have a string that contains letters. for example 'gototheshopatnoon'. from that string i need to transfer all the letters to another array and discard any duplicate letters. so once ive done that the string would look like 'gothespan'. so i only have each letter appearing once. (iam not sure if i explained that good enough)

for some reason i just cant get my head around the problem. i can solve the problem in my head easily but when iam trying to put it in code i just cant work it out.

the only way i cant think of doing this it to get string and compare the position next to it. if its the same then delete that position. if it isnt compare the next position on. once it compared to the end of the string get the second position in the array and compare that to all positions then get the 3rd positon in the string and do the same etc . but that way sounds crap and i dont undertsand how i would even program that

could some one help me?

thanks

Edited 6 Years Ago by lethal.b: n/a

What have you done so far? We can't help until we know what you've tried and where you are at.

thats the thing. i havn't started it as i dont understand how i would translate it to code.
this would be my logical way of doing it:

i=0
p=1



string="blahblah"

loop through string untill the end

if string[i] == string[p] then
erase that position
start the loop again

else 
p++

thats what iam thinking about doing but i just dont know if that will work or how i would tanslate that into proper code as i dont understand how i would make the loop

Edited 6 Years Ago by lethal.b: n/a

Copy the first letter to the new array
Start a loop (to copy letters)
    Start a Loop (to compare letters)
        Compare the current letter with all letters in the new array
    If not there, Copy it.
Copy the first letter to the new array
Start a loop (to copy letters)
    Start a Loop (to compare letters)
        Compare the current letter with all letters in the new array
    If not there, Copy it.

is this along the right lines?

int main()
{
	std::string empty[26];
	std::string mystr = "blahblah";
	char temp;
	
	temp = mystr[0];
	empty[0] = temp;

int p=1;

	for (int i=1; i < mystr.length();i++)
	{
		temp = mystr[i];


		for (int n = 0; n < mystr.length();n++)
		{
			if (temp == empty[n])  // i get an error on this line 
			{
			empty[p] = temp;
			p++;
			}
			
	
	}

edit**
my codes wrong i give up. my brain just isnt working today

Edited 6 Years Ago by lethal.b: n/a

If you really desperately need the code, here you go, I do hope you learn from it, as this is just a simple test of logic is all, but hey, we were all beginners once too ;). I warn you, this is not the BEST example of how to do it, but I did it in like 2 minutes in haste, just so maybe I could give you ideas? Take and look and tell me what you think.

#include <iostream>
#include <string>

using namespace std;

void main()
{
	string toparse = "gototheshopatnoon";
	string norepeats;

	bool found = false;

	for(int i = 0; i < toparse.length(); i++)
	{
		for(int j = 0; j < norepeats.length(); j++)
		{
			if(norepeats[j] == toparse[i])
			{
				found = true;
			}
		}
		if(!found)
		{
			norepeats.push_back(toparse[i]);
		}
		else
		{
		    found = false;
		}
	}
	cout << "The original string is: " << toparse << endl;
	cout << "The original string with no repeats is: " << norepeats << endl;
}

Edited 6 Years Ago by WargRider: Better print for display

Comments
Don't give away the answers especially when other posters are trying to direct the OP
This article has been dead for over six months. Start a new discussion instead.