Answers for "java stack reverse string"

1

how to reverse the stack in java

public void rev() {
    	ArrayStack s1 = new ArrayStack();
    	int[] data2;
    	data2 = new int[1000];
    	for (int i = 0 ; i<size();i++) {
    		data2[i] = data[i];
    	}
    	
    	for (int i = 0 ; i<size();i++) {
    	 s1.push(data2[i]);
    	}
    	
    	for (int i = 0 ; i<size();i++) {
    		data2[i] = s1.pop();
    		System.out.print(data2[i] + " ");
    	}
    	System.out.println();
    }
Posted by: Guest on April-24-2021
2

java string reverse

String str = "Reverse this strings";
//for loop
for (int i = str.length() - 1; i >= 0; i--) {
	System.out.print(str.charAt(i));
}

// StringBuffer for multi thread
StringBuffer sb = new StringBuffer(); 
sb.append(str);
System.out.print(sb.reverse().toString());

// StringBuffer for single thread
StringBuilder sb = new StringBuilder(); 
sb.append(str);
System.out.print(sb.reverse().toString());
Posted by: Guest on June-01-2020

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language