题目
如题
思路(注意事项)
用指针
纯代码
#include<stdio.h>
void func(char* a){
char *p = a;
int i = 0;
while(*p == ' ') p ++;
while(*p){
a[i ++] = *p;
p ++;
}
a[i] = '\0';
}
int main(){
char str[20];
gets(str);
func(str);
puts(str);
return 0;
}
进阶(防止缓冲区溢出)
#include<stdio.h>
void func(char* a){
char *p = a;
int i = 0;
while(*p == ' ') p ++;
while(*p){
a[i ++] = *p;
p ++;
}
a[i] = '\0';
}
int main(){
char str[20];
fgets(str, sizeof(str), stdin);
str[strspn(str, "\n")] = '\0';
func(str);
puts(str);
return 0;
}