954,498 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

what function can delete a space in a string?

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

see_moonlight
Newbie Poster
7 posts since Oct 2004
Reputation Points: 10
Solved Threads: 0
 
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.

Tight_Coder_Ex
Posting Whiz in Training
215 posts since Feb 2005
Reputation Points: 47
Solved Threads: 17
 
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 >>

see_moonlight
Newbie Poster
7 posts since Oct 2004
Reputation Points: 10
Solved Threads: 0
 

>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.

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

[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

blahblahcoding
Newbie Poster
1 post since Oct 2009
Reputation Points: 8
Solved Threads: 0
 
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>

void remSpaces(char *ch){
    char *ch2 = ch;
    int i,j=0;

    for(i=0;i<=strlen(ch);i++){
        if(ch[i]!=' '){
            ch2[j] = ch[i];
            j++;
        }
    }
    printf(ch2);
}

int main()
{
    char *ch = malloc(256);
    printf("Enter your text :\n");
    gets(ch);
    remSpaces(ch);
    return 0;
}


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]email snipped[/email]

MrM_CodingCat
Newbie Poster
1 post since Nov 2010
Reputation Points: 10
Solved Threads: 0
 
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.

The last line should be:

Str [Dest] = 0;
/*
This would put the null right after the condensed text.
*/


not:

Str [Pntr] = 0;
/*
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.
*/
isaacws
Newbie Poster
1 post since Jan 2012
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You