反转字符串I am a student

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


方法是先反转整个字符串,然后再反转字串。譬如先将“I am a student”反转为“tneduts a ma I”,然后再对每个字串(空格分割)反转一次。


算法

 1 #include <stdio.h>
 2 
 3 void main()
 4 {
 5     char str[]="I am a student";

 7     printf(str);
 8     printf("\n");
 9 
10     char *p,*q;
11     char temp;
12     p=q=str;14     while(*q!='\0')
15     {
16         q++;
17     }
18     q--;
19     while(p<=q)
20     {
21         temp=*p;
22         *p=*q;
23         *q=temp;
24         p++;
25         q--;
26     }//反转整个字符串
27 
28     printf(str);
29     printf("\n");
30 
31     q=str;//指针指向开始位置
32     char *s,*t;
33     s=t=str;
34     while(*q!='\0')
35     {
36         if(*q==' ')
37         {
38             t--;
39             while(s<=t)
40             {
41                 temp=*t;
42                 *t=*s;
43                 *s=temp;
44                 s++;
45                 t--;
46             }//反转局部字符串
47 
48             s=q+1;
49             t=q;
50         }52         q++;
53         t++;
54     }
55 
56     printf(str);
57     printf("\n");
58 }

改进

运行之后,我发现是成功的。

但是怎么想都感觉有点问题,把“I am a student”换成“you are a student”果然有问题。

没有处理最后一个字串的缘故。因为我是按照

if(*q==' ')

来处理字串的,而字符串最后一个的结尾没有空格了,而是以'\0'结尾的。

最后一个字串的处理我是这样做的。

if(*q==' '||*(q+1)=='\0')
        {
            t--;
            if(*(q+1)=='\0')//处理最后一个字串
                t++;

看上去有点奇怪吧,但是确实是可以了。

代码貌似可以继续优化吧。怎么都感觉自己写的代码好烂。

代码

以下是完整代码

复制代码
#include <stdio.h>

void main()
{
    char str[]="you are a student";
    printf(str);
    printf("\n");

    char *p,*q;
    char temp;
    p=q=str;
    while(*q!='\0')
    {
        q++;
    }
    q--;
    while(p<=q)
    {
        temp=*p;
        *p=*q;
        *q=temp;
        p++;
        q--;
    }//反转整个字符串

    printf(str);
    printf("\n");

    char *s;
    q=p=s=str;//指针指向开始位置
    while(*q!='\0')
    {
        if(*q==' '||*(q+1)=='\0')
        {
            p--;
            if(*(q+1)=='\0')//处理最后一个字串
                p++;
            while(s<=p)
            {
                temp=*p;
                *p=*s;
                *s=temp;
                s++;
                p--;
            }//反转局部字符串

            s=q+1;
            p=q;
        }
        q++;
        p++;
    }

    printf(str);
    printf("\n");
}
复制代码

 

另外给一个我在《程序员面试宝典》看到的代码,不过这个主要采用数组处理,而且使用了库函数(strlen()),但是思想差不多吧。可以参考参考。

《程序员面试宝典》实现方法
复制代码
#include <iostram>
#include <stdio.h>

int main(void)
{
    int num=-12345,j=0,i=0,flag=0,begin,end;
    char str[]="I am a student",temp;
    j=strlen(str)-1;
    
    printf(" string=%s\n",str);
    //第一步是进行全盘反转,将单词变成“tneduts a ma I”
    while(j>i)
    {
        temp=str[i];
        str[i]=str[j];
        str[j]=temp;
        j--;
        i++;
    }
    printf(" string=%s\n",str);
    int i=0;
    //第二步进行部分反转,如果不是空格则开始反转单词
    while(str[i])
    {
        if(str[i]!=' ')
        {
            begin=i;
            while(str[i]&&str[i]!=' ')
            {
                i++;
            }
            i=i-1;
            end=i;
        }
        while(end>begin)
        {
            temp=str[begin];
            str[begin]=str[end];
            str[end]=temp;
            end--;
            begin++;
        }
        i++;
    }
    printf(" string=%s\n",str);
    return 0;
}
复制代码

 

既然看到了,就应该要思考吧。仅提升..


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值