what function can delete a space in a string?

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Oct 2004
Posts: 7
Reputation: see_moonlight is an unknown quantity at this point 
Solved Threads: 0
see_moonlight see_moonlight is offline Offline
Newbie Poster

what function can delete a space in a string?

 
0
  #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
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 199
Reputation: Tight_Coder_Ex is an unknown quantity at this point 
Solved Threads: 14
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?

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

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

 
0
  #3
Mar 31st, 2005
  1. extern char* delete_space_from_string ( char *string /* I */ )
  2. {
  3.  
  4. char space_token[] = " ";
  5. char *temp_string = NULL;
  6. char *temp_result = NULL;
  7.  
  8. if ( string == NULL || *string == NULL )
  9. {
  10. return( temp_string );
  11. }
  12.  
  13. /* delete space */
  14.  
  15. temp_result = strtok( strip_ending_whitespace( string ), space_token );
  16.  
  17. temp_string = temp_result;
  18.  
  19. while( temp_result != NULL )
  20. {
  21. temp_result = strtok( NULL, space_token );
  22.  
  23. if ( temp_result == NULL )
  24. {
  25. break;
  26. }
  27.  
  28. temp_string = sprintf("%s%s",temp_string,temp_result);
  29. }
  30.  
  31. free(temp_result);
  32.  
  33. return( temp_string );
  34.  
  35. }

<< moderator edit: added [code][/code] tags >>
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,827
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 750
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Senior Bitch

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

 
0
  #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:
  1. 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.
New members chased away this month: 3
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 1
Reputation: blahblahcoding is an unknown quantity at this point 
Solved Threads: 0
blahblahcoding blahblahcoding is offline Offline
Newbie Poster
 
-1
  #5
Oct 13th, 2009
[code]
#include "stdafx.h"
#include "stdio.h"
#include "conio.h"

void delspace(char *Buf1, char* Buf2)
{
while(*Buf1 != '\0')
{
if((*Buf1) == 0x20)
{
++Buf1; // if space skip */
}
else
*Buf2++ = *Buf1++; // if not space copy to buf 2
}
*Buf2 = '\0'; //NULL Terminate

}
int main()
{
char buffer[50];
delspace("WTF this may have finally worked", &buffer[0]);
printf("\n%s\n", buffer);
getch();
}
[icode]

commenting on anothers code and not telling how simple this is just not worth anybodies time.

This has worked for me in VS and Dev c++

Cheers
~blahblahcoder
Reply With Quote Quick reply to this message  
Reply

Message:


Thread Tools Search this Thread



Tag cloud for C
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC