字符串逆转
来源:互联网 发布:最准确的生男生女算法 编辑:程序博客网 时间:2024/04/27 18:03
题意:输入一个字符串,逆向输出该串。
Sample Input
3
Frankly, I don't think we'll make much
money out of this scheme.
madam I'm adam
Sample Output
hcum ekam ll'ew kniht t'nod I ,ylknarF
.emehcs siht fo tuo yenom
mada m'I madam
方法一采用求解字符串长度,代码如下:
#include <iostream>#include <string.h>#include <stdio.h>#include <string>using namespace std;int main(){ int n ; cin >> n ; getchar() ; while(n--) { char s[100] ; gets(s) ; int len = strlen(s) ; for(int i = len - 1 ; i >= 0 ; i--) cout << s[i] ; cout << endl ; } return 0;}
方法二采用字符数组赋值给字符串,调用reverse函数完成:
#include <iostream>#include <algorithm>using namespace std;int main (){int n;char ss[71];string s;cin>>n;cin.get(); //吸收一个'\n',不然会影响到cin.getline() while (n--){cin.getline(ss,71);s = ss; //讲字符数组赋值给字符串,太方便了 reverse(s.begin(),s.end());cout<<s<<endl;}return 0;}
方法三采用JAVA完成:
import java.util.Scanner;public class Main {public static void main(String[] args) {Scanner sc = new Scanner(System.in) ;int n = sc.nextInt() ;String get = sc.nextLine() ; //吸收换行符while(n-- > 0){String s = sc.nextLine() ;StringBuffer str = new StringBuffer(s) ;System.out.println(str.reverse());}}}
0 0
- 字符串逆转
- 字符串逆转
- 字符串逆转
- 字符串逆转
- 字符串逆转
- 字符串逆转
- 字符串逆转
- 字符串逆转
- 逆转字符串
- 逆转中文字符串-->逆转字符串
- 递归逆转字符串
- 字符串按单词逆转
- 苛刻的字符串逆转
- 字符串逆转问题
- 字符串逆转算法
- 字符串逆转--递归
- 面试题字符串逆转
- 逆转字符串 C语言
- C#创建windows服务
- IOS推送详解
- POI读取Excel(兼容Excel2003、Excel2007)
- Eclipse——工作台
- php发送邮件
- 字符串逆转
- Log4Net使用指南
- Hackerrank Sherlock and Squares
- 指针的指针
- poi 解析excel 03与07不兼容问题
- Python中的多态与虚函数
- oracle树形结构由子节点递归得到父节点
- CKEditor+CKFinder+php使用心得
- C基础问题