How to reverse a string through programming( In C, C#, JAVA)
If you want to reverse a string , just take a string variable and a
character array and the length of the array must be same as
the length of string , You can achieve this by finding the length of
string. And then store the string into character array.
Now to display the string in reverse manner, Just print the array from last index to first index!!!
The example is shown here, it is in C#, you can change it into any of language by just changing the syntax.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
string _myString;
Console.WriteLine("Enter any string ");
_myString = Console.ReadLine();
int _length = _myString.Length;
char[] _reverse = new char[_length];
for (int i = 0; i < _length; i++)
{
_reverse[i] = _myString[i];
}
for (int i = _length-1; i >= 0; i--)
{
Console.Write(_reverse[i]);
}
Console.ReadLine();
}
}
}
Now to display the string in reverse manner, Just print the array from last index to first index!!!
The example is shown here, it is in C#, you can change it into any of language by just changing the syntax.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
string _myString;
Console.WriteLine("Enter any string ");
_myString = Console.ReadLine();
int _length = _myString.Length;
char[] _reverse = new char[_length];
for (int i = 0; i < _length; i++)
{
_reverse[i] = _myString[i];
}
for (int i = _length-1; i >= 0; i--)
{
Console.Write(_reverse[i]);
}
Console.ReadLine();
}
}
}
Comments
Post a Comment