一、问题描述
给定一个字符串,输出反转后的字符串。
样例输入1: abc
样例输出1: cba
样例输入2: GeeksforGeeks
样例输出2: skeeGrofskeeG
二、Java实现(九个程序)
1、实现1
思想: 逐个取 + 倒序拼
Java代码:
import java.io.*;
import java.util.Scanner;
public class Method_1
{
public static void main(String args[])
{
String str = "Geeks", nstr = "";
char ch;
System.out.print("Original word: ");
System.out.println(str);
for(int i = 0;i < str.length();i ++)
{
ch = str.charAt(i);
nstr = ch + nstr;
}
System.out.println("Reversed word: " + nstr);
}
}
样例输出:
2、实现2
思想: 转字节串,倒序输出
Java代码:
import java.lang.*;
import java.io.*;
import java.util.*;
public class Method_2
{
public static void main(String args[])
{
String input = "GeeksforGeeks";
byte [] strAsByteArray = input.getBytes();
byte [] result = new byte[strAsByteArray.length];
for (int i = 0;i < strAsByteArray.length;i ++)
{
result[i] = strAsByteArray[strAsByteArray.length - i - 1];
}
System.out.println("original string: " + input);
System.out.println("original converted byte string: " + strAsByteArray);
System.out.println("reversed byte string: " + result);
System.out.println("reversed string: " + new String(result));
}
}
样例输出:
3、实现3
思想: 使用StringBuilder类内置的reverse()方法
Java代码:
import java.lang.*;
import java.io.*;
import java.util.*;
public class Method_3
{
public static void main(String args[])
{
String input = "Geeks for Geeks";
StringBuilder input1 = new StringBuilder();
input1.append(input);
input1.reverse();
System.out.println("Original String: " + input);
System.out.println("Reversed String: " + input1);
}
}
样例输出:
4、实现4
思想: 转字符数组CharArray,前后倒转Swap
Java代码:
import java.lang.*;
import java.io.*;
import java.util.*;
public class Method_4
{
public static void main(String args[])
{
String input = "Geeks for Geeks";
char [] temparray = input.toCharArray();
int left, right = 0;
right = temparray.length - 1;
for(left = 0;left < right;left ++, right --)
{
char temp = temparray[left];
temparray[left] = temparray[right];
temparray[right] = temp;
}
System.out.println("Original string: " + input);
System.out.print("Reversed string: ");
for (char c : temparray)
{
System.out.print(c);
}
System.out.println();
}
}
样例输出:
5、实现5
思想(类似实现4): 转字符数组CharArray,逆序输出
Java代码:
import java.lang.*;
import java.io.*;
import java.util.*;
public class Method_5
{
public static void main(String args[])
{
String input = "GeeksForGeeks";
char [] try1 = input.toCharArray();
System.out.println("Original string: " + input);
System.out.print("Reversed string: ");
for (int i = try1.length - 1; i >= 0;i --)
{
System.out.print(try1[i]);
}
}
}
样例输出:
6、实现6
思想: 使用ArrayList对象,ListIterator迭代变量
Java代码:
import java.lang.*;
import java.io.*;
import java.util.*;
public class Method_6
{
public static void main(String args[])
{
String input = "Geeks for Geeks";
char [] hello = input.toCharArray();
List<Character> trial1 = new ArrayList<>();
for (char c : hello)
{
trial1.add(c);
}
Collections.reverse(trial1);
ListIterator Li = trial1.listIterator();
System.out.println("Original string: " + input);
System.out.print("Reversed string: ");
while (Li.hasNext())
{
System.out.print(Li.next());
}
}
}
样例输出:
7、实现7
思想: 使用StringBuffer类内置的reverse()方法
Java代码:
import java.lang.*;
import java.io.*;
import java.util.*;
public class Method_7
{
public static void main(String args[])
{
String str = "GeeksforGeeks";
StringBuffer sbr = new StringBuffer(str);
sbr.reverse();
System.out.println("Original string: " + str);
System.out.println("Reversed string: " + sbr);
}
}
样例输出:
8、实现8
思想(类似实现4): 转字符数组,不同之处:读取用户输入
Java代码:
import java.io.*;
import java.util.Scanner;
public class Method_8
{
public static void main(String args[])
{
Scanner scanner = new Scanner(System.in);
String Str = scanner.nextLine();
char [] arr = Str.toCharArray();
String rev = "";
for (int i = Str.length() - 1;i >= 0;i --)
{
rev = rev + Str.charAt(i);
}
System.out.println("Original string: " + Str);
System.out.println("Reversed string: " + rev);
}
}
样例输入:
样例输出:
9、实现9
思想: 使用堆栈的LIFO(Last-in First-out,后入先出)性质
java代码:
import java.util.*;
public class Method_9
{
public static void main(String args[])
{
String s = "Geeks for Geeks";
Stack<Character> stack = new Stack<>();
for (char c : s.toCharArray())
{
stack.push(c);
}
String temp = "";
while (! stack.isEmpty())
{
temp += stack.pop();
}
System.out.println("Original string: " + s);
System.out.println("Reversed string: " + temp);
}
}
样例输出:
参考
Geeks for Geeks.