字符串的输出处理

来源:互联网 发布:好看的喜剧电影知乎 编辑:程序博客网 时间:2024/06/06 10:39
我对字符串的输出是比较头疼的,就是在技巧方面把握的不好,记得刚开始接触字符串的时候,就是要正着输入倒着输。。。。。。都不会,就是找不到技巧,下面我会从简单到复杂分析一下这种题型,可能不全,请见谅。

一.

先说一下题意,就是随便输入一句话如“hello my friend”,输出的时候是“dneirf ym olleh”,大家看出什么了没有,就是正着输入,倒着输出,先看一下主要的代码内容吧.

#include<stdio.h>#include<string.h>int main(){    int i, len;    char a[1000];    while(gets(a))    {        len=strlen(a);        for(i=len-1;i>=0;i--)        {            printf("%c", a[i]);        }        printf("\n");    }    return 0;}

简单的已经话,就是“正着输入,倒着输出”,要注意的是因为这句话中有空格,所以,要用gets(),scanf()的特性是遇到空格或者回车就会停止,大家可以试一下,这样有助于加深自己的印象。
二.

先说一下大体意思,就是也是随便输入一句话,如“hello my friend”,输出的时候是“olleh ym dneirf”,大家看出什么没有,就是各个单词的位置还是原来的位置,但是,都倒着输出了。下面看一道例题

Word Reversal

Description

For each list of words, output a line with each word reversed without changing the order of the words.

This problem contains multiple test cases!

The first line of a multiple input is an integer N, then a blank line followed by N input blocks. Each input block is in the format indicated in the problem description. There is a blank line between input blocks.

The output format consists of N output blocks. There is a blank line between output blocks.

Input

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

Output

For each test case, print the output on one line.

Sample Input

1

3
I am happy today
To be or not to be
I want to win the practice contest

Sample Output
I ma yppah yadot
oT eb ro ton ot eb
I tnaw ot niw eht ecitcarp tsetnoc

下面先看一下我的代码吧

#include<stdio.h>#include<string.h>int main(){    int n, i, len, j, l, m, k;    char a[100];    while(~scanf("%d",&m))    {        for(k=0;k<m;k++)        {            scanf("%d",&n);            getchar();            for(i=0;i<n;i++)            {                gets(a);                len=strlen(a);                for(j=0;j<len;j++)                {                    if(a[j]==' ')                    {                        for(l=j-1;;l--)                        {                            if(a[l]==' '||l==-1)                            {                                break;                            }                            printf("%c",a[l]);                        }                        printf(" ");                    }                    if(j==len-1)//特殊处理最后一个单词                    {                        for(l=len-1;;l--)                        {                            if(a[l]==' '||l==-1)                            {                                break;                            }                            printf("%c",a[l]);                        }                    }                }                    printf("\n");            }            if(k!=m-1)            {                printf("\n");            }        }    }    return 0;}

这道题要思路是只要是空格前面的就要倒着输出,要特别注意的是最后的一个单词,这样我们就知道要特殊处理啦。

还有的题型是从大写的方面考虑的,以后一定细说,其实要把字符串驾驭的很好很好也是不易的,所以,加油吧!

0 0
原创粉丝点击