In this program, we will learn How to remove the duplicate character from the string in C#.
Console.WriteLine("Enter a string to remove duplicate character from
it:"); //Receive input from the user string inputString = Console.ReadLine(); //Converting the inputString to
lowercase //other wise Uppercase and
lowercase will be considered as difference character inputString =
inputString.ToLower(); //result for holding the distinct
characters string result = string.Empty; //Loop through each character of
the string for (int i = 0; i < inputString.Length;
i++) {
//If character is not added (contains) in the result variable, Add it
to the result string
if
(!result.Contains(inputString[i]))
{ result += inputString[i];
} } //Show the final result to the
user Console.WriteLine(result); //Console.ReadLine() to hold the
screen after the execution of the program Console.ReadLine(); |
Output:
0 comments:
Post a Comment