Contest5.StringReverse

Description

You will be given a number of test cases. The first line contains the number of cases as a positive integer. Each case is given with a line containing a list of words separated by a space, and each word contains only uppercase and lowercase letters.
 

Input

The first line contains the number of cases as a positive integer. Each case is given with a line containing a list of words separated by a space, and each word contains only uppercase and lowercase letters.

Output

For each test case, output the result.

Sample Input

3
I am good
You are OK
nice char

Sample Output

I ma doog
uoY era KO
ecin rahc

HINT

注意在第一次使用gets()函数时,可能将缓冲区中的\n也接收了,所以在第一个scanf后加一个getchar()



    scanf("%d",&n);

    getchar();


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

void reverseStr(char* str, int i, int j)//将一个字符串中的第i项到第j项反转
{
    for (; i < j; i++, j--)
    {
        char tmp;
        tmp = str[i];
        str[i] = str[j];
        str[j] = tmp;
    }
    return ;
}

void reverseWords(char* str)//完成对一句句子的操作
{

    char* subStrStart;//用Start和End来定义一个单词,
    char* subStrEnd;  //即从第Start项到第End项为一个单词
    char* currentPos; //扫描的当前位置

    currentPos = &str[0];//扫描位置初始化
    while(*currentPos!='\0')
    {
        subStrStart = currentPos;
        while(*currentPos!=' ' && *currentPos!='\0')//得出一个
            currentPos++;                           //单词的长度
        subStrEnd = currentPos - 1;
        reverseStr(str, (int)(subStrStart - &str[0]), (int)(subStrEnd - &str[0]));
        currentPos++;
    }
    return;
}

void main()
{
    int n,i;
    scanf("%d",&n);
    getchar();
    char line[n][250];
    for(i=0;i<n;i++)
    {
        gets(line[i]);
        reverseWords(line[i]);
        printf("%s\n",line[i]);
    }
    return ;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值