Huawei Online Programming Question Series-12-String Reversal


Problem Description:
Problem Description

1. The question involves knowledge points.

  • character flip

2. Solve it yourself.

  • Solution 1: Use string.charAr(index)the reverse method to remove characters.
import java.util.Scanner;

/**
 * Create by tianchaoxiong on 18-4-9.
 */
public class HuaWei_12 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNext()){
            String key = scanner.nextLine();
            getResult(key);
        }
    }
    private static void getResult(String key) {
        int len = key.length();
        for(int i=0;i<key.length();i++){
            System.out.print(key.charAt(len-i-1));
        }
    }
}
  • Solution 2: use StringBuffer.reverse()/ StringBulder.reverse()directly invert. // faster
package com.chaoxiong.niuke.huawei;

import java.util.Scanner;

/**
 * Create by tianchaoxiong on 18-4-9.
 */
public class HuaWei_12_2 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNext()){
            String key = scanner.nextLine();
            getResult(key);
        }
    }
    private static void getResult(String key) {
        System.out.println(new StringBuffer(key).reverse());
    }
}

3. Quality answers.

null

4. Summary of this question.

There are two ways to reverse the order of a string
- use string.charAt(index)reverse fetch.
- use stringbuffer.reverse()/ StringBulder.reverse()direct reverse order.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325967501&siteId=291194637