Jump to content
  • Advertisement
Sign in to follow this  
PennstateLion

better way to do this?

This topic is 4863 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

trying to take one string and taking each element in it and searching another string, if i get a match, i want to delete teh element from the first string... dunno if this is the cleanest way to do it thanks in advance!
void squeeze(char q[], char w[])
{
int i, j, k;
char switched;
	for(i = j = 0; q != '\0'; i++)
	{
		switched = 'N';
		for(k = 0; w[k] != '\0'; k++)
		{
			if (q == w[k])
			switched = 'Y';
		}
		if (switched != 'Y')
			q[j++] = q;
	}
}

Share this post


Link to post
Share on other sites
Advertisement
I'm sorry in advance if I've misunderstood your question, as my thinking patterns are really confused today...

Anyway, your problem seems to fall under the general category of string searching algorithms, of which there are several that you can choose from (it's a pretty common problem). This is assuming you are searching for a short string within a large string, such as replacing every occurance of a specific word in a text document (such as swapping "colour" for "color", as an example).

Now I'm not sure if that's what you want. Maybe you are wanting to look for each specific character in a large string and removing it? If that's the case, there's a better way to do it. Since there's only 256 characters anyway (assuming you are using ASCII), you can store which characters to delete in a lookup table. Then when searching through the big string, just look up in the table whether it needs to be deleted or not. This will give you a better performance.

If any of that's not clear, I'll clarify any further questions you have! Best of luck!

Share this post


Link to post
Share on other sites
Maybe there is.

void squeeze(char q[], char w[])
{
int i = 0, j = 0, k;

for(; q != '\0'; ++i)
{
q[j++] = q;
for(k = 0; w[k] != '\0'; ++k)
{
if (q == w[k])
{
--j;
break;
}
}
}
q[j]='\0';
}



or

void squeeze(char q[], char w[])
{
int i = 0, j = 0, k;

for(; q != '\0'; ++i)
{
for(k = 0; w[k] != '\0'; ++k)
{
if (q == w[k])
break;
}
if(!w[k])
q[j++] = q;
}
q[j]='\0';
}


Share this post


Link to post
Share on other sites
1) Algorithm is nowhere close to being correct.
2) Use sane variable names. They cost nothing because they don't exist at runtime.
3) C++ provides a boolean type, "bool"; use it.
4) C++ provides a real string type; use it. Even in C, at least make use of the available relevant library functions.

In C++:


void squeeze(std::string& target, const std::string& toRemove) {
size_t position = target.find(toRemove);
target.erase(position, position + toRemove.length());
}


In C:


void squeeze(char[] target, char[] toRemove) {
char* position = strstr(target, toRemove);
// Not sure if this is ok, since in general the ranges overlap
strcpy(position+strlen(toRemove), position);
}

Share this post


Link to post
Share on other sites
ya thanks for the advice Zahman, but the point of the excercise was not to use a std library function but to create your own

and this is "C"

anyway, i appreciate the help everyone

Share this post


Link to post
Share on other sites
Fine; look up how strstr, strcpy and strlen work, and see if you can't integrate them directly.

Then, having learned something from the experience, please don't ever torture yourself like this again. :)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!