Top C# String Technical Interview Questions

This article will demonstrate you, top 10 C# string technical interview questions and their solutions.

This article will demonstrate you, top 10 C# string technical interview questions and their answers. This is specific to C# string and it is useful for beginner and experience both. As a .NET developer, if you are working with C# programming language then most of the time, you have faced some challenges while dealing with strings. We have seen, most of the interviewers also ask string related difficult questions like reversing a string, finding duplicate characters in a string and so on. So, today, we will see, the top 10 technical problems related to string and how to resolve that problem. So, let's start the practical demonstration of the technical problems and their solutions.

Problem 1: Find duplicate characters in a string.

Solution

In this sample code, we have a string and we need to find all duplicate characters in the string. Let create two StringBuilder objects as a result and duplicateChar. We will simply loop through string chars and keep track if a char exists or not.
  1. sing System;  
  2. using System.Collections.Generic;  
  3. using System.Text;  
  4. using static System.Console;  
  5.   
  6. namespace DataStructureDemo.Example  
  7. {  
  8.     public class StringDataStructure  
  9.     {  
  10.         //Find the duplicate character  
  11.         public StringBuilder GetDuplicateCharacter(string _title)  
  12.         {  
  13.             StringBuilder result = new StringBuilder();  
  14.             StringBuilder duplicateChar = new StringBuilder();  
  15.   
  16.             foreach (var item in _title)  
  17.             {  
  18.                 if (result.ToString().IndexOf(item.ToString().ToLower()) == -1)  
  19.                 {  
  20.                     result.Append(item);  
  21.                 }  
  22.                 else  
  23.                 {  
  24.                     duplicateChar.Append(item);  
  25.                 }  
  26.             }  
  27.   
  28.             return duplicateChar;  
  29.         }  
  30.    }  
  31. }  

Let's run the program. First create an object of the StringDataStructure class and call the GetDuplicateCharacter method. This method accepts one argument as a string. 

  1. StringDataStructure _stringDT = new StringDataStructure();  
  2. //Find the duplicate char in string  
  3. var value1 = _stringDT.GetDuplicateCharacter("google");  
  4. WriteLine("Duplicate char in 'google' is = " + value1);  

Output

Here, you can see, we have passed "google" as a string and we got a result as "og". It means these two characters (og) are duplicate in the word "google".

Duplicate char in 'google' is = og

Problem 2: Get all unique characters in a string.

Solution

This is just opposite of problem 1. In problem 1, we are trying to find duplicate characters but here, we are trying to find all unique characters. Means, just remove duplicate characters and you will get a unique characters list. If you compare above and below code, we are now returning the result object where all unique characters are available.
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Text;  
  4. using static System.Console;  
  5.   
  6. namespace DataStructureDemo.Example  
  7. {  
  8.     public class StringDataStructure  
  9.     {          
  10.         //Find the unique character  
  11.         public StringBuilder GetUniqueCharFromString(string _title)  
  12.         {  
  13.             StringBuilder result = new StringBuilder();  
  14.             StringBuilder duplicateChar = new StringBuilder();  
  15.   
  16.             foreach (var item in _title)  
  17.             {  
  18.                 if (result.ToString().IndexOf(item.ToString().ToLower()) == -1)  
  19.                 {  
  20.                     result.Append(item);  
  21.                 }  
  22.                 else  
  23.                 {  
  24.                     duplicateChar.Append(item);  
  25.                 }  
  26.             }  
  27.   
  28.             return result;  
  29.         }  
  30.     }  
  31. }  

Let's run the program and pass a string as "google" in a method GetUniqueCharFromString as an argument. 

  1. //Remove the duplicate char string and show original string  
  2. StringDataStructure _stringDT = new StringDataStructure();  
  3. var value2 = _stringDT.GetUniqueCharFromString("google");  
  4. WriteLine("Unique char in string 'google' is = " + value2);  

Output

Here, you can see the output as "gole". Which means after removing duplicate characters from the word "google" we get the "gole" which are unique chars.

Unique char in string 'google' is = gole

Problem 3: Reverse a string.

Solution

