C语言字符串去空格(最简单版本)

#include

char* despace(char *a){
     
	int i=0;char *b=a;
	while(a[i++]!='\0'){
     
		if(a[i]==' '){
     
			for(int j=i;j>=1;j--){
     
				a[j]=a[j-1];
			}
			b++;
		}
	}
	return b;
}

int main(){
     
	char a[]="a b cd efg";//不可以用指针,会指向常量区字符串,导致可以访问但无法改变
	puts(a);
	printf("%s\n",despace(a));
	return 0;
}

你可能感兴趣的:(算法问题,实例,字符串,c语言,指针)