腾讯在线笔试题-把字符串“I am from china.”反转成为“I am from china.”,以及把整个字符串逆序。


注:首先得说一下腾讯2016年在线笔试题的最后一个大题出题的人大意出错了,他举的例子是:字符串“I am from china.”反转成为“I am from china.”  但是他说的是表达符号又不反转,如果表达符号不转换的话,那么他举的例子就是错的,所以当时做这题的时候就真是一万个草泥马(不好意思说脏话)没忍住,下面就是给出字符串也转换的例子

  1#include <stdio.h>
  2 #include <string.h>
  3 #include <stdlib.h>
  4 #define SIZE 1024
  5 
  6 void swap(char *first,char *last)
  7 {
  8     char tmp=*first;
  9     *first=*last;
 10     *last=tmp;
 11 }
 12 void change(char *first,char *last)
 13 {
 14     char *p_first=first;
 15     char *p_last=last;
 16     while(p_first < p_last)
 17     {
 18         swap(p_first,p_last);
 19         p_first++;
 20         p_last--;
 21     }
 22 }
 23 void rever(char *str)
 24 {
 25     if(NULL==str)
 26         return;
 27     int len=strlen(str);
 28     char *last=str+len-1;
 29     char *first=str;
 30     change(first,last);
 31 //  while(first < last)
 32 //  {
 33 //      swap(first,last);
 34 //      first++;
 35 //      last--;
 36 //  }
 37     char *cur=str;
 38     char *tmp=str;
 39     while(*cur !='\0')
 40     {
 41         if(*cur==' ' || *(cur+1)=='\0')
 42         {
 43             change(tmp,cur-1);
 44             cur++;
 45             tmp=cur;
 46             continue;
 47         }
 48         cur++;
 49     }
 50 }
 51 void print_log(char *str)
 52 {
 53     if(NULL==str)
 54         return;
 55     printf("%s\n",str);
 56 }
 57 int main()
 58 {
 59 //  char arr[]="i am child.";
 60 //  char *str=(char*)malloc(sizeof(str)*SIZE);
 61 //  memset(str,'\0',SIZE*sizeof(str));
 62 //
 63 //  char *log="please enter char:";
 64 //  print_log(log);
 65 //  fgets(str,SIZE,stdin);
 66     char ptr[]="I am from china.";
 67     print_log(ptr);
 68     rever(ptr);
 69     print_log(ptr);
 70     return 0;
 71 }


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值