Skip to content

Instantly share code, notes, and snippets.

@maplebeats
Created September 18, 2014 16:04
Show Gist options
  • Save maplebeats/c3030886fe503140601a to your computer and use it in GitHub Desktop.
Save maplebeats/c3030886fe503140601a to your computer and use it in GitHub Desktop.
删除空格
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
int strlen(char * str)
{
int i=0;
while(str[i]!='\0')
i++;
return i+1;
}
int strcpy(char *Rstr, char *Dstr)
{
for(int i=0;i<strlen(Rstr);i++)
{
Dstr[i]=Rstr[i];
}
return 0;
}
int remove_space(char *Pstr, int len)
{
int num=0;
int i;
for(i=0;i<len;i++)
{
if(Pstr[i] == ' ')
{
num++;
}
else
{
printf("str::%c--->%c\n",Pstr[i-num],Pstr[i]);
Pstr[i-num] = Pstr[i];
}
}
Pstr[len-num]='\0';
return num;
}
int main(int argc, char *argv[])
{
assert(argc!=1);
char * str = argv[1];
int len = strlen(str);
char * string = (char *) malloc(len);
strcpy(str, string);
int space = remove_space(string, len);
printf("space:%d\n", space);
printf("string:%s\n", string);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment