Removing characters from a string

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Dec 2004
Posts: 8
Reputation: dannyfang is an unknown quantity at this point 
Solved Threads: 0
dannyfang dannyfang is offline Offline
Newbie Poster

Removing characters from a string

 
0
  #1
Dec 14th, 2004
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[size]={1,2,3,1,1,2,3,4,5,3};
int frequency[10]={0};

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

In this case, grades[i] 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
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 445
Reputation: 1o0oBhP is an unknown quantity at this point 
Solved Threads: 6
1o0oBhP's Avatar
1o0oBhP 1o0oBhP is offline Offline
Posting Pro in Training

Re: Removing characters from a string

 
0
  #2
Dec 14th, 2004
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['a'];

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.
http://sales.carina-e.com

no www
no nonsense

coming soon to a pc near you! :cool:
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 2
Reputation: mkadwa is an unknown quantity at this point 
Solved Threads: 0
mkadwa mkadwa is offline Offline
Newbie Poster

Re: Removing characters from a string

 
0
  #3
Dec 7th, 2006
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!

  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. #define TRUE 1
  5. #define FALSE 0
  6.  
  7. char *RemoveChars( char *src
  8. , char *key )
  9. {
  10.  
  11. char *dest;
  12. size_t len_src;
  13. size_t len_key;
  14. int found;
  15. int i;
  16. int j;
  17. int k;
  18.  
  19. /*
  20.   ** Initialise
  21.   */
  22. i = 0;
  23. j = 0;
  24. k = 0;
  25. len_src = 0;
  26. len_key = 0;
  27. dest = NULL;
  28.  
  29. len_src = strlen( src );
  30. len_key = strlen( key );
  31.  
  32. /*
  33.   ** Allocate memory for the destination and initialise it
  34.   */
  35. dest = (char *) malloc( sizeof( char ) * len_src + 1 );
  36. if ( NULL == dest )
  37. {
  38. printf("Unable to allocate memory\n");
  39. // DO EXCEPTION HANDLING HERE
  40. }
  41.  
  42. memset( dest, 0x00, sizeof( char ) * len_src + 1 );
  43.  
  44. /*
  45.   ** MAIN LOOP. For each character in the source, we check against the key.
  46.   ** We use the 'found' boolean to evaluate whether we need to copy or not.
  47.   */
  48. for ( i = 0; i < len_src; i++ )
  49. {
  50. found = FALSE;
  51. for ( j = 0; j < len_key; j++ )
  52. {
  53. if ( src[i] == key[j] )
  54. found = TRUE;
  55. }
  56.  
  57. /*
  58.   ** Copy the character if it was NOT found in the key
  59.   */
  60. if ( FALSE == found )
  61. {
  62. dest[k] = src[i];
  63. k++;
  64. }
  65. }
  66.  
  67. /*
  68.   ** Return the destination pointer to the main function
  69.   */
  70. return ( dest );
  71.  
  72. }
  73.  
  74.  
  75. void main()
  76. {
  77.  
  78. char string[] = "Hello World";
  79. char remove[] = "eord";
  80. char *result = NULL;
  81.  
  82. result = RemoveChars( string
  83. , remove );
  84.  
  85. printf( "The result is %s\n", result );
  86.  
  87. }
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 1,580
Reputation: Infarction has a spectacular aura about Infarction has a spectacular aura about Infarction has a spectacular aura about 
Solved Threads: 52
Infarction's Avatar
Infarction Infarction is offline Offline
Battle Programmer

Re: Removing characters from a string

 
0
  #4
Dec 7th, 2006
Originally Posted by mkadwa View Post
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)
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,437
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1473
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is online now Online
Still Learning

Re: Removing characters from a string

 
0
  #5
Dec 7th, 2006
Originally Posted by Infarction View Post
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.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 1,580
Reputation: Infarction has a spectacular aura about Infarction has a spectacular aura about Infarction has a spectacular aura about 
Solved Threads: 52
Infarction's Avatar
Infarction Infarction is offline Offline
Battle Programmer

Re: Removing characters from a string

 
0
  #6
Dec 7th, 2006
Originally Posted by Ancient Dragon View Post
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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC