Tuesday, 17 May 2022

C# code to check if two strings are anagrams

 Let's discuss what is Anagram. So in general terms two strings can be said to anagram of each other if  they have same characters irrespective of their order. For example ABCD is anagram of CBDA or DABC. In general we can say LISTEN is anagram of SILENT or POST is anagram of OPTS. So you can find many such words.

Technically how we can do this? So there are several methods available and you can think of any. Here I am going to show one method.

In this method, we will change the casing of each string to either lower or upper so that we can compare them. Now as we all know string is actually an array of characters, so we can sort these string using Array's inbuilt method Array.Sort and after sorting we can easily use the equal operator to check if both strings are equal or not. If they are equal then we can say strings are anagram of each other else not.

Lets see the below code.

There are many other methods through which you can solve this . Try and let us know in the comment section.

Happy Coding

C# code to remove duplicate character in a string

Most common or you can say frequently asked questions in .Net interviews for Beginner to intermediate. There are several ways in .Net using C# through which you can remove or find duplicate characters in a string. 

Lets see one bye one. Suppose we have input string as : India then output should be Inda 

Method 1: Using C# HashSet . 

.Net have many inbuilt data structures with multiple functionalities. HashSet is one of the generic collection data structure which holds unique values only. If you provide the duplicates then it omits the duplicate values and keep only unique values without any exception. 

Issue with this method is like it treats Caps and small case chars different. So either you have to make all characters to upper case or lower case

See below example


Method 2: Using C# Linq's Distinct method on array . 

Linq have many methods through which we can manipulate Collections. So there is inbuilt function which gets distinct items from a collection. Let's see it with example:

Method 3: Using Loop and string's inbuilt methods . 

String type have inbuilt method thorugh which you can check if the string have a character or not using the IndexOf method. This method returns interger or the position of the character in the string. If it's negative means character does'nt exists and if it is greater or equal to 0 then that character exists in that string.
String is actually array of characters. You can either change it to array of character by using method .TocharArray() or can directly treat it as char array. Let's see below example

There are several ways to find this. You can explore and let us know which method you find better and how you did it.
Happy Coding

C# code to reserve a string

Most common or you can say frequenctly asked questions in .Net interviews for Beginner to intermediate.  There are several ways in .Net using C# through which you can reverse a given string.
Lets see one bye one.
Suppose we have input string as : hello then output should be olleh

Method 1: Using inbuild array functions.
So in this method we can split the given string into the character arra using string ToCharArray method and then again we can use Array reverse method to reverse the array and then finally convert it into string. Here is program given which you can execute and see the output.
Method 2: Using Loop.
Using for loop we can add the charatcers of string into another array in reverse order and then we can convert that array to string
Method 3: Using LINQ.
Using System.Linq you can reverse a string in single line. See below code sample

There many more methods through which you can reverse string easily. You can explore more ways yourself and can give you feedback or connect with me as a comment below.