Hey, I wrote this function to eliminate all leading and trailing spaces from a string..
Code:
void trim(char** str) {
char ptr[strlen(str)+1];
int i = 0, j = 0;
while (isspace(str[i]) == 1) {
i++;
}
while (str[i] != '\0') {
ptr[j++] = str[i++];
}
ptr[j] = '\0';
int n = strlen(ptr);
while (--n >= 0) {
if (isspace(ptr[n]) == 0) {
break;
}
}
ptr[n+1] = '\0';
str = ptr;
}
..and have a quick question about ways of using it, I know to call the above I should:
Code:
char *aString = " trim this string ";
trim(&aString);
right? due to the passing by reference thing.. but i was thinking if the following changes were made, the result will be exactly the same? just hoping someone can affirm my thoughts.
Code:
char *trim(char* str) {
/* trim code.. */
return str;
}
aString = trim(aString);
is one way better style than the other?
thanks.