0

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

6
Contributors
6
Replies
14
Views
11 Years
Discussion Span
Last Post by isaacws
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.

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

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.

-1
#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();
}

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

Edited by mike_2000_17: Fixed formatting

Votes + Comments
Reviving a 4-year-old thread to post really crappy code?
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]

Edited by WaltP: Do NOT post email addresses on forums!

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.
*/
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.