Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How java uses the stack to reverse characters

Shulou Source: shulou.com Published: 2022-05-31 13:40:43 10月13日 Update

This article is about how java uses the stack to reverse characters. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

Use the stack

Package net.javaguides.corejava.string;import java.util.Stack;/*** * @ author Ramesh Fadatare**/public class ReverseStringUsingStack {/ / Function to reverse a string in Java using a stack and character arraypublic static String reverse (String str) {/ / base case: if string is null or emptyif (str = = null | | str.equals (")) return str;// create an empty stack of charactersStack

< Character >

Stack = new Stack

< Character >

(); / / push every character of the given string into the stackchar [] ch = str.toCharArray (); for (int I = 0; I < str.length (); iSum +) stack.push (ch [I]); / / start from index 0int k = 0 stack.isEmpty / pop characters from the stack until it is emptywhile (! stack.isEmpty ()) {/ / assign each popped character back to the character arraych [kicking +] = stack.pop ();} / / convert the character array into string and return itreturn String.copyValueOf (ch) } public static void main (String [] args) {String str = "javaguides"; str = reverse (str); / / string is immutableSystem.out.println ("Reverse of the given string is:" + str);}}

Output:

Reverse of the given string is: what are the basic data types of sediugavajjava? the basic data types of Java can be divided into: 1. Integer types, which are used to represent the data types of integers. 2. Floating point type, which is used to represent the data type of decimals. 3. Character type. The keyword of character type is "char". 4. Boolean type, which is the basic data type that represents logical values.

Thank you for reading! This is the end of this article on "how java uses stack inversion characters". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!

Tags: Type data character stack content integer more article good practical key keyword decimal Boolean article floating point see knowledge logic logical value Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno OPPO Reno NVidia MariaDB Microsoft Apple