2012年12月17日 星期一

[Programming] 以單一空格取代重複空格

  1 #include <stdio.h>

  2 // This code transform multple blanks to single blank
  3 #define NONBLANK 'a'
  4
  5 int main(){
  6         int c=0;
  7         int lastBlank=NONBLANK;
  8         while((c=getchar())!=EOF){
  9                 if(' '!=c){
 10                         putchar(c);
 11                 }else{
 12                         if(lastBlank!=' '){
 13                                 putchar(c);
 14                         }
 15                 }
 16                 lastBlank=c;
 17         }
 18         return 0;
 19 }

沒有留言:

張貼留言