字符串的输出处理
来源:互联网 发布:好看的喜剧电影知乎 编辑:程序博客网 时间: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;}
这道题要思路是只要是空格前面的就要倒着输出,要特别注意的是最后的一个单词,这样我们就知道要特殊处理啦。
还有的题型是从大写的方面考虑的,以后一定细说,其实要把字符串驾驭的很好很好也是不易的,所以,加油吧!
- 字符串的输出处理
- 字符串处理输出每个字符出现的次数的代码
- 对字符串倒过来输出的几种处理方法。
- 【2037】利用字符串处理,输出x+y的结果
- hdu 1062(字符串的处理 倒序输出)
- 字符串的逆向输出
- 字符串的遍历输出
- 字符串的逆序输出
- 字符串的倒序输出
- 字符串的反转输出
- 字符串的输出
- 字符串的倒序输出!
- 字符串的输出
- 字符串的逆序输出
- 字符串的输出
- 字符串的反转输出
- 字符串的倒序输出
- 字符串的输入、输出
- C语言实现二分法查找
- 实时监听UITextField内文字的改变
- Android运用Gradle build后生成的app-debug-unaligned.apk是什么?
- 2037,多想,多做。
- python中的交互执行函数
- 字符串的输出处理
- 227 - Puzzle
- 乱弹java并发(八)-- Phaser
- Android是一个针对触摸屏专门设计的操作系统,当点击编辑框,系统自动为用户弹出软键盘,以便用户进行输入。 那么,弹出软键盘后必然会造成原有布局高度的减少,那么系统应该如何来处理布局的减少
- 一个可以将nginx 加入windows 服务的软件----SeeHiServer
- Amf3协议
- 轻松搞定素数
- 数值类型转换根据大小排列顺序
- 聚类