1. Reverse string without built-in function
Code
string input = "Hello";
string result = "";
for (int i = input.Length - 1; i >= 0; i--)
{
result += input[i];
}
Console.WriteLine(result);2. Remove duplicate characters from string
Code
string input = "programming";
string output = new string(input.Distinct().ToArray());
Console.WriteLine(output);3. Count occurrences of each character
Code
string s = "hello";
var result = s.GroupBy(x => x)
.Select(g => new { Char = g.Key, Count = g.Count() });
foreach (var item in result)
Console.WriteLine(item.Char + " = " + item.Count);4. Find 2nd highest number in array
Code
int[] a = { 4, 8, 1, 9, 3, 7 };
int second = a.OrderByDescending(x => x).Skip(1).First();
Console.WriteLine(second);5. Check Palindrome
Code
string s = "madam";
bool isPal = s == new string(s.Reverse().ToArray());
Console.WriteLine(isPal);6. Find Prime numbers in range
Code
for (int i = 2; i <= 50; i++)
{
bool prime = true;
for (int j = 2; j <= Math.Sqrt(i); j++)
if (i % j == 0) { prime = false; break; }
if (prime) Console.Write(i + " ");
}
Join the conversation! Your thoughts help the community grow.