943,923 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 43761
  • C RSS
Dec 14th, 2004
0

Removing characters from a string

Expand Post »
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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
dannyfang is offline Offline
8 posts
since Dec 2004
Dec 14th, 2004
0

Re: Removing characters from a string

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.
Reputation Points: 16
Solved Threads: 6
Posting Pro in Training
1o0oBhP is offline Offline
445 posts
since Dec 2004
Dec 7th, 2006
0

Re: Removing characters from a string

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. }
Reputation Points: 10
Solved Threads: 0
Newbie Poster
mkadwa is offline Offline
2 posts
since Dec 2006
Dec 7th, 2006
0

Re: Removing characters from a string

Click to Expand / Collapse  Quote originally posted by mkadwa ...
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)
Reputation Points: 683
Solved Threads: 53
Posting Virtuoso
Infarction is offline Offline
1,580 posts
since May 2006
Dec 7th, 2006
0

Re: Removing characters from a string

Click to Expand / Collapse  Quote originally posted by Infarction ...
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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,953 posts
since Aug 2005
Dec 7th, 2006
0

Re: Removing characters from a string

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.
Reputation Points: 683
Solved Threads: 53
Posting Virtuoso
Infarction is offline Offline
1,580 posts
since May 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: Removing found chars from string
Next Thread in C Forum Timeline: activating VC6 debugger from CGI prog





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC