简介
通过 realloc
实现 将字符串开头的空白符删除。
示例代码
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char* nospace(char* str){
char* old = str;
char* new = str;
while(*old == ' ' ){ // 跳过空格
old++;
}
while(*old){ // 将 old 的每一个字符赋值给 new
*(new++) = *(old++);
}
*new = 0;
return (char*) realloc(str, strlen(str) + 1);
}
int main(){
char* buffer = (char*) malloc(strlen(" hdu") + 1);
strcpy(buffer, " hdu");
printf("%s\n", nospace(buffer));
system("pause");
return 0;
}
输入: hdu
输出:hdu
See more
realloc: https://msdn.microsoft.com/zh-cn/library/xbebcx7d.aspx