Here, we have to reverse a string. For example, if we pass a string as "hello" then output should be "olleh". For reversing the string, first, we will check the string should not be null or empty. After that we will loop on the string from the second last index (length - 1) and output will be added into another string object "result".
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Text;  
  4. using static System.Console;  
  5.   
  6. namespace DataStructureDemo.Example  
  7. {  
  8.     public class StringDataStructure  
  9.     {  
  10.         // Reverse the single string  
  11.         public string ReverseString(string _title)  
  12.         {  
  13.             string result = "";  
  14.   
  15.             if (string.IsNullOrEmpty(_title))  
  16.                 return string.Empty;  
  17.   
  18.             for (int i = _title.Length - 1; i >= 0; i--)  
  19.             {  
  20.                 result += _title[i];  
  21.             }  
  22.   
  23.             return result;  
  24.         }  
  25.     }  
  26. }  

Let's run the program and pass an argument string as "google" into the method "ReverseString".

  1. StringDataStructure _stringDT = new StringDataStructure();  
  2. //Reverse the single string   
  3. var value3 = _stringDT.ReverseString("google");  
  4. WriteLine("Reversed string for 'google' is = " + value3);  

Output

The output will be like as follows:

Reversed string for 'google' is = elgoog

Problem 4: Reverse each word of the sentence (string).

Solution

Here, we have not only reversed a string but need to reverse a sentence. Here, we will first split the sentence into an string array and then reverse each word one by one as follows.
  1. namespace DataStructureDemo.Example  
  2. {  
  3.     public class StringDataStructure  
  4.     {  
  5.         // Reverse each word of the sentence  
  6.         public string ReverseEachString(string _title)  
  7.         {  
  8.             string result = "";  
  9.             if (string.IsNullOrEmpty(_title))  
  10.                 return string.Empty;  
  11.   
  12.             string[] arr = _title.Split(" ");  
  13.             for (int i = 0; i < arr.Length; i++)  
  14.             {  
  15.                 if (i != arr.Length - 1)  
  16.                 {  
  17.                     result += ReverseString(arr[i]) + " ";  
  18.                 }  
  19.                 else  
  20.                 {  
  21.                     result += ReverseString(arr[i]) + " ";  
  22.                 }  
  23.             }  
  24.   
  25.             return result;  
  26.         }  
  27.     }  
  28. }  

Let's run the program and pass the argument as a string "My name is mukesh" into the method ReverseEachString.

  1. StringDataStructure _stringDT = new StringDataStructure();            
  2. //Reverse the string with multiple word  
  3. var value4 = _stringDT.ReverseEachString("My name is mukesh");  
  4. WriteLine("Reversed string for 'My name is mukesh' is = " + value4);  

Output

The output will similar to as follows.

Reversed string for 'My name is mukesh' is = yM eman si hsekum

Problem 5: Get the word count in a sentence (string).

Solution

Here, we have to count the number of words in a sentence or a string and skip spaces. First, check if the string is not null or empty then we have to split the sentence based on space and count the length using the Length property. 
  1. namespace DataStructureDemo.Example  
  2. {  
  3.     public class StringDataStructure  
  4.     {  
  5.         // Get the count of the word in a sentence  
  6.         public int GetWordCount(string _title)  
  7.         {  
  8.             if (string.IsNullOrEmpty(_title))  
  9.                 return 0;  
  10.   
  11.             _title = _title.Trim();  
  12.   
  13.             var length = _title.Split(' ').Length;  
  14.   
  15.             return length;  
  16.         }  
  17.     }  
  18. }  

Let's run the project and call the GetWordCount method to pass an argument as "My name is mukesh".

  1. StringDataStructure _stringDT = new StringDataStructure();            
  2. //Get the word cound for string  
  3. var value5 = _stringDT.GetWordCount("My name is mukesh");  
  4. WriteLine("Word count for the string 'My name   is mukesh' is = " + value5);  

Output

The output will be as follows.

Word count for the string 'My name is mukesh' is = 4

Problem 6: Check if a string is a palindrome or not.

Solution

