杭电OJ1062 Text Reverse
2016-07-19 17:16
639 查看
#include<iostream> using namespace std; int main(){ int t,i; char a[1000],p; cin >> t; cin.ignore(1, '\0'); while (t--){ i = -1; cin.getline(a, 1000); for (int n = 0; n <=strlen(a); n++){ i++; if (a == ' ' || a == '\0'){ int l = 0; int r = i / 2; int j = n - i; //n-i 为单词第一个字母 while(r--){ p = a[n - 1+l]; a[n - 1+l] = a[j ]; a[j] = p; l--; j++; } i = -1; } } for (int n = 0; n < strlen(a); n++) cout << a ; cout << endl; } }
本例中,字符串中单词的长度知道,循环交换的次数为(int)r/2次,循环次数确定,用while比用for方便
用cin.getline(a,100)获得字符串,在数组的a[strlen(a)]处存放结束符\0
使用reverse()函数:
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
int main(){
char s[2001];
char *c;
int t;
cin >> t;
cin.ignore(1, '\n');
while (t--){
cin.getline(s, 2001);
c = &s[0];
for (int n = 0; n <= strlen(s); n++){
if (s
== ' ' || s
== '\0'){
reverse(c, &s
);
c = &s[n + 1];
}
if (s
== '\0')
c = NULL;
}
cout << s << endl;
}
}
相关文章推荐
- Window 下mysql binlog开启及查看,mysqlbinlog
- 《剑指offer》二叉树中和为某一值的路径
- 重置kafka的offset
- centos hadoop搭建准备
- LUA学习笔记(第5-6章)
- ACM2016多校联赛1A Abandoned country
- TASK
- C语言运算符的优先级和结合性实例详解
- Java 中正确使用 hashCode 和 equals 方法
- 数据结构实验之图论八:欧拉回路
- Android内存泄漏01
- 78. Subsets
- Integer Break
- android自动化测试
- opengl画出一个sin函数
- 弹出框上下居中
- Echarts地图合并提取
- C/C++枚举设备管理器上显示的物理串口(能够枚举出USB转串口等)
- HOG特征
- 11. Container With Most Water(重要!)