![]() |
| ||
| Removing characters from a string 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 |
| ||
| 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. |
| ||
| 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! #include <stdio.h> |
| ||
| Re: Removing characters from a string Quote:
(you forgot to free() what you allocated) |
| ||
| Re: Removing characters from a string Quote:
|
| ||
| Re: Removing characters from a string Quote:
|
| All times are GMT -4. The time now is 5:41 pm. |
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC