Introduction
This article will demonstrate to you the top 10C# string technical interview questions and their answers. This is specific to the C# string, and it is useful for beginners and experienced. 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 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 problems. 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 twoStringBuilderobjects as aresultandduplicateChar. We will simply loop through string chars and keep track if a char exists or not.
using System;
using System.Collections.Generic;
using System.Text;
namespace DataStructureDemo.Example
{
public class StringDataStructure
{
// Find the duplicate characters
public StringBuilder GetDuplicateCharacters(string input)
{
StringBuilder result = new StringBuilder();
HashSet<char> seenChars = new HashSet<char>();
foreach (char item in input)
{
if (!seenChars.Contains(char.ToLower(item)))
{
seenChars.Add(char.ToLower(item));
}
else if (result.ToString().IndexOf(char.ToLower(item)) == -1)
{
result.Append(item);
}
}
return result;
}
}
}
Let's run the program. First, create an object of theStringDataStructureclass and call theGetDuplicateCharactermethod. This method accepts one argument as a string.
StringDataStructure _stringDT = new StringDataStructure();
// Find the duplicate characters in the string
var duplicates = _stringDT.GetDuplicateCharacters("google");
WriteLine("Duplicate characters in 'google' are: " + duplicates);
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 duplicates in the word "google".

Problem 2. Get all unique characters in a string
Solution. This is just the opposite of problem 1. In problem 1, we are trying to find duplicate characters, but here, we are trying to find all unique characters. This means just remove duplicate characters, and you will get a unique characters list. If you compare the above and below code, we are now returning the result objectwhere all unique characters are available.
using System;
using System.Collections.Generic;
using System.Text;
namespace DataStructureDemo.Example
{
public class StringDataStructure
{
// Find the unique characters
public StringBuilder GetUniqueCharacters(string input)
{
StringBuilder result = new StringBuilder();
HashSet<char> seenChars = new HashSet<char>();
foreach (char item in input)
{
if (!seenChars.Contains(char.ToLower(item)))
{
seenChars.Add(char.ToLower(item));
result.Append(item);
}
}
return result;
}
}
}
Let's run the program and pass a string as "google" in a method GetUniqueCharFromStringas an argument.
using System;
using System.Collections.Generic;
using System.Text;
namespace DataStructureDemo.Example
{
public class StringDataStructure
{
// Remove duplicate characters and return the unique characters
public StringBuilder GetUniqueCharFromString(string input)
{
StringBuilder result = new StringBuilder();
HashSet<char> seenChars = new HashSet<char>();
foreach (char item in input)
{
if (!seenChars.Contains(char.ToLower(item)))
{
seenChars.Add(char.ToLower(item));
result.Append(item);
}
}
return result;
}
}
}
Output.Here, you can see the output as"gole". This means after removing duplicate characters from the word "google," we get the "gole" which are unique chars.

Problem 3.Reverse a string
Solution. Here, we have to reverse a string. For example, if we pass a string as "hello," then the 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 secondlast index (length- 1)and the output will be added into another string object, "result".
using System;
using System.Collections.Generic;
using System.Text;
namespace DataStructureDemo.Example
{
public class StringDataStructure
{
// Reverse the given string
public string ReverseString(string input)
{
if (string.IsNullOrEmpty(input))
return string.Empty;
StringBuilder result = new StringBuilder();
for (int i = input.Length - 1; i >= 0; i--)
{
result.Append(input[i]);
}
return result.ToString();
}
}
}
Let's run the program and pass an argument string as "google" into the method "ReverseString".
StringDataStructure _stringDT = new StringDataStructure();
// Reverse the single string
var reversedValue = _stringDT.ReverseString("google");
WriteLine("Reversed string for 'google' is = " + reversedValue);
Output.The output will be as follows.

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 a string array and then reverse each word one by one as follows.
using System;
using System.Collections.Generic;
using System.Text;
namespace DataStructureDemo.Example
{
public class StringDataStructure
{
// Reverse each word of the sentence
public string ReverseEachString(string input)
{
if (string.IsNullOrEmpty(input))
return string.Empty;
string[] words = input.Split(' ');
StringBuilder result = new StringBuilder();
for (int i = 0; i < words.Length; i++)
{
result.Append(ReverseString(words[i]));
if (i != words.Length - 1)
{
result.Append(" ");
}
}
return result.ToString();
}
// Reverses a single string
private string ReverseString(string str)
{
StringBuilder reversed = new StringBuilder();
for (int i = str.Length - 1; i >= 0; i--)
{
reversed.Append(str[i]);
}
return reversed.ToString();
}
}
}
Let's run the program and pass the argument as a string"My name ismukesh"into the methodReverseEachString.
StringDataStructure _stringDT = new StringDataStructure();
// Reverse the string with multiple words
var reversedValue = _stringDT.ReverseEachString("My name is mukesh");
WriteLine("Reversed string for 'My name is mukesh' is = " + reversedValue);
Output.The output will similar to as follows.








Join the conversation! Your thoughts help the community grow.