Java教程

Java查找字符串的反向字符

在此程序中,我们需要查找字符串的反向。这可以通过向后迭代字符串并将每个字符从原始字符串存储到新字符串中来完成。
Original string: Dream big
Reverse of the string: big maerD

算法

步骤1: START 步骤2: DEFINE String string ="Dream big" 步骤3: 定义reversedStr ="" 步骤4: SET i = string.length()-1。直到i> = 0 为止,将步骤5重复到步骤6 步骤5: reversedStr = reversedStr + string.charAt(i) 步骤6: i = i-1 步骤7: 打印字符串。 步骤8: 打印reversedStr。 步骤9: END

程序:

public class Reverse
{
    public static void main(String[] args) {
        String string = "Dream big";
        //Stores the reverse of given string
        String reversedStr = "";
        //Iterate through the string from last and add each character to variable reversedStr
        for(int i = string.length()-1; i >= 0; i--){
            reversedStr = reversedStr + string.charAt(i);
        }
        System.out.println("Original string: " + string);
        //Displays the reverse of given string
        System.out.println("Reverse of given string: " + reversedStr);
    }
}
输出:
Original string: Dream big
Reverse of given string: gib maerD
昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4