笔试题

删除字符串中的多余空白,包含前,中,尾。
“ I am a boy . ”->"I am a boy" /* *删除多余的空白,包括前面,后面,中间 *" I am a boy . " => "I am a boy ." */ #include <stdio.h> #include <string.h> #define NUM_Max 1000 void back_space(char str[],char outstr[],int n,int len){ int space_num=0; int i=0; int j=0; while (j<len) { if (str[j]==' ') { if(i==0){ ++j; }else { if (space_num!=0) { ++j; }else { outstr[i++]=' '; ++space_num; ++j; } } }else{ space_num=0; outstr[i++]=str[j]; j++; } } } int main(void){ char str[]=" I am a boy . "; printf("%s\n",str); char outstr[NUM_Max]; back_space(str,outstr,NUM_Max,sizeof(str)); printf("%s\n",outstr); getchar(); return 0; }

  

posted @ 2014-10-30 22:42  cc_jony  阅读(161)  评论(0编辑  收藏  举报