| | |
what function can delete a space in a string?
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
C Syntax (Toggle Plain Text)
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:
Solved Threads: 0
C Syntax (Toggle Plain Text)
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:
C Syntax (Toggle Plain Text)
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.
New members chased away this month: 3
•
•
Join Date: Oct 2009
Posts: 1
Reputation:
Solved Threads: 0
-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
#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
![]() |
Similar Threads
- how to remove space from string? (PHP)
- Help with delete blank from string function PLEASE!!!! (C)
- Creating a Basic String Database (C++)
- remove unnecesary space from a string (C++)
- no matching function for call to 'strcmp(std::string&, std::string&)' (C++)
Other Threads in the C Forum
- Previous Thread: Stack Program help
- Next Thread: i need help 2 solve dis problem
| Thread Tools | Search this Thread |
Tag cloud for C
adobe ansi api array arrays asterisks binarysearch calculate centimeter char command convert copyimagefile copypdffile cprogramme creafecopyofanytypeoffileinc createcopyoffile csyntax directory drawing dynamic executable fflush file fork forloop frequency getlasterror givemetehcodez graphics gtkgcurlcompiling hacking hardware highest homework i/o inches incrementoperators infiniteloop kernel km lazy linked linkedlist linux linuxsegmentationfault list lists locate logical_drives match matrix microsoft motherboard multi mysql number open opendocumentformat opensource owf pattern pdf performance pointer pointers posix problem probleminc program programming radix recursion recv repetition research scanf scheduling scripting segmentationfault send sequential shape socketprograming spoonfeeding stack standard string strings structures student systemcall testautomation turboc unix user variable voidmain() wab windows.h






