Contest5.StringReverse

来源:互联网 发布:淘宝精仿鞋店推荐 编辑:程序博客网 时间:2024/06/14 15:04

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

3I am goodYou are OKnice char

Sample Output

I ma dooguoY era KOecin 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 ;}

原创粉丝点击