Please support our C advertiser: Programming Forums
Views: 6902 | Replies: 3
![]() |
•
•
Join Date: Feb 2005
Location: 55 26'N 118 46'W
Posts: 184
Reputation:
Rep Power: 4
Solved Threads: 13
void delspace (char *Str)
{
int Pntr = 0;
int Dest = 0;
while (Str [Pntr])
{
if (Str [Pntr] != ' ')
Str [Dest++] = Str [Pntr];
Pntr++;
}
Str [Pntr] = 0;
}•
•
Join Date: Oct 2004
Posts: 7
Reputation:
Rep Power: 0
Solved Threads: 0
extern char* delete_space_from_string ( char *string /* I */ )
{
char space_token[] = " ";
char *temp_string = NULL;
char *temp_result = NULL;
if ( string == NULL || *string == NULL )
{
return( temp_string );
}
/* delete space */
temp_result = strtok( strip_ending_whitespace( string ), space_token );
temp_string = temp_result;
while( temp_result != NULL )
{
temp_result = strtok( NULL, space_token );
if ( temp_result == NULL )
{
break;
}
temp_string = sprintf("%s%s",temp_string,temp_result);
}
free(temp_result);
return( temp_string );
}<< moderator edit: added [code][/code] tags >>
>extern char* delete_space_from_string ( char *string /* I */ )
extern is redundant. All function declarations are extern in C by default.
>if ( string == NULL || *string == NULL )
NULL should only be used in pointer context:
>free(temp_result);
This is effectively a no-op at all times. If you get here then temp_result is NULL. Otherwise, it would be a ghastly error because the function has no way of knowing whether string was dynamically allocated.
You also neglect to show strip_ending_whitespace, but its very presence strikes home the fact that your solution is bloated and overkill for such a simple operation. As an exercise in the use of strtok and sprintf, it's okay, but as a real solution to the problem of stripping whitespace, you can do much better.
extern is redundant. All function declarations are extern in C by default.
>if ( string == NULL || *string == NULL )
NULL should only be used in pointer context:
if ( string == NULL || *string == '\0' )
This is effectively a no-op at all times. If you get here then temp_result is NULL. Otherwise, it would be a ghastly error because the function has no way of knowing whether string was dynamically allocated.
You also neglect to show strip_ending_whitespace, but its very presence strikes home the fact that your solution is bloated and overkill for such a simple operation. As an exercise in the use of strtok and sprintf, it's okay, but as a real solution to the problem of stripping whitespace, you can do much better.
I'm here to prove you wrong.
![]() |
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)






Linear Mode