Program to reverse a String :
static void ReverseString(string str)
{
char[] charArray = str.ToCharArray();
for(int i=0, j = charArray.Length-1; i<j; i++, j--)
{
charArray[i] = str[j];
charArray[j] = str[i];
}
string reverseString = new string(charArray);
Console.WriteLine(reverseString);
}
Program to find if the given string is a palindrome or not?
static void CheckPalindrome(string str)
{
bool flag = false;
for(int i=0,j= str.Length-1; i<j/2; i++,j-- )
{
if(str[i] != str[j])
{
flag = false;
break;
}else
{
flag=true;
}
}
if(flag)
Console.WriteLine("Palindrome");
else
Console.WriteLine("Not palindrome");
}
Program to reverse the order of words in a given string?
static void ReverseWords(string str)
{
//split the input string into words based on white spaces
string[] words = str.Split(new char[] {' '},
StringSplitOptions.RemoveEmptyEntries);
//Reverse the array of words
Array.Reverse(words);
//Join the reverse array back into single StringSplitoptions
string result = string.Join(" ", words);
Console.WriteLine(result);
}
Program to reverse each word in a given string?
static void ReverseWords(string str)
{
//split the input string into words based on white spaces
string[] words = str.Split(new char[] {' '},
StringSplitOptions.RemoveEmptyEntries);
//Reverse the array of words
for(int i=0; i< words.Length; i++)
{
char[] charArray = words[i].ToCharArray();
Array.Reverse(charArray);
words[i]=new string(charArray);
}
//Join the reverse array back into single StringSplitoptions
string result = string.Join(" ", words);
Console.WriteLine(result);
}
Program to count the occurrence of each character in a string?
static void Countcharacter(string str)
{
Dictionary<char, int> characterCount = new Dictionary<char, int>();
foreach(char character in str)
{
if(character != ' ')
{
if(!characterCount.ContainsKey(character))
{
characterCount.Add(character, 1);
}else
{
characterCount[character]++;
}
}
}
foreach(var character in characterCount)
{
Console.WriteLine("{0} - {1}", character.Key, character.Value);
}
}
Program to remove duplicate characters from a string?
static void RemoveDuplicates(string str)
{
string result = string.Empty;
for(int i=0; i < str.Length; i++)
{
if(!result.Contains(str[i]))
{
result += str[i];
}
}
Console.WriteLine(result);
}
Program to find all possible substring of a given string?
static void findallsubstring(string str)
{
for(int i=0; i< str.Length; i++)
{
StringBuilder subString = new StringBuilder(str.Length - 1);
for(int j=i; j < str.Length; ++j)
{
subString.Append(str[j]);
Console.Write(subString +" ");
}
}
}
Program to have left rotation of an array :
static void LeftRotation(int[] nums)
{
int size = nums.Length;
int temp;
for(int j=size-1; j>0 ; j--)
{
temp = nums[size-1];
nums[size-1] = nums[j-1];
nums[j-1] = temp;
}
foreach(int num in nums)
{
Console.Write(num + " " );
}
}
Program to have right rotation of an array :
static void LeftRotation(int[] nums)
{
int size = nums.Length;
int temp;
for(int j=0; j < size-1 ; j++)
{
temp = nums[0];
nums[0] = nums[j+1];
nums[j+1] = temp;
}
foreach(int num in nums)
{
Console.Write(num + " " );
}
}
Program to find second largest integer in an array using only one loop?
internal static void FindSecondLargeInArray(int[] arr)
{
int max1 = int.MinValue;
int max2 = int.MinValue;
foreach (int i in arr)
{
if (i > max1)
{
max2 = max1;
max1 = i;
}
else if (i >= max2 && i != max1)
{
max2 = i;
}
}
Console.WriteLine(max2); ;
}
Program to merge the sorted array ?
using System;
static int[] MergeSortedArrays(int[] arr1, int[] arr2)
{
int n1 = arr1.Length;
int n2 = arr2.Length;
int[] mergedArray = new int[n1 + n2];
int i = 0, j = 0, k = 0;
// Merge the two arrays
while (i < n1 && j < n2)
{
if (arr1[i] < arr2[j])
{
mergedArray[k++] = arr1[i++];
}
else
{
mergedArray[k++] = arr2[j++];
}
}
// Copy remaining elements of arr1, if any
while (i < n1)
{
mergedArray[k++] = arr1[i++];
}
// Copy remaining elements of arr2, if any
while (j < n2)
{
mergedArray[k++] = arr2[j++];
}
return mergedArray;
}
Program to find 3 largest element in an array ?
static void ThirdLargestInteger(int[] nums)
{
int max1 = int.MinValue;
int max2 = int.MinValue;
int max3 = int.MinValue;
foreach(int num in nums)
{
if(num > max1)
{
max3 = max2;
max2 = max1;
max1 = num;
}else if( num > max2 && num != max1)
{
max3=max2;
max2 = num;
}else if( num > max3 && num != max2 && num != max1)
{
max3 = num;
}
}
Console.WriteLine("The 3 largest number of array is " + max3);
}
Program to find Duplicates in an array ?
static List<int> FindDuplicates(int[] arr)
{
HashSet<int> seen = new HashSet<int>();
List<int> duplicates = new List<int>();
foreach (int number in arr)
{
// If the number is already in the HashSet, it's a duplicate
if (seen.Contains(number))
{
// Add to duplicates if not already added
if (!duplicates.Contains(number))
{
duplicates.Add(number);
}
}
else
{
// Add the number to the HashSet
seen.Add(number);
}
}
return duplicates;
}
Program to find intersection between 2 array's
static List<int> FindIntersection(int[] arr1, int[] arr2)
{
HashSet<int> set = new HashSet<int>(arr1);
List<int> intersection = new List<int>();
foreach (int number in arr2)
{
// Check if the number from arr2 exists in the HashSet
if (set.Contains(number))
{
intersection.Add(number);
}
}
return intersection;
}
Program to check 2 string are anagram or not :
static bool AreAnagrams(string str1, string str2)
{
// Normalize the strings: remove spaces and convert to lowercase
str1 = str1.Replace(" ", "").ToLower();
str2 = str2.Replace(" ", "").ToLower();
// If lengths are different, they cannot be anagrams
if (str1.Length != str2.Length)
{
return false;
}
// Sort the characters of both strings
char[] charArray1 = str1.ToCharArray();
char[] charArray2 = str2.ToCharArray();
Array.Sort(charArray1);
Array.Sort(charArray2);
// Compare the sorted character arrays
return new string(charArray1) == new string(charArray2);
}
Program to replace spaces with specified character ?
static string ReplaceSpaces(string input, char replacementChar)
{
// Use String.Replace to replace spaces with the specified character
return input.Replace(' ', replacementChar);
}
Program to find the angle between hour and minute hands of a clock at any given
time?
internal static void SingleToMulti(int[] array, int row, int column)
{
int index = 0;
int[,] multi = new int[row, column];
for (int y = 0; y < row; y++)
{
for (int x = 0; x < column; x++)
{
multi[y, x] = array[index];
index++;
Console.Write(multi[y, x] + " ");
}
Console.WriteLine();
}
}