Inverter uma String

5 respostas
G

Olá alguem conhece um método ou classe que torne isso possivel!
Por ex… ao digitar 123456 ele escrever 654321 ou entao ao escrever casa e responsa asac??
Seria apenas trocar a posicao na String??

5 Respostas

D

Acho q naum tem nenhum metodo na classe string para fazer isso, mas da pra fazer isso facil:

public class Teste {

  public Teste() {
  }

  static public void main(String args[]) {
    String str = "casa"; 
    char buf[] = new char[4]; 
    int j = str.length() - 1; 

    for (int i=0; i < str.length(); i++) { 
      buf[i] = str.charAt(j); 
      j--; 
    } 

    String strInvertida = String.copyValueOf(buf); 
    System.out.println(strInvertida);
  }

}

Provavelmente deve haver maneira melhor de se fazer isso, mas fiz meio q no vurto aqui

G

Isso mesmo!
Deu certo
Valeu ai!

V

Realmente:

String invertida = new StringBuffer("abcde").reverse().toString();

ehehehe

G

Putz agora sim fecho…
ahuah valeu ai kra!

D

“viecili”:

Realmente:

String invertida = new StringBuffer("abcde").reverse().toString();

ehehehe

Hauhauh to me sentindo um idiota hauahuah :humm:

Caramba, eu sempre esqueco da classe stringbuffer, fazer o q neh?! Vivendo e aprendendo…
Valeu pela solucao Viecili!

Criado 26 de outubro de 2004
Ultima resposta 27 de out. de 2004
Respostas 5
Participantes 3