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