(转载)2011年金山软件C++开发工程师笔试题 关于逆序输出

(转载)http://blog.sina.com.cn/s/blog_7939cf980101b749.html

给定一个字符串,它包含了大量由空格分隔的单词,例如“dogs eat bones”。试构造一个函数,创建一个新字符串(不需要原地转换),将给定字符串中的单词反序,上例中即为“bones eat dogs”。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct Word{
    char s[20];
    int num;
}word[100];

void main()
{
    struct Word;
    char str[2000];
    char *p = str;
    int i = 0;
    int j = 0;
    int k = 0;

    gets(str);

    while(*p != '\0')
    {
        while(*p == ' ')
            ++p;
        do
        {
            word[i].s[j]=*p++;

            j++;
        }while(*p != ' ' && *p != '\0');

        word[i].num=i;
        i++;
        j=0;
    }

    int len=i;
    for(int ll = len ;ll >= 0 ;ll--)
        printf("%s ",word[ll].s);

    putchar('\n');
}

运行截图:

posted @ 2013-05-05 22:48  robotke1  阅读(191)  评论(0)    收藏  举报