Foros Club Delphi

Foros Club Delphi (https://www.clubdelphi.com/foros/index.php)
-   OOP (https://www.clubdelphi.com/foros/forumdisplay.php?f=5)
-   -   Invertir string - (https://www.clubdelphi.com/foros/showthread.php?t=57191)

mariolop12 07-06-2008 05:48:01

Invertir string -
 
Hola,

Yo uso Delphi 5.
Código Delphi [-]
Estuve buscando por internet como invertir un string.... y encontré el siguiente código

 Function String_Reverse(S : String): String;
Var
   i : Integer;
Begin
   Result := '';
   For i := Length(S) DownTo 1 Do
   Begin
     Result := Result + Copy(S,i,1) ;
   End;
End;

Yo soy muy nuevo en delphi, estoy leyendo y aprendiendo.... 
quisiera saber en qué parte del editor de código puedo introducirlo.

este es mi programa...


interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, Buttons;

type
  TForm1 = class(TForm)
    BitBtn1: TBitBtn;
    procedure BitBtn1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.BitBtn1Click(Sender: TObject);
VAR a,b,e:real;
r:integer;
begin

a:=strtofloat(inputbox('Datos','Introduzca numero A:',''));
b:=strtofloat(inputbox('Datos','Introduzca numero B:',''));

//parte a   -----------------------------------------------------------
e:=Exp(B*Ln(A));
showmessage('Respuesta:'+' '+floattostr(e));

//parte b   ----------------------------------------------------------
r:=(trunc(Int(A)) mod 4567);

AQUÍ DEBERÍA IR EL CÁLCULO DEL INVERSO DE   'r'

//parte c  ------------------------------------------------------------
ShowMessage('Redondear B a las milésimas:'+FormatFloat('"Value = "0.000', b));

end;

end.


espero puedan ayudarme

xEsk 07-06-2008 12:40:03

Hola, como no sé que quieres hacer con el resultado de "invertir", he hecho que se muestre con un ShowMessage.

Sería bueno que usaras las etiquetas de código DELPHI, así queda todo mucho mas bonito y claro :)
Código Delphi [-]
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, Buttons;

type
  TForm1 = class(TForm)
    BitBtn1: TBitBtn;

    procedure BitBtn1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

Function String_Reverse(S : String): String;
Var
  i : Integer;

Begin
  Result := '';
  For i := Length(S) DownTo 1 Do
    Begin
      Result := Result + Copy(S,i,1);
    End;
End;

procedure TForm1.BitBtn1Click(Sender: TObject);
VAR
  a,b,e:real;
  r:integer;

begin
  a:=strtofloat(inputbox('Datos','Introduzca numero A:',''));
  b:=strtofloat(inputbox('Datos','Introduzca numero B:',''));

  //parte a -----------------------------------------------------------
  e:=Exp(B*Ln(A));
  showmessage('Respuesta:'+' '+floattostr(e));

  //parte b ----------------------------------------------------------
  r:=(trunc(Int(A)) mod 4567);

  ShowMessage(String_Reverse(IntToStr(r)));

  //parte c ------------------------------------------------------------
  ShowMessage('Redondear B a las milésimas:'+FormatFloat('"Value = "0.000', b));
end;

end.

Te propongo otra forma de invertir un string, es muy similar:
Código Delphi [-]
function String_Reverse(s : String): String;
var
  i : Integer;

begin
  Result := '';
  for i:=1 to Length(s) do
    Result:=s[i] + Result;
end;

Saludos.

Caro 07-06-2008 13:49:14

Hola mariolop12, también puedes utilizar la función ReverseString de StrUtils.

Código Delphi [-]
 Showmessage(ReverseString('Texto a Invertir'));

Saluditos

mariolop12 07-06-2008 14:46:11

Cita:

Empezado por xEsk (Mensaje 292021)
Hola, como no sé que quieres hacer con el resultado de "invertir", he hecho que se muestre con un ShowMessage.

Sería bueno que usaras las etiquetas de código DELPHI, así queda todo mucho mas bonito y claro :)Código Delphi [-] implementation {$R *.dfm} Function String_Reverse(S : String): String;
Var i : Integer; Begin Result := '';
For i := Length(S) DownTo 1 Do Begin Result := Result + Copy(S,i,1); End;
End;



Saludos.



Gracias por tu respuesta,en tal caso i sería el string ya invertido?...
yo necesito colocar si un string es igual a su inverso...


lo estoy haciendo de esta manera...

Código Delphi [-]
if ((trunc(Int(A)) mod 4567))=(String_Reverse(IntToStr((trunc(Int(A)) mod 4567))))
then
begin
showmessage('El numero es capicua')
end
else
showmessage('El numero no es capicua');


y me muestra este error:

[Error] mario.pas(54): Incompatible types: 'String' and 'Int64'
la linea 54 es : Then


--------------

Caro 07-06-2008 15:48:46

Hola de nuevo, te muestra el error porque StringReverse te devuelve un String y para comparar debes hacer la conversión a entero.

Código Delphi [-]
 
Var
 Numero : Integer;
begin
 ......
 ......
 Numero := trunc(Int(A)) mod 4567;
 if Numero= StrToInt(ReverseString(IntToStr(Numero))) then
  showmessage('El numero es capicua')
 else
  showmessage('El numero no es capicua'); 
 ......

Como estas trabajando con números, no te sería mejor invertir el número, aquí te dejo la función para invertir números.

Código Delphi [-]
 
function TForm1.InvertirNumero(Numero : Integer):Integer;
var
 NumeroInvertido : Integer;
begin
 NumeroInvertido := 0;

 While (Numero>0) do
 begin
  NumeroInvertido := NumeroInvertido*10 + (Numero MOD 10);
  Numero := Numero Div 10;
 end;
 result := NumeroInvertido;
end;
 
procedure TForm1.BitBtn1Click(Sender: TObject);
var
 Numero : Integer;
begin
 Numero := trunc(Int(A)) mod 4567;
 if Numero = InvertirNumero(Numero) then
  showmessage('El numero es capicua')
 else
  showmessage('El numero no es capicua'); 
end;

Saluditos


La franja horaria es GMT +2. Ahora son las 09:32:18.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Traducción al castellano por el equipo de moderadores del Club Delphi