Chinaunix首页 | 论坛 | 博客
  • 博客访问: 3598
  • 博文数量: 1
  • 博客积分: 67
  • 博客等级: 民兵
  • 技术积分: 20
  • 用 户 组: 普通用户
  • 注册时间: 2009-11-25 23:11
文章分类
文章存档

2012年(1)

我的朋友
最近访客

分类: C/C++

2012-05-17 21:30:59


题目如下: 写一个函数,将字符串翻转,翻转方式如下:“I am a student”反转成“student a am I”,不借助任何库函数。

 方法1(自己写的)  #include


点击(此处)折叠或打开

  1. #include<stdlib.h>
  2.  char* reserve(char* str);
  3.  int get_length(char *str);
  4.  int main(void)
  5.  {
  6.  char *str="I am a student";
  7.  str="you are a student";
  8.  char *result=reserve(str);
  9.  printf("%s\n",result);
  10.  free(result);
  11.  result=NULL;
  12.  str=NULL;
  13.  return 0;
  14.  }
  15.  int get_length(char *str)
  16.  {
  17.  int length=-1;
  18.  if(str!=NULL)
  19.  {
  20.  length=0;
  21.  while(*str++!='\0')
  22.  {
  23.  length++;
  24.  }
  25.  }
  26.  return length;
  27.  }
  28.  char* reserve(char* str)
  29.  {
  30.  int i=get_length(str);
  31.  printf("%d\n",i);
  32.  char* result=malloc(sizeof(char)*(i+1));
  33.  if(result==NULL)
  34.  {
  35.  printf("%s\n","allocate string failed");
  36.  exit(1);
  37.  }
  38.  char* temp=malloc(sizeof(char)*i);
  39.  if(temp==NULL)
  40.  {
  41.  printf("%s\n","allocate string failed");
  42.  exit(2);
  43.  }
  44.  char c='\0';
  45.  int n=i-1;
  46.  int m=0;
  47.  c=*str;
  48.  while(c!='\0')
  49.  {
  50.  if( ')
  51.  {
  52.  temp[m++]=c;
  53.  }
  54.  else
  55.  {
  56.  for(m=m-1;m>=0;m--)
  57.  {
  58.  result[n--]=temp[m];
  59.  }
  60.  result[n--]=c;
  61.  m=0;
  62.  }
  63.  str++;
  64.  c=*str;
  65.  }

  66.  for(m=m-1;m>=0;m--)
  67.  {
  68.  result[n--]=temp[m];
  69.  }
  70.  free(temp);
  71.  temp=NULL;
  72.  return result;
  73.  }


方法2(从网上收集过来的)

点击(此处)折叠或打开

  1. #include <stdio.h>

  2.  void main()
  3.  {
  4.  char str[]="you are a student";
  5.  printf(str);
  6.  printf("\n");

  7.  char *p,*q;
  8.  char temp;
  9.  p=q=str;
  10.  while(*q!='\0')
  11.  {
  12.  q++;
  13.  }
  14.  q--;
  15.  while(p<=q)
  16.  {
  17.  temp=*p;
  18.  *p=*q;
  19.  *q=temp;
  20.  p++;
  21.  q--;
  22.  }//反转整个字符串

  23.  printf(str);
  24.  printf("\n");

  25.  char *s;
  26.  q=p=s=str;//指针指向开始位置
  27.  while(*q!='\0')
  28.  {
  29.  if(*q==' '||*(q+1)=='\0')
  30.  {
  31.  p--;
  32.  if(*(q+1)=='\0')//处理最后一个字串
  33.  p++;
  34.  while(s<=p)
  35.  {
  36.  temp=*p;
  37.  *p=*s;
  38.  *s=temp;
  39.  s++;
  40.  p--;
  41.  }//反转局部字符串

  42.  s=q+1;
  43.  p=q;
  44.  }
  45.  q++;
  46.  p++;
  47.  }

  48.  printf(str);
  49.  printf("\n");
  50.  }




方法3(传说中是《程序员面试宝典》里的方法,不过我没有看过这本书,也没去验证): #include
 

点击(此处)折叠或打开

  1. #include <stdio.h>

  2.  int main(void)
  3.  {
  4.  int num=-12345,j=0,i=0,flag=0,begin,end;
  5.  char str[]="I am a student",temp;
  6.  j=strlen(str)-1;

  7.  printf(" string=%s\n",str);
  8.  //第一步是进行全盘反转,将单词变成“tneduts a ma I”
  9.  while(j>i)
  10.  {
  11.  temp=str[i];
  12.  str[i]=str[j];
  13.  str[j]=temp;
  14.  j--;
  15.  i++;
  16.  }
  17.  printf(" string=%s\n",str);
  18.  int i=0;
  19.  //第二步进行部分反转,如果不是空格则开始反转单词
  20.  while(str[i])
  21.  {
  22.  if(str[i]!=' ')
  23.  {
  24.  begin=i;
  25.  while(str[i]&&str[i]!=' ')
  26.  {
  27.  i++;
  28.  }
  29.  i=i-1;
  30.  end=i;
  31.  }
  32.  while(end>begin)
  33.  {
  34.  temp=str[begin];
  35.  str[begin]=str[end];
  36.  str[end]=temp;
  37.  end--;
  38.  begin++;
  39.  }
  40.  i++;
  41.  }
  42.  printf(" string=%s\n",str);
  43.  return 0;
  44.  }

阅读(579) | 评论(1) | 转发(0) |
0

上一篇:没有了

下一篇:没有了

给主人留下些什么吧!~~

gjf05_052012-05-26 18:52:12

只觉得第三种方法简单。第一个好像不能用。(可能是我没有耐心看那么复杂的吧。)