944,149 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 21726
  • C RSS
Mar 29th, 2005
0

what function can delete a space in a string?

Expand Post »
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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
see_moonlight is offline Offline
7 posts
since Oct 2004
Mar 29th, 2005
0

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

  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.
Reputation Points: 47
Solved Threads: 17
Posting Whiz in Training
Tight_Coder_Ex is offline Offline
215 posts
since Feb 2005
Mar 31st, 2005
0

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

  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 >>
Reputation Points: 10
Solved Threads: 0
Newbie Poster
see_moonlight is offline Offline
7 posts
since Oct 2004
Mar 31st, 2005
0

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

>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.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Oct 13th, 2009
-1
Re: what function can delete a space in a string?
[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
Reputation Points: 8
Solved Threads: 0
Newbie Poster
blahblahcoding is offline Offline
1 posts
since Oct 2009
Nov 20th, 2010
0
Re: what function can delete a space in a string?
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <ctype.h>
  4.  
  5. void remSpaces(char *ch){
  6. char *ch2 = ch;
  7. int i,j=0;
  8.  
  9. for(i=0;i<=strlen(ch);i++){
  10. if(ch[i]!=' '){
  11. ch2[j] = ch[i];
  12. j++;
  13. }
  14. }
  15. printf(ch2);
  16. }
  17.  
  18. int main()
  19. {
  20. char *ch = malloc(256);
  21. printf("Enter your text :\n");
  22. gets(ch);
  23. remSpaces(ch);
  24. return 0;
  25. }

This has worked for me in C.

This is a sollution I came down to on my own after trying other ones.
Hope this helps!

email snipped
Last edited by WaltP; Nov 22nd, 2010 at 4:54 am. Reason: Do NOT post email addresses on forums!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
MrM_CodingCat is offline Offline
1 posts
since Nov 2010
30 Days Ago
0

I like the simplicity of this but there is a small bug in this code...

  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.
The last line should be:
  1. Str [Dest] = 0;
  2. /*
  3. This would put the null right after the condensed text.
  4. */
not:
  1. Str [Pntr] = 0;
  2. /*
  3. This would put the null at the spot it was originally at ... not what you want as it leaves behind what ever chars that were not covered up by the chars that were moved.
  4. */
Reputation Points: 10
Solved Threads: 0
Newbie Poster
isaacws is offline Offline
1 posts
since Jan 2012
Message:
Previous Thread in C Forum Timeline: just want an initialization of an problem
Next Thread in C Forum Timeline: Handeling errors with atoi()





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC