练习7

来源:互联网 发布:当今网络强国 编辑:程序博客网 时间:2024/05/01 23:37
 
  1. /*********************************************************************************
  2.   7. 读入一行文本,包含若干个单词(以空格间隔,%结尾)。将其中以 A 开头的
  3.   单词与以 N 结尾的单词,用头尾交换的办法予以置换。
  4.   例子:
  5.   Never frown even when you are sad because you never know who is falling in love with your smile
  6.   置换后为:
  7.   Never nrowf nvee nhew you era sad because you never know who is falling ni love with your smile
  8.   ********************************************************************************/
  9. #include <stdio.h>
  10. void main()
  11. {
  12.     char *s,*t;
  13.     char str[] = "Never frown even when you are sad because you never know who is falling in love with your smile%";
  14.     s = t = str;
  15.     
  16.     while(*t != '%')
  17.     {
  18.         while(*t == ' ')
  19.             printf("%c",*t++);
  20.         s = t;
  21.         while(*t != ' ' && *t != '%')
  22.             t++;
  23.         if(*s == 'a' || *(t-1) == 'n')
  24.         {
  25.             if(s != t-1)
  26.             {
  27.                 char temp;
  28.                 temp = *s;
  29.                 *s = *(t-1);
  30.                 *(t-1) = temp;
  31.             }
  32.         }
  33.         while(s != t)
  34.         {
  35.             printf("%c",*s++);
  36.         }
  37.     }
  38.     printf("/n");
  39. }
原创粉丝点击