New to the forum and C. I am trying to write a function that will allow me to remove a certian string from and exisiting one. The below program I wrote by using examples in an text book and online. I understand it at a basic level, but I'm pretty lost when it comes to figuring out how the difference array work with one another. Could any one give me some help in getting this going, and maybe point me in the direction of a good piece that explains this in idiot language.

Thanks

#include <stdio.h>

void removeString (char string[], char remove[])
{
	int index, source, numchar[81];

	for(index = 0; index < 80, index++;)
	{
		numchar[index]= 0;
	}

	index = 0;
	while(remove[index]) {

		numchar[remove[index]] = 1;
		index++;

	}

	index = source = 0;

	do
	{

		if(! numchar[string[index]])
		{
			string[source++] = string[index];
		}
	}

	while(string[index++]); 
}


int main (void)
{

	char string[] = "Hello World";
	char remove[] = "World";
	char result = 0; 
		
	removeString(string, remove);

	printf ("The Result is %s\n", result);

}

Recommended Answers

All 4 Replies

Well the first thing I would get working is a function that finds a c-string in a c-string. Once that's working then you can work on removing the characters.

A side note - Are trying to accomplish this without the string library?

Well the first thing I would get working is a function that finds a c-string in a c-string. Once that's working then you can work on removing the characters.

A side note - Are trying to accomplish this without the string library?

Yes, we haven't learned how to use the string libary... Would this make my life easier? Because this whole function thing is not making sense to me.

Yes, we haven't learned how to use the string libary... Would this make my life easier? Because this whole function thing is not making sense to me.

Using the string.h library would indeed make life easier but would invalidate the exercise..The idea is to understand, at least superficially, how to create this type of functionality.

This is how I would approach this problem

Suppose I have to remove the string "tes" from United States of America

1. Check to see if I can find a t any where in United states of America
2. If do find a t then check for e and then s
3. Once I find the start index of the match, in this case 10 the end index has to be 13
4. Then from index 14 till the end of the string I copy chars one by one to positions 10,11,12 ....

I guess the tricky part of this exercise is identifying when you have a string match. Check out this algorithm. Might be of help

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.