C# program to determine if the string has all unique characters
Here's a C# program that determines whether a given string has all unique characters:
using System;
using System.Collections.Generic;
class Program
{
static bool HasAllUniqueCharacters(string input)
{
HashSet charSet = new HashSet();
foreach (char c in input)
{
if (charSet.Contains(c))
return false;
charSet.Add(c);
}
return true;
}
static void Main()
{
Console.WriteLine("Enter a string:");
string input = Console.ReadLine();
if (HasAllUniqueCharacters(input))
{
Console.WriteLine("The string has all unique characters.");
}
else
{
Console.WriteLine("The string does not have all unique characters.");
}
}
}
In this program, we have the 'HasUniqueCharacters' method that takes a string as input and returns a boolean indicating whether the string has all unique characters.
We create a boolean array named 'charSet' of size 128, representing ASCII characters from 0 to 127. Each element in the array indicates whether the corresponding character has been encountered.
We iterate through each character c in the input string. For each character, we check if 'charSet[c]' is true, indicating that the character has already been encountered. If it is 'true', we return 'false', as the string does not have all unique characters. Otherwise, we mark 'charSet[c]' as 'true' to indicate that the character has been encountered.
If the loop completes without finding any duplicate characters, it means the string has all unique characters, and we return 'true'.
In the example above, the output would be:
The string has all unique characters.
The string "OpenAI" has all unique characters, so the program indicates that the string has all unique characters.