This program finds the first unique character in a given text.
The easiest way to run the program is through Replit. Click the button below to instantly launch the program in Replit.
To run the program locally, follow these steps:
- Clone the repository:
git clone https://github.com/jaroshevskii/first-unique-character.git
- Navigate to the project directory:
cd first-unique-character
- Compile and run the program:
swift run
The program defines the function firstUniqueCharacter(in text: String) -> Character?
, which takes text as input and returns the first unique character found in the text, or `nil' if there are no unique characters.
The function performs the following steps to find the first unique character:
- Splits the input text into separate words using
.whitespacesAndNewlines
as a delimiter. - Initializes an empty array
uniqueCharacters
to store the found unique characters from each word. - Searches for each word in the text.
- For each word, iterates over each character in the word.
- Checks if a character appears only once in a word.
- If the character is unique in the word, adds it to the
uniqueCharacters
array and exits the loop. - After processing all the words, it iterates over the unique characters in the
uniqueCharacters
array. - Returns the first character that occurs only once, or
nil
if no unique characters are found.
Given the provided text:
The Tao gave birth to machine language.
Machine language gave birth to the assembler.
The assembler gave birth to the compiler.
Now there are ten thousand languages.
Each language has its purpose, however humble.
Each language expresses the Yin and Yang of software.
Each language has its place within the Tao.
But do not program in COBOL if you can avoid it.
-- Geoffrey James, "The Tao of Programming"
The program will find the first unique character in the text, which is 'm'. It will then print the following message:
The first unique character in the text is 'm'.
If there are no unique characters in the text, the program will print:
There are no unique characters in the text.
Licensed under the MIT License.