RSS Forums RSS
Please support our C advertiser: Programming Forums
Views: 6902 | Replies: 3
Reply
Join Date: Oct 2004
Posts: 7
Reputation: see_moonlight is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
see_moonlight see_moonlight is offline Offline
Newbie Poster

Solution what function can delete a space in a string?

  #1  
Mar 29th, 2005
what function can delete a space in a string?

i want to find a c function , e.g. delspace(char *string)

string = "abc d e"

and

delspace(string)-> "abcde"


thanks, i want C function
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Feb 2005
Location: 55 26'N 118 46'W
Posts: 184
Reputation: Tight_Coder_Ex is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 13
Tight_Coder_Ex's Avatar
Tight_Coder_Ex Tight_Coder_Ex is offline Offline
Junior Poster

Re: what function can delete a space in a string?

  #2  
Mar 29th, 2005
void delspace (char *Str)
{
int Pntr = 0;
int Dest = 0;
 
while (Str [Pntr])
{
if (Str [Pntr] != ' ')
	Str [Dest++] = Str [Pntr];
Pntr++;
}
 
Str [Pntr] = 0;
}
Think in terms of how you would have to do something manually and then coding it usually straight forward.
Reply With Quote  
Join Date: Oct 2004
Posts: 7
Reputation: see_moonlight is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
see_moonlight see_moonlight is offline Offline
Newbie Poster

Re: what function can delete a space in a string?

  #3  
Mar 31st, 2005
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 >>
Reply With Quote  
Join Date: Sep 2004
Posts: 6,580
Reputation: Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of 
Rep Power: 31
Solved Threads: 499
Super Moderator
Narue's Avatar
Narue Narue is online now Online
Expert Meanie

Re: what function can delete a space in a string?

  #4  
Mar 31st, 2005
>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:
if ( string == NULL || *string == '\0' )
>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.
I'm here to prove you wrong.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes
Forums | Blogs | Tutorials | Code Snippets | Whitepapers | RSS Feeds | Advertising
All times are GMT -4. The time now is 12:15 pm.
Newsletter Archive - Sitemap - Privacy Statement - Acceptable Use Policy - Contact Us
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC