Suppose you want to reverse a string. This article shows how to do that.
class Program
{
static void Main(string[] args)
{
string s = string.Empty;
s = Console.ReadLine();
char[] arr = s.ToCharArray();
char[] arr1 = new char[s.Length];
string s1 = string.Empty;
for (int i = s.Length - 1; i >= 0; i--)
{
s1 += arr[i];
}
Console.WriteLine(s1);
Console.ReadLine();
}
}
Output
We have now learned how to reverse a string. Okay the next question is, how to check a string to determine whether it is a Palindrome or not.
Note: A string is said to be a Palindrome if the string from left to right is equal to the string from right to left, for example MALAYALAM and so on.
To overcome this we only check the reverse of the string to the original string.
class Program
{
static void Main(string[] args)
{
string s = string.Empty;
s = Console.ReadLine();
char[] arr = s.ToCharArray();
char[] arr1 = new char[s.Length];
string s1 = string.Empty;
for (int i = s.Length - 1; i >= 0; i--)
{
s1 += arr[i];
}
if (s1 == s)
{
Console.WriteLine("Palindrome String");
Console.ReadLine();
}
else
{
Console.WriteLine("Not Palindrome String");
Console.ReadLine();
}
}
}
Output
Now, enter a string and count the number of capital letters, small letters, digits, spaces and special characters.
class Program
{
static void Main(string[] args)
{
string s = string.Empty;
s = Console.ReadLine();
int number = 0;
int capitalLetter = 0;
int smallletter = 0;
int space = 0;
int SpecialSymbol = 0;
char[] a = s.ToCharArray();
for (int i = 0; i < a.Length; i++)
{
if (a[i] >= 48 && a[i] <= 58)
{
number += 1;
}
else if (a[i] >= 65 && a[i] <= 90)
{
capitalLetter += 1;
}
else if (a[i] >= 97 && a[i] <= 122)
{
smallletter += 1;
}
else if (a[i] == 32)
{
space += 1;
}
else
{
SpecialSymbol += 1;
}
}
Console.Write("Capital Letter " + capitalLetter);
Console.Write("\nsmall letter " + smallletter);
Console.Write("\nnumber are " + number);
Console.Write("\nTotal Spaces " + space);
Console.Write("\nTotal Special Symbol are " + SpecialSymbol);
Console.ReadLine();
}
}
Output
Now I will explain how to compare strings using the compare function.
String.compare(string,string2,[optional[true|false])
This function returns 0 if both strings are equal.
It returns -1 if string2 is greater than string1 and returns 1 if string1 is greater than string2.
It takes an optional parameter that is true or false. If we pass true than it ignores case for the comparison.
Example
string s1 = "NANhE";
string s2 = "NANHe";
int n = string.Compare(s1, s2, true);
//int n = s1.CompareTo(s2);
Console.Write(n);
Console.ReadLine();
Output: 0
If
string s1 = "NANhE";
string s2 = "NANHe";
int n = string.Compare(s1, s2, false);
//int n = s1.CompareTo(s2);
Console.Write(n);
Console.ReadLine();
Output: -1
By default it is false.
Now the next string function is the string.contains function.
string s1 = "The quick brown fox jumps over the lazy dog";
string s2 = "lox";
bool b;
b = s1.Contains(s2);
Console.Write(b);
Console.ReadLine();
If the s2 string is found inside the s1 string then this function returns true otherwise false.
Many string functions exist but it is impossible to cover all in this article.