实现一个反转程序,要求:不能使用库函数。such as: 输入:123    输出:321
          输入:asdf   输出:fdsa 欢迎大家试试。不难,考基础。 今天考的不好,看来还要多多练习编程。

解决方案 »

  1.   

    public String getStr(String s)
    {
    StringBuffer sb = new StringBuffer();for(int i=s.length()-1; i>=0;i--)
    {
      sb.append(s.charAt(i));
    }return sb.toString() ;
    }
      

  2.   

    s.length
    人家不让用,怎么办
      

  3.   

    StringBuffer sb=new StringBuffer("123");
    return sb.reverse().toString();
      

  4.   

    public static void main(String[] args) throws IOException{     byte[] container = new byte[8189];
        int read = 0;
        int total = 0;
        while((read=System.in.read())!=-1 && read!='\n'){
            if(total+1>container.length){
                byte[] oldArray = container;
                container = new byte[total*2];
                for(int i=0;i<total;i++){
                    container[i] = oldArray[i];
                }
            }
             container[total] = (byte)read;
             total++;
        }
        for(int i=total;i>0;i--){
            System.out.print((char)container[i-1]);
        }
    }
      

  5.   

    import java.io.*;
    import java.lang.*;public class Jex1
    {
    public static void main(String[] args){
        int rev=0,c,i=0,sign=0;
    System.out.println("Please input a 3 bits int:");
    try{
              while ((c=System.in.read())>=48 && c<58 || c=='+' || c=='-')
              {
      if (c>=48 && c<58)
      {
      rev=rev+(c-48)*(int)Math.pow(10,i);
      i++;
        }
    else if(c=='-')
    sign=1;
              } 
      if (sign==1)
      rev=-rev;
      System.out.println("The reverse of the int is" +rev);
      }
      catch(Exception e){
      System.err.println("Input error!");
      }
    }
    }
      

  6.   

    public class AlterString { public String altStr(String str) {
    String tempStr = "";
    if (str != null) {
    for (int i = 0; i < str.length(); i++) {
    tempStr = str.charAt(i) + tempStr;
    }
    }
    return tempStr;
    }}=================================================================
      

  7.   

    用toCharArray()把String拆成char[],然后......
      

  8.   

    "不能使用库函数"是不是所有api的方法都不能用的意思?
      

  9.   

    linuxbing(翅膀) 的答案就可以。符合要求。不是所有API不让用,有些还是可以用的。
      

  10.   

    晕翅膀的是挨个输入的,如果是存在的string呢??
      

  11.   

    用StringBuffer里面的reverse()方法,就行了!自己实现也是比较简单的!
    import java.io.*;public class ReverseStringDemo { public ReverseStringDemo() {
    InputStreamReader reader = new InputStreamReader(System.in);
    BufferedReader cin = new BufferedReader(reader);
    try {
    StringBuffer str = new StringBuffer(cin.readLine());
    System.out.println(str.reverse().toString());
    }catch(IOException ioe) {
    ioe.printStackTrace();
    }
    }

    public static void main(String[] args) {
    new ReverseStringDemo();
    }}
      

  12.   

    import java.io.*;
    import java.awt.event.*;class Transfer 
    {
    public static void main(String[] args) throws IOException
    {
    BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    System.out.print("Please input:");
    String inputString = in.readLine();
    char[] inputChar = inputString.toCharArray();
    System.out.print("The result:----->");
    for(int i = inputChar.length-1;i >= 0;i--)
    System.out.print(inputChar[i]);
    System.out.println();
    }
    }
      

  13.   

    如果也不让用"System.in"怎么办,
    呵呵,开玩笑
      

  14.   

    public class tt { public static void main(String[] args) {

    String s = "123";
    for(int i = (s.length()-1);i>=0;i--){
    System.out.println(s.charAt(i));
    }

    }
    }
    -=l---------------------------------
    垃圾公司,有函数不让用。
      

  15.   

    if(String s=="123"){
      out.println("321");
    }
    嘻嘻^_^
      

  16.   

    不用库函数?招java程序员不让用库函数?
      

  17.   

    StringBuffer sb=new StringBuffer("123");
    return sb.reverse().toString();这样就可以了阿
    如果这个不让用酒可以用流,放入数据然后从后面往前读
      

  18.   

    什么算库函数
    System.in
    让不让用??
      

  19.   

    顶,好像一般用个loop就可以搞定的。
      

  20.   

    if(String s=="123"){
      out.println("321");
    }
    ~!~~`就是这个答案了,,公司考的是智力题,,.真是绝了...
      

  21.   

    时间/空间效率最高的代码:
    StringBuffer str = new StringBuffer("abcdefgh");
    int len = str.length(), left, right;
    char c;
    for(left = 0; left < len/2; left++) {
    right = len - left - 1;
    c = str.charAt(left);
    str.setCharAt(left, str.charAt(right));
    str.setCharAt(right, c);
    }
    System.out.println(str);
      

  22.   

    123是String类型的还是int型的,是不是要对这个反转函数重载才行啊!
      

  23.   

    搞笑呢!不让用函数?那用java干什么啊?用C得了1!!
      

  24.   

    这种题目肯定不让用任何String的API,
    所以用char[], 很简单...
      

  25.   

    搞笑呢!不让用函数?那用java干什么啊?用C得了1!!
    同意,不如换成考让自己建个队列或栈的数据结构并完成操作实现
      

  26.   

    StringBuffer sb=new StringBuffer("123");
    return sb.reverse().toString();
    不过,就是不知道效率如何
      

  27.   

    if(String s=="123"){
      out.println("321");
    }
    ~!~~`就是这个答案了,,公司考的是智力题,,.真是绝了...
    ================
    强啊,哈哈
      

  28.   

    if(String s=="123"){
      out.println("321");
    }
    if(String s=="asdf"){
      out.println("fdsa");
    }
      

  29.   

    import java.io.*;
    public class DaoZhuang
    {
        public static void main(String[] args) throws java.io.IOException
        {
    String s1;
             int i, numcount;

    InputStreamReader isr = new InputStreamReader(System.in);
    BufferedReader br = new BufferedReader(isr);

    System.out.print("Enter a text: ");
             s1 = br.readLine();

    numcount = s1.length();

    for (i = numcount-1; i >=0; i--)
             System.out.print(s1.charAt(i));
    }
    }
      

  30.   

    翅膀的程序的试过了,可以的,不过要导入包IOException
    而且读入的时候把回车读进去了,所以会输出多一个回车
      

  31.   

    yesbi(我来也yesbi) 
    说得对,脑筋急转弯,呵呵。。开玩笑。。
      

  32.   

    public static void inverse(String s){
    for(int i=s.length()-1;i>=0;i--)
    System.out.print(s.charAt(i));
    }
      

  33.   

    JAVA里有没有堆栈?? 一个个放到堆栈里去....不过我估计这样的题目都要求只能用 char[] 来做, 不允许用任何String类的API, 否则太简单...
      

  34.   

    /* to henry_he()
    *可用除法,321-((321/10)*10)可得1
    *依次类推  
    */
      用除法怎么倒序字母??
      望指点
      

  35.   

    import java.io.IOException;public class StringReverserThroughStack {
      private String input;   private String output;  public StringReverserThroughStack(String in) {
        input = in;
      }  public String doRev() {
        int stackSize = input.length(); 
        Stack theStack = new Stack(stackSize);     for (int i = 0; i < input.length(); i++) {
          char ch = input.charAt(i); 
          theStack.push(ch); 
        }
        output = "";
        while (!theStack.isEmpty()) {
          char ch = theStack.pop(); 
          output = output + ch; 
        }
        return output;
      }  public static void main(String[] args) throws IOException {
        String input = "Java Source and Support";
        String output;
        StringReverserThroughStack theReverser = new StringReverserThroughStack(input);
        output = theReverser.doRev();
        System.out.println("Reversed: " + output);
      }
      class Stack {
        private int maxSize;
      
        private char[] stackArray;
      
        private int top;
      
        public Stack(int max) {
          maxSize = max;
          stackArray = new char[maxSize];
          top = -1;
        }
      
        public void push(char j) {
          stackArray[++top] = j;
        }
      
        public char pop() {
          return stackArray[top--];
        }
      
        public char peek() {
          return stackArray[top];
        }
      
        public boolean isEmpty() {
          return (top == -1);
        }
      
      }
    }
      

  36.   

    if(String s=="123"){
      out.println("321");
    }
    ~!~~`就是这个答案了,,公司考的是智力题,,.真是绝了...
    ================
    强啊,哈哈
    ================
    ================同哈哈
      

  37.   

    import java.io.*;
    public class ReverseDemo { public static void main(String[] args) {
    try {
    System.out.print("Type any number of characters and press Enter: ");
    String s = "";
    int i = 0;
    while( i != 13){

    i =  System.in.read();
    s = (char)i + s;

    }
    System.out.println(s);
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }
    }
      

  38.   

    不用流,不让用length,不使用库函数

    命令行参数+getchar()+for()
      

  39.   

    只能用基本类型,最高效旋转法,简单起见直接初始数组
    class strrev{
      public static void main(String args[]){
        char[] s={'a','b','c','d','e'};
        int i=0;
        char a;
        while((i+1)<=s.length/2){
          a=s[i];
          s[i]=s[s.length-1-i];
          s[s.length-1-i]=a;
          i++;
        }
        System.out.print(s);
      }
    }
      

  40.   

    没那么困难吧,一句话就可以了。
    String str = "12345";
    String rts = new StringBuffer(str).reverse().toString(); // 54321
      

  41.   

    楼主要多看看 JDK 文档。没事就多看看。
      

  42.   

    用substring不可以吗?用截取换位的思想就可以实现了呀
      

  43.   

    public void getStr(String str){
    for(int i=str.length()-1;i>=0;i--)
    System.out.print(str.charAt(i));
    }
    =================================================
    public void getStr(String str){
        StringBuffer sb=new StringBuffer(str);
        System.out.print( sb.reverse() );
    }
      

  44.   

    看我的这个行不 
    String Reverse(String str) {
        byte[] bt = str.getBytes();
        byte temp;
        for (int i = 0; i < bt.length / 2; i++) {
          temp = bt[i];
          bt[i] = bt[bt.length - i - 1];
          bt[bt.length - i - 1] = temp;
        }
        return new String(bt);
      }
      

  45.   

    不能使用库函数!!
    是不是也不能用 JAVA,C++,C ,VB,PB...
      

  46.   

    同意 hanshufan(小帆)
    用栈来实现,公司应该考的就是这个了
      

  47.   

    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    public class Convert{
    public static void main(String args[]){
    String str=null;
    System.out.println("please input a string:");
    try {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    str=br.readLine();
    } catch (IOException e) {
    // TODO 自动生成 catch 块
    e.printStackTrace();
    }
    System.out.println("the string is:"+ str);
    char[] temp = str.toCharArray(); 
    char[] temp1 = new char[temp.length];
    for(int i=temp.length-1, j=0;i>=0;i--,j++){
    temp1[j] = temp[i];

    }
    str=temp1.toString();

    System.out.println("the converted string is:"+ str);
    }


    }
      

  48.   

    if(String s=="123"){
      out.println("321");
    }
    ~!~~`就是这个答案了,,公司考的是智力题,,.真是绝了...
    ================
    强啊,哈哈
    ================
    ================同哈哈
      

  49.   

    import java.io.IOException;public class www {
    public static void main(String[] args) throws IOException { byte[] container = new byte[8189];
    int read = 0;
    int total = 0;
    while ((read = System.in.read()) != -1 && read != '\n') {
    if (total + 1 > container.length) {
    byte[] oldArray = container;
    container = new byte[total * 2];
    for (int i = 0; i < total; i++) {
    container[i] = oldArray[i];
    }
    }
    System.out.print (read);
    container[total] = (byte) read;
    total++;
    }
    for (int i = total; i > 0; i--) {
    System.out.print((char) container[i - 1]);
    }
    }
    }翅膀的代码的确有用..不过我有些不明白的地方请大家指教一下.
    就是这段
    if (total + 1 > container.length) {
    byte[] oldArray = container;
    container = new byte[total * 2];
    for (int i = 0; i < total; i++) {
    container[i] = oldArray[i];
    }
    }
    我是初学者请大家多多指教.
      

  50.   

    对了,栈的操作算是用api不?
      

  51.   

    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    public class Convert{
    public static void main(String args[]){
    String str=null;
    System.out.println("please input a string:");
    try {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    str=br.readLine();
    } catch (IOException e) {
    // TODO 自动生成 catch 块
    e.printStackTrace();
    }
    System.out.println("the string is:"+ str);
    Convert c = new Convert();
    String s = c.reverse(str);
    System.out.println("the converted string is:"+ s);
    }
    public String reverse(String str){
    char[] bt=str.toCharArray();
    char temp;
    for(int i=0;i<bt.length/2;i++){
          temp = bt[i];
          bt[i] = bt[bt.length - i - 1];
          bt[bt.length - i - 1] = temp;
    }
    return new String(bt);
    }

    }
      

  52.   

    import java.util.*;class Reverse 
    {
    public static void main(String[] args) 
    {
    String str;
            if (args.length<0)
            {
                System.out.println("请输入参数!");
            }
            str = args[0];
    char[] ch = str.toCharArray();
    char[] b;
    b = new char[ch.length];
    for (int i=0; i<ch.length;i++)
    {
    b[i] = ch[ch.length-1-i];
    }
    for (int i=0; i<b.length; i++)
    {
    System.out.print(b[i]);
    }
    /*就地逆置
    char temp;
    for (int i=0; i<ch.length/2;i++)
    {
    temp = ch[i];
    ch[i]=ch[ch.length-1-i];
    ch[ch.length-1-i] = temp;
    }

    for (int i=0; i<ch.length; i++)
    {
    System.out.print(ch[i]);
    }*/ /*利用栈来实现
    Stack st = new Stack();
    for (int i=0; i<ch.length; i++)
    {
    st.push(ch[i]);
    }
    for (int i=0; i<ch.length; i++)
    {
    System.out.println(st.pop());
    }*/
    }
    }---------------------------------------------------------------------
    以上三种方法都可以。他说不让用库函数是指不让用系统的Reverse函数而已。