-
-
Save parzibyte/37cf5b36d1ad9ed27ba3997b64775394 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Dos formas de invertir una cadena o string | |
en C# | |
@author parzibyte | |
*/ | |
using System; | |
class MainClass { | |
public static void Main (string[] args) { | |
Console.WriteLine ("Hola mundo"); | |
Console.WriteLine (InvertirManualmente("Hola mundo")); | |
Console.WriteLine (Invertir("Hola mundo")); | |
} | |
public static string InvertirManualmente(string cadena){ | |
string cadenaInvertida = ""; | |
// Recorrer cadena letra por letra | |
foreach(char letra in cadena){ | |
// Anteponer la letra a la cadena invertida | |
cadenaInvertida = letra + cadenaInvertida; | |
} | |
return cadenaInvertida; | |
} | |
public static string Invertir(string cadena){ | |
// Convertir a arreglo | |
char[] cadenaComoCaracteres = cadena.ToCharArray(); | |
// Invertir el arreglo usando métodos incorporados | |
Array.Reverse(cadenaComoCaracteres); | |
// Convertir de nuevo el arreglo a cadena | |
return new string(cadenaComoCaracteres); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment