Critique my trim function
Just out of the blue I decided to write a trim function, it took me a little while but eventually got it. Can anyone offer some tips on how to optimize it if possible. I feel like theres gotta be another way to offset the string and also do something differently than using that trimmed variable. Thanks.
Code:
#include <stdio.h>
#include <string.h>
void trim(char *string){
int idx, trimmed = 0;
for(idx = 0; idx < strlen(string); idx++){
if(string[idx] != ' ' || (string[idx] == ' ' && trimmed)){
string[trimmed++] = string[idx];
}
}
const int terminate = strlen(string) - (strlen(string) - trimmed);
string[terminate] = '\0';
}
int main(){
char string[] = " trim this string";
trim(string);
puts(string);
}