#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define TRUE    1
#define FALSE   0
void Result (char *result, char string, char remove);
char *RemoveChars( char *src
                 , char *key );
int main(void)
{
    char string[] = "Alesia";
    char remove[] = "aeiou";
    char result;
	Result (&result, string, remove);
	fflush(stdin);
	printf("  Press any key ...\n");	
	getchar();
	return 0;
}
void Result (char *result, char string, char remove)
{
	*result  = NULL;
	result = RemoveChars( string
                       , remove );
   printf( "The result is %s\n", result );
}
char *RemoveChars( char *src, char *key )
{
   char             *dest;
   int               found;
   int               i,j,k;
   i        = 0;
   j        = 0;
   k        = 0;
   dest     = NULL;
   dest = (char *) malloc( sizeof( char ) * strlen( src ) + 1 );
   memset( dest, 0x00, sizeof( char ) * strlen( src ) + 1 );
   for ( i = 0; i < strlen( src ); i++ ) {
      found = FALSE;
      for ( j = 0; j < strlen( key ); j++ ){
         if ( src[i] == key[j] )
            found = TRUE;
      }
      if ( FALSE == found ) {
         dest[k++] = src[i];	    
      }
   }
   return ( dest );
}

First of all, i am appoogize because this topic has been posted but
1. im trying to call the removechar function with another function but it doesnt work.
2. is there any other way that doesnt require the use of malloc in removechar function that easier to understand?
Im new to C so simple explaination is best for me
thanks for all your help and have a nice day. :)

Can you present the original specification of the "remove chars" stuff (not this wittingly incorrect implementation)?

Can you present the original specification of the "remove chars" stuff (not this wittingly incorrect implementation)?

char *RemoveChars( char *src
                 , char *key )
{

   char             *dest;
   size_t            len_src;
   size_t            len_key;
   int               found;
   int               i;
   int               j;
   int               k;

   /*
   ** Initialise
   */
   i        = 0;
   j        = 0;
   k        = 0;
   len_src  = 0;
   len_key  = 0;
   dest     = NULL;

   len_src = strlen( src );
   len_key = strlen( key );

   /*
   ** Allocate memory for the destination and initialise it
   */
   dest = (char *) malloc( sizeof( char ) * len_src + 1 );
   if ( NULL == dest )
   {
      printf("Unable to allocate memory\n");
      // DO EXCEPTION HANDLING HERE
   }

    memset( dest, 0x00, sizeof( char ) * len_src + 1 );

   /*
   ** MAIN LOOP. For each character in the source, we check against the key.
   ** We use the 'found' boolean to evaluate whether we need to copy or not.
   */
   for ( i = 0; i < len_src; i++ )
   {
      found = FALSE;
      for ( j = 0; j < len_key; j++ )
      {
         if ( src[i] == key[j] )
            found = TRUE;
      }

      /*
      ** Copy the character if it was NOT found in the key
      */
      if ( FALSE == found )
      {
         dest[k] = src[i];
         k++;
      }
   }

   /*
   ** Return the destination pointer to the main function
   */
   return ( dest );

}

That's the code i got from one of the post in this forum which is
http://www.daniweb.com/forums/thread15538.html

Well, and what do you want to do with this 5-years old slops?
Better think then implement the proper solution from the scratch.

HERE is how you do it:

use two pointers. an input and an output, both pointing to the start of the source string.

compare the input pointer character to each of your key characters. if you have a match, increment your input pointer only. if you dont have a match, then copy the input pointer character to the output pointer location, and increment BOTH pointers.

once you evaluate the last source character pointed to by the input pointer, NULL terminate the source string at the output pointer.

your source string now contains only the characters NOT matching any characters in your key. in other words, the key characters have all been removed from your source.


.

#include<stdio.h>
#include <string.h>
void remove(char *aft);
int main(void)
{
	char aft[10+1] = "Alesia";
	remove(aft);
	printf("[%s]\n\n", aft);

	fflush(stdin);
	printf("  Press any key ...\n");	
	getchar();
	return 0;

}
void remove(char *aft) 
{     
	char key[] = "aeiou";
	int i;
	int j;
	int k;
	k =0;
	j =0;
	i =0;
	for (i=j=0; aft[i] != '\0'; i++)
		for (k=0; key[k] != '\0'; k++)
			if ( aft[i] != key[k])
				aft[j++] = aft[i];
	aft[j] = '\0';
}

I'm trying to re-write a code for removingchar . however, the code above have no error but how come it doesnt show anything?
did i do sumthing wrong?

you're increasing j++ every time it doesnt make a single character match within the key, which is messing your count up.

and its a bad idea to incrment a for loop counter within the loop itself unless you know what youre doing.

you should increment your pointers only after you've checked every single key character.

if there was a match then only increment the "input" pointer that reads along the string.

if there wasnt any matches out of all the key characters, then and only then do you copy the the input character to the output character, and also increment BOTH pointers.

actually, the fact that you're incrementing j++ within the for loop is okay. i thought 'j' was a loop control variable.

but, still, you should only increment the index after you've checked all the key characters.

you pretty much had it done. so, here's what i did, keeping with your original attempt. if i had to write it from scratch, i would have used pointers rather than index, but i guess it's all about the same. also, note that it's not especially fault tolerant, but you get the idea.

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

void remove(char *myStr, char *myKey);

int main(void)
{
   char source[256];
   char key[40];

   printf("enter string to search: ");
   fgets(source,sizeof(source),stdin);

   printf("enter chars to remove:  ");
   fgets(key,sizeof(key),stdin);

   remove(source, key);
   printf("Result: [%s]\n\n", source);

   printf("  Press any key ...\n");
   getchar();
   return 0;

}
void remove(char *myStr, char * myKey)
{
   int in=0, out=0, k=0, match=0;

   for (in=0; myStr[in] != '\0'; in++)
   {
      match = 0;

      for (k=0; myKey[k] != '\0'; k++)
         if (myStr[in] == myKey[k])
            match = 1;

      if (match == 0)
         myStr[out++] = myStr[in];
   }

   myStr[out] = '\0';
}

PS: dont use "fflush(stdin)"


.

So I take a look at this problem, become interested in solving it, but without success.
Can anybody explain to me why this code does not work?
It leaves the original string untouched...

void remove(char *aft)
{
	char *key = "aeiouAEIOU";
	int i, j, k = 0;

	for ( i = 0; aft[i] != '\0'; i++ )
		for ( j = 0; key[j] != '\0'; j++ )
            		if ( aft[i] == key[j] ) continue;
            		else { aft[k++] = aft[i]; break; }

	aft[k] = '\0';
}

For what I know, those to are equal...?

char *a = "aeiou";
char a[] = "aeiou";

And it make no change, if i declare it as char a[].
Can not understand why. Interesting is, if want to remove any other char than vowel( the if would be like this: if ( aft == key[j] ) aft[k++] = aft; break; ) it works... heh

oh, sorry, i didnt really look at your code.

the problem is your use of continue, the logic is all screwed up.

you keep trying the key until you find a non-match at which point you copy the character. since you'll always find a non-match, you'll never skip any characters.

anyhow this thread is solved. i think we're done here.

For what I know, those to are equal...?

char *a = "aeiou";
char a[] = "aeiou";

In general terms, yes, they are equivalent. *a is a pointer that points to the string a[] is an array, and a itself is the address to the string -- effectively a pointer.
Though not quite the same thing, at this level they are interchangable.

This question has already been answered. Start a new discussion instead.