How efficient is this code?
Below is a function which takes a string and an integer as it's parameters, and returns a string. It's purpose is to remove the character at the position denoted by the integer, and return the remaining characters in the form of another string.
Code:
char *
extract_letter( char *word, int letter )
{
int ctr;
char *str;
str = (char *) malloc( strlen( word ) - 1);
for( ctr = 0 ; ctr < letter ; ctr++ )
str[ctr] = word[ctr];
for( ctr = letter ; ctr < strlen( word ) ; ctr++ )
str[ctr] = word[ctr+1];
return( str );
}
The calling function ensures that the value of (int letter) always relates to a valid position in (char *word).
The problem I've got is that this function is being called many many times during the excution of the program (potentially hundreds of millions of times), and I need to make sure that it is as fast as possible. Can anyone suggest a way of improving the speed of this function?
Also, I'm just guessing that this function might be a bottleneck, as I don't know how to work out what percentage of time the program takes to execute each part. Does anyone know of a way to measure this?
Thanks.