笔试题:把一串字符串中单词逆序

例如输入:
wang zheng jun

输出:
gnaw gnehz nuj

#include 
#include 
int main()
{

    char str[100] = {0};


    int len = 0;
    int count = 0;
    int index = 0;
    char ch = 0;
    int begin = 0, end = 0;
    int i = 0;
    int spaceNumbers = 0;

    /* 得到输入的字符串 */
    gets(str);
    len = strlen(str);


    for(index = 0; index < len; index++)
    {
        if(str[index] == ' ')
        {
            end = index;

            for(i = 0; i < count/2; i++)
            {
                ch = str[begin+i];
                str[begin+i] = str[end-1-i];
                str[end-1-i] = ch;
            }
            begin = end+1;
            count = 0;
            spaceNumbers++;

        }

        /* 说明到字符串的结尾了 */
        if(index == len-1)
        {
            end = len;
            for(i = 0; i < count/2; i++)
            {
                ch = str[begin+i];
                str[begin+i] = str[end-1-i];
                str[end-1-i] = ch;
            }

        }

        count++;
    }

    printf("%s\n",str);


    return 0;
}

你可能感兴趣的:(经典编程题)