zoukankan      html  css  js  c++  java
  • 字符串逆转

    题意:输入一个字符串,逆向输出该串。

    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(); //吸收一个'
    ',不然会影响到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());
    		}
    	}
    }




  • 相关阅读:
    js-计算器
    正确看待HTML5的语法变化
    什么是Web前端,Web前端是做什么的?
    css复合属性的写法
    Unicode与JavaScript详解
    input 定宽,文本超出后缩小字体,尽可能多的显示文本
    手机号中间四位加*号
    React 状态和生命周期
    还是数组 练习题 而已
    数组 练习题 而已
  • 原文地址:https://www.cnblogs.com/NYNU-ACM/p/4236814.html
Copyright © 2011-2022 走看看