Q How to reverse a string in C# using predefined method ToCharArray()
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ReverseStringConcept
{
class Program
{
static void Main(string[] args)
{
string strInput = null, strOutput = null;
Console.WriteLine("enter a string to revers");
strInput = Console.ReadLine();
char[] charInput = strInput.ToCharArray();
for (int i = charInput.Length-1; i >=0; i--)
{
strOutput = strOutput + strInput[i];
}
Console.WriteLine("reverse---->" + strOutput);
Console.ReadLine();
}
}
}
//ToCharaArray[]è
Copies the characters in this instance to a Unicode character array.
//
// Returns:
// A Unicode character array whose elements are the individual characters of
// this instance. If this instance is an empty string, the returned array is
// empty and has a zero length.
Q. How to reverse a string without any pre define function ?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ReverseStringConcept
{
class Program
{
static void Main(string[] args)
{
string strInput = null, strOutput = null;
int Length;
Console.WriteLine("enter a string to revers");
strInput = Console.ReadLine();
Length = strInput.Length - 1;
while (Length >= 0)
{
strOutput = strOutput + strInput[Length];
Length--;
}
Console.WriteLine("reverse -->" + strOutput);
Console.ReadLine();
}
}
Output
Q Find given String is a palindrom or not?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ReverseStringConcept
{
class Program
{
static void Main(string[] args)
{
string strInput = null, strOutput = null;
Console.WriteLine("enter a string to ");
strInput = Console.ReadLine();
char[] charInput = strInput.ToCharArray();
for (int i = charInput.Length- 1; i >= 0; i--)
{
strOutput = strOutput + charInput[i];
}
//Console.WriteLine(strInput);
//Console.WriteLine(strOutput);
if (strOutput == strInput)
{
Console.WriteLine("palindrom");
}
else
{
Console.WriteLine("Not a palindrom");
}
Console.ReadLine();
}
}
}
Output:
Q. Reverse the string words in the same position
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ReverseStringConcept
{
class Program
{
static void Main(string[] args)
{
string strInput = null, strOutput = null;
Console.WriteLine("enter a string to ");
strInput = Console.ReadLine();
string[] words = strInput.Split(' ');
for (int i = 0; i <= words.Length-1; i++)
{
char[] charInput = words[i].ToCharArray();
for (int j = charInput.Length-1; j >=0; j--)
{
strOutput = strOutput + charInput[j];
}
strOutput += " ";
}
Console.WriteLine("reverse---->" + strOutput);
Console.ReadLine();
}
}
}
Output:
0 comments:
Post a Comment