Hi,

I'm a junior computer science student and I'm currently new to this forum.
Im having some problems understanding the code excerpt from a book (see code excerpt below). The code is suppose to delete characters from a string, based on the given prototype:

void RemoveChars(char str[], char remove[]);


str[] contains the entire string e.g "Hello World" while remove contains the characters to be removed from str[] e.g remove[]={'e','o','r','d'}; Once removed str[] should now be: "Hll Wl"


void RemoveChars(char str[], char remove[]){
int srcIndex, destination, removeArray[256];


// Initialize all elements in the lookup array to be 0.
for(srcIndex=0; srcIndex<256; srcIndex++){
removeArray[srcIndex]=0;
}


//set true for all characters to be removed
srcIndex=0;
while(remove[srcIndex]){
removeArray[remove[srcIndex]]=1;
srcIndex++;
}


//copy chars unless it must be removed
srcIndex=destination=0;
do{
if(!removeArray[str[src]]){
str[destination++]=str[srcIndex];
}
}
while(str[srcIndex++]);
}

However, my problem is with understanding the statement :
removeArray[remove[srcIndex]]

My understanding of that statement is remove[srcIndex] returns the character to be removed, and that character is then used as an array subscript for the array removeArray[remove[srcIndex]] ??

My understanding of such array notations are such:

int size=10;
int grades={1,2,3,1,1,2,3,4,5,3};
int frequency[10]={0};


for(int i=0;i<size;i++){
++frequency[grades];
}

In this case, grades returns the numbers from the grades array i.e. 1,2,3, etc ...which is in turn used as an index to the frequency array to increment the freqeuncy of occurence for the particular grade.

Could anyone help me out in clarifying the earlier notation of removeArray[remove[srcIndex]] ?

Thanks
Danny

Recommended Answers

All 10 Replies

does the code work and compile? as i cant see either how a char can be used as a subscript.... unless... i do have an idea as it is passing a char, and the array is an array referring to the char set (256 chars) it may be a case that the char is being converted to an integer automatically and is a valid index as the RemoveArray is an array of the 256 char codes: In other words:

RemoveArray[char(55)]; if the 55th character happened to be 'a' (i dont know what it is) and
RemoveArray;

would apparently do the same thing, not having tried it myself i cant say which, if either, work but it seems that the char is being converted to a number.

Your understanding of the removeArray is not correct.
removeArray is set up as a 256 array set, which conforms to the possible values of one byte. One byte = 2 to the power 8, = 256. Thus, the possible values of a byte is 0 to 255, or 0x00 to 0xFF.

The removeArray merely is a lookup template. Since we know that we wish to remove letters 'e', 'o', 'r', and 'd', these translate to the ascii values 101, 111, 114, and 100. Thus the removeArray gets set up with 256 zeros, and in position 101, the value is 1. Eg. positions 109 to 115 will look like : ... 0,0,1,0,0,1,0 ....

Now, when we read our string, we check the character against this template. If it is set to 1, (or true), we do not copy it to the string, but simply read over it to the next character.

Strangely, though, in your example of the frequency, the template set up is in fact, correct!!!. However, note that the the frequency array index corresponds to the grade, and the contents at the index the frequency itself. It differs in the logic of the removeArray, but the concept of the template still holds true.

BTW, I must admit that this code is really crappy. Today, a developers time is MORE valuable than memory or CPU processing speed; thus, always write easy to read maintainable code!

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

#define TRUE    1
#define FALSE   0

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 );

}


void main()
{

   char string[] = "Hello World";
   char remove[] = "eord";
   char *result  = NULL;

   result = RemoveChars( string
                       , remove );

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

}

BTW, I must admit that this code is really crappy. Today, a developers time is MORE valuable than memory or CPU processing speed; thus, always write easy to read maintainable code!

Unfortunately, developers still do have to worry to some extent about CPU and memory usage. Thinks like memory leaks pop up, even if the code is easy to read and/or maintain. ;)

(you forgot to free() what you allocated)

Unfortunately, developers still do have to worry to some extent about CPU and memory usage. Thinks like memory leaks pop up, even if the code is easy to read and/or maintain. ;)

(you forgot to free() what you allocated)

There are still many cases where speed is more important than the programmer's time and salary. embedded programs for example require fastest running code possible, so programmer's can not do sloppy or inefficient work there.

There are still many cases where speed is more important than the programmer's time and salary. embedded programs for example require fastest running code possible, so programmer's can not do sloppy or inefficient work there.

Embedded systems is a great example. Huge scale applications are as well, e.g. Amazon.com or Google. Obviously, they have to achieve a balance of maintainability and performance, but they can't just throw resources around like it's no big deal. The amount of resources they consume already is, at least to me, amazing, yet they're still growing fairly rapidly, and always worrying about efficiency.

maybe this ? please review

void vRemoveDelimiters(char *pcBuffer, char* pcDelimiters)
{
   int j = 0;
   int i = 0;
   int iLen = strlen(pcBuffer);

   for(i = 0 ; i < iLen; i++)
   {
      if( strchr(pcDelimiters, pcBuffer[i]) != NULL )
      {
         j = i;
         if(strchr(pcDelimiters, pcBuffer[j + 1]) != NULL)//next is delimiter, start from this position agayn after
         {
            i--;
         }
         memcpy(&pcBuffer[j], &pcBuffer[j + 1], iLen - i);
         iLen--;
      }

   }

   return;
}

i think this should work any idea how to do better ?

void vRemoveDelimiters(char *pcBuffer, char* pcDelimiters)
{
   int i = 0;
   int iLen = strlen(pcBuffer);

   for(i = 0 ; i < iLen; i++)
   {
      if( strchr(pcDelimiters, pcBuffer[i]) != NULL )
      {
         memcpy(&pcBuffer[i], &pcBuffer[i + 1], iLen - i);
         iLen--;
         i--;
      }

   }
   return;
}

Guys i highly doubt that a junior computer science student will understand concepts like pointers when hes having problems with infering basic textbook examples... Lets just try to tone it down a tad bit so that people do get the answers to their solutions and dont end up getting confused.. The last thing he'd be worrying bout is efficiency and size of the program right now.. Try mentioning only whats required and is to the point..

After 7 years one would hope said junior computer student has been in the industry for a few years and can finally understand pointers... ;o)

Maybe it would be better if people stop resurrecting long dead threads with worthless posts.

After 7 years one would hope said junior computer student has been in the industry for a few years and can finally understand pointers... ;o)
Maybe it would be better if people stop resurrecting long dead threads with worthless posts.

sorry i needed fast solution and found this thread on google, maybe this answer will help to someone

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.