Java 不使用字符串函数反转字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20393318/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Reverse a string without using string functions
提问by Jeevan Roy dsouza
How to write a java program to reverse a string without using string functions?
如何在不使用字符串函数的情况下编写一个java程序来反转字符串?
String a="Siva";
for(int i=0;i<=a.length()-1;i++)
{
System.out.print(a.charAt(i));
}
System.out.println("");
for(int i = a.length() - 1; i >= 0; --i)
{
System.out.print(a.charAt(i));
}
here charAt()
and a.length()
are string functions
了这里charAt()
,并a.length()
在字符串函数
回答by Keerthivasan
This will help
这将有助于
public class StringReverse {
public static void main(String[] args){
String str = "Reverse";
StringBuilder sb = new StringBuilder(str);
str = sb.reverse().toString();
System.out.println("ReverseString : "+str);
}
}
There is no usage of String methods
没有使用 String 方法
回答by laksys
String s = "abcdef";
char c[] = s.toCharArray();
for( int i = c.length -1; i>=0; i--)
System.out.print(c[i]);
回答by Deepak
Use StringBuilder
class or StringBuffer
class they have already a method reverse()
for reversing the string
使用StringBuilder
类或StringBuffer
类他们已经有reverse()
反转字符串的方法
StringBuilder str = new StringBuilder("india");
System.out.println("string = " + str);
// reverse characters of the StringBuilder and prints it
System.out.println("reverse = " + str.reverse());
// reverse is equivalent to the actual
str = new StringBuilder("malayalam");
System.out.println("string = " + str);
// reverse characters of the StringBuilder and prints it
System.out.println("reverse = " + str.reverse());
http://docs.oracle.com/javase/7/docs/api/java/lang/StringBuilder.html
http://docs.oracle.com/javase/7/docs/api/java/lang/StringBuilder.html
回答by Michael Kazarian
Below is ugly hack. It concept but it not invoke any String
methods.
下面是丑陋的黑客。它的概念但它不调用任何String
方法。
import java.io.*;
import java.util.*;
public class Hello {
public static String reverceWithoutStringMethods(String word){
String result = "";
//------ Write string to file -----------
BufferedWriter writer = null;
try {
writer = new BufferedWriter( new FileWriter("tempfile"));
writer.write(word);
}
catch ( IOException e) {}
finally {
try{
if ( writer != null) writer.close( );
}
catch ( IOException e){}
}
//------ read string from file -------------
RandomAccessFile f=null;
try {
f = new RandomAccessFile("tempfile", "r"); // Open file
int length = (int) f.length(); // Get length
// Read file
byte[] data = new byte[length];
f.readFully(data);
// Reverse data
for (int i=data.length; i>=0; i--){
result += (char)data[i-1];
}
} catch(Exception e){}
finally {
try{
f.close();
}
catch (Exception e){}
}
return result;
}
public static void main(String[] args) {
System.out.println(reverceWithoutStringMethods("Test"));
System.out.println(reverceWithoutStringMethods(""));
}
}
Output:
输出:
tseT