Here we have to check that a string is palindrome or not. Palindrome means when you read it from the beginning or from the last, characters should be the same. Here, we will check each character from the beginning and compare it from last.
  1. namespace DataStructureDemo.Example  
  2. {  
  3.     public class StringDataStructure  
  4.     {  
  5.         // Check the string is a palindrome  
  6.         public bool CheckPalindrome(string _title)  
  7.         {  
  8.             bool result = true;  
  9.   
  10.             if (string.IsNullOrEmpty(_title))  
  11.                 return false;  
  12.   
  13.             _title = _title.ToLower().Trim();  
  14.   
  15.             var min = 0;  
  16.             var max = _title.Length - 1;  
  17.   
  18.             while (max >= 0)  
  19.             {  
  20.                 if (_title[min] == _title[max])  
  21.                 {  
  22.                     min++;  
  23.                     max--;  
  24.                 }  
  25.                 else  
  26.                 {  
  27.                     return false;  
  28.                 }  
  29.             }  
  30.   
  31.             return result;  
  32.         }  
  33.     }  
  34. }  

Let's run the program and call the CheckPalindrome method to pass "radar" as an argument.

  1. StringDataStructure _stringDT = new StringDataStructure();  
  2. //Check the string is palindrome  
  3. var value6 = _stringDT.CheckPalindrome("radar");  
  4. WriteLine("Is 'radar' palindrome? = " + value6);  

Output

Here is the output, which says "radar" is a palindrome.

Is 'radar' palindrome? = True

Problem 7: Check max occurrence of a character in the string.

Solution

Here, we have one string and need to find that character whose occurrence is maximum. First, we will convert that string to a character array and then using the loop, add it to dictionary array using a key-value pair. At last, we will check whose has maximum occurrence using the foreach loop.
  1. namespace DataStructureDemo.Example  
  2. {  
  3.     public class StringDataStructure  
  4.     {  
  5.         // Check the max occurance of the any character in the string  
  6.         public char? CheckMaxOccuranceOfChar(string _title)  
  7.         {  
  8.             char? maxOccuranceChar = null;  
  9.             int maxOccuranceValue = 0;  
  10.   
  11.             if (string.IsNullOrEmpty(_title))  
  12.                 return null;  
  13.   
  14.             _title = _title.ToLower().Trim();  
  15.             char[] arr = _title.ToCharArray();  
  16.   
  17.             Dictionary<charint> _dictionary = new Dictionary<charint>();  
  18.             for (int i = 0; i < arr.Length; i++)  
  19.             {  
  20.                 if (arr[i] != ' ')  
  21.                 {  
  22.                     if (!_dictionary.ContainsKey(arr[i]))  
  23.                     {  
  24.                         _dictionary.Add(arr[i], 1);  
  25.                     }  
  26.                     else  
  27.                     {  
  28.                         _dictionary[arr[i]]++;  
  29.                     }  
  30.                 }  
  31.             }  
  32.   
  33.   
  34.             foreach (KeyValuePair<charint> item in _dictionary)  
  35.             {  
  36.                 if (item.Value > maxOccuranceValue)  
  37.                 {  
  38.                     maxOccuranceChar = item.Key;  
  39.                     maxOccuranceValue = item.Value;  
  40.                 }  
  41.             }  
  42.   
  43.             return maxOccuranceChar;  
  44.         }  
  45.     }  
  46. }  

Let's run the program and call the CheckMaxOccurenceOfChar method and pass "Hello World" as an argument.

  1. StringDataStructure _stringDT = new StringDataStructure();  
  2. //Check multiple occurance of a char in string  
  3. var value7 = _stringDT.CheckMaxOccuranceOfChar("Hello World");  
  4. WriteLine("Multiple occurance char in 'Hello world' is = " + value7);  

Output

Here is the output and you can see, a max occurrence of a character is "l".

Multiple occurance char in 'Hello world' is = l

Problem 8: Get all possible substring in a string.

Solution

