| | |
Removing characters from a string
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Dec 2004
Posts: 8
Reputation:
Solved Threads: 0
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
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
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.
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:
no www
no nonsense
coming soon to a pc near you! :cool:
•
•
Join Date: Dec 2006
Posts: 2
Reputation:
Solved Threads: 0
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!
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!
C Syntax (Toggle Plain Text)
#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!

(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.
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.
![]() |
Similar Threads
- Capitalizing all characters of a string (C)
- Adding in escape characters into a string (C#)
- Count characters of a string. (C++)
- Removing punctuation from a string (C++)
- Counting all Non-blank Characters in a String (Java)
- Counting specific characters in a string (C)
Other Threads in the C Forum
- Previous Thread: Removing found chars from string
- Next Thread: activating VC6 debugger from CGI prog
| Thread Tools | Search this Thread |
#include * ansi append array arrays asterisks bash binarysearch centimeter changingto char character convert copyimagefile cprogramme creafecopyofanytypeoffileinc database dynamic execv feet fgets file floatingpointvalidation fork framework function getlogicaldrivestrin givemetehcodez grade gtkwinlinux hacking histogram ide inches include incrementoperators infiniteloop initialization input interest intmain() iso kernel keyboard kilometer license linked linkedlist linux list lists locate looping lowest matrix meter microsoft number oddnumber opendocumentformat openwebfoundation overwrite owf pdf pointer posix power probleminc process program programming radix recursion recv recvblocked research reversing segmentationfault sequential single socket socketprograming socketprogramming standard strchr string suggestions systemcall test testing threads turboc unix urboc user variable wab whythiscodecausesegmentationfault windowsapi






