【问题标题】:Reverse a string without using string functions [duplicate]不使用字符串函数反转字符串[重复]
【发布时间】:2016-08-03 03:52:36
【问题描述】:

如何编写一个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)); 
 }

这里的charAt()a.length() 是字符串函数

【问题讨论】:

  • 你想反转字符串本身还是只打印反转?
  • 把它倒过来打印出来
  • 如果不使用 any 方法,您根本无法获取数据。想必这是作业,所以我建议你问问你的导师你被允许使用哪些方法。
  • 不使用任何 String 方法我们不能反转一个字符串?

标签: java


【解决方案1】:

这会有所帮助

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);
  }

}

没有使用String方法

【讨论】:

  • str.toCharArray() 是一个字符串函数
  • 噗,我一直到。字符串 str = "测试";匹配器 m = Pattern.compile("$").matcher(str); m.find(); int 长度 = m.end();
【解决方案2】:
String s = "abcdef";
char c[] = s.toCharArray();

for( int i = c.length -1; i>=0; i--)
    System.out.print(c[i]);

【讨论】:

  • length() 是字符串函数
  • 是长度而不是长度()
  • toCharArray() 字符串函数。
  • s.toCharArray() 数组是一个字符串函数
  • 考虑到Java,我认为如果不使用函数我们无法完成反向操作,因为它不直接支持指针。
【解决方案3】:

使用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

【讨论】:

  • +1 这应该是公认的答案
【解决方案4】:

下面是丑陋的 hack。它的概念,但它不调用任何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(""));
    } 
}

输出:

tseT

【讨论】:

  • charAt() word.length() 是字符串函数
  • @JeevanRoydsouza 查看更新。它不调用任何字符串方法。
  • 是的,我们也可以这样做。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-06-21
  • 2011-09-27
  • 2022-01-22
  • 1970-01-01
  • 2012-08-06
  • 2013-02-08
相关资源
最近更新 更多