Here, we have one string and we have to find out all possible substrings in a string. Here, we will use two for loops, one inside another one to create substrings. Based on the loop index value and Substring method, we will create all possible substrings for the provided string.
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Text;  
  4. using static System.Console;  
  5.   
  6. namespace DataStructureDemo.Example  
  7. {  
  8.     public class StringDataStructure  
  9.     {  
  10.         // Get the possible substring in a string  
  11.         public void GetPossibleSubstring(string word)  
  12.         {  
  13.             if (!string.IsNullOrEmpty(word))  
  14.             {  
  15.                 for (int i = 1; i < word.Length; i++)  
  16.                 {  
  17.                     for (int j = 0; j <= word.Length - i; j++)  
  18.                     {  
  19.                         WriteLine(word.Substring(j, i));  
  20.                     }  
  21.                 }  
  22.   
  23.                 ReadLine();  
  24.             }  
  25.         }  
  26.     }  
  27. }  

Let's run the program and call the GetPossibleSubstring method to pass "india" as an argument.

  1. StringDataStructure _stringDT = new StringDataStructure();  
  2. stringDT.GetPossibleSubstring("india");  

Output

Following is the output, here you can find all the possible substring for "india" string.

i
n
d
i
a
in
nd
di
ia
ind
ndi
dia
indi
ndia

Problem 9: Get the first char of each word in capital letter.

Solution

Here, you will get one string, it could be a word or sentence as well. We have to find out the first character of each word and capitalize it. After checking the null or empty string, we will split it into an string array based on space. Then loop through the string array and get the first character of each word using Substring method and capitalize it using ToUpper method.
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Text;  
  4. using static System.Console;  
  5.   
  6. namespace DataStructureDemo.Example  
  7. {  
  8.     public class StringDataStructure  
  9.     {  
  10.         // Get the first char of the each word  
  11.         public void GetFirstCharForEachWord(string sentence)  
  12.         {  
  13.             if (!string.IsNullOrEmpty(sentence))  
  14.             {  
  15.                 string[] arr = sentence.Split(' ');  
  16.   
  17.                 foreach (string item in arr)  
  18.                 {  
  19.                     Write(item.Substring(0, 1).ToUpper() + " ");  
  20.                 }  
  21.   
  22.                 ReadKey();  
  23.             }  
  24.         }  
  25.     }  
  26. }  

Let's run the program and call the GetFirstCharForEachWord method and pass the argument as "I love my india.".

  1. StringDataStructure _stringDT = new StringDataStructure();  
  2. stringDT.GetFirstCharForEachWord("I love my india.");  

Output

Here is the output as the first character of each word with capitalization.

I L M I

Problem 10: Get the index of string using binary search.

Solution

Here, C# provides the binary search mechanism on array, list etc. We can use the BinarySearch method. In this method, you can pass an array or list and the strings that needs to be searched.
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Text;  
  4. using static System.Console;  
  5.   
  6. namespace DataStructureDemo.Example  
  7. {  
  8.     public class StringDataStructure  
  9.     {  
  10.         // Get the binary search on string  
  11.         public void BinarySearchOnstring()  
  12.         {  
  13.             string[] arr = new string[] { "Hi""Guest""I""Mukesh""Am" };  
  14.             Array.Sort(arr); // Am, Guest, Hi, I, Mukesh  
  15.             var index = Array.BinarySearch<string>(arr, "Hi");  
  16.   
  17.             WriteLine("The position of 'Hi' in array is " + index);  
  18.         }  
  19.   
  20.     }  
  21. }  

Let's run the program and call the BinarySearchOnstring method. We are not passing any arguments because we are already creating a string array in the method itself.

  1. StringDataStructure _stringDT = new StringDataStructure();  
  2. _stringDT.BinarySearchOnstring();  

Output

Here is the output, the index of "Hi" is 2.

The position of 'Hi' in array is 2

Conclusion

So, today we have seen how to solve various string related problems.

I hope this post will help you. Please post your feedback using comments below that will help me improve myself for the next post. If you have any questions, please ask your queries in the comment section and If you like this post, please share it with your friends. Thanks

You are here,  it means you have found it useful. Please feel free to connect with me me on @Facebook@Twitter@LinkedIn@Google+.