上面一道京东今年的面试题,可惜我没来得及参加笔试!如果有机会真想再去试一次!这里私下拿来做试试!if else那段写的不好,想到好的方式的可以告诉我!
上代码!有不合适的地方是我不知道后面那段废弃的数组要怎么处理!
#include <stdio.h>
#include <string.h>
void FormatString(char str[], int len){
if(str == NULL || len <= 0)
return;
int star = 0;
int pre = 0;
bool isSpace = false;
for(; pre < len+1; ++pre){
if(str[pre] == ' '){
isSpace = true;
}
else{
if(isSpace && str[pre] == '\0'){
str[star++] = str[pre];
}
else if(isSpace && star !=0){
str[star++] = ' ';
str[star++] = str[pre];
}
else{
str[star++] = str[pre];
}
isSpace = false;
}
}
for(;star < len; ++star){
str[star] = ' ';
}
}
int main(){
char str[]=" i am a little girl. ";
int len = strlen(str);
FormatString(str,len);
int p2=0;
while(str[p2]!='\0'){
printf("%c", str[p2++]);
}
printf("%s","end");
return 0;
}