From 2ded8d2c88b6f0f134c08a7e35e1f3fa70018827 Mon Sep 17 00:00:00 2001 From: Supratim <109270340+sgindeed@users.noreply.github.com> Date: Wed, 8 Oct 2025 09:57:26 +0530 Subject: [PATCH] Create unique.letters.count.R --- string_manipulation/unique.letters.count.R | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 string_manipulation/unique.letters.count.R diff --git a/string_manipulation/unique.letters.count.R b/string_manipulation/unique.letters.count.R new file mode 100644 index 00000000..8083ec3f --- /dev/null +++ b/string_manipulation/unique.letters.count.R @@ -0,0 +1,21 @@ +# Ask for user input +input.string <- readline(prompt = "Enter a string: ") + +# Convert to lowercase and remove non-letter characters +# [^a-zA-Z] ensures both uppercase and lowercase letters are kept before conversion +clean.string <- tolower(gsub("[^a-zA-Z]", "", input.string)) + +# Split string into individual letters +letters.vec <- strsplit(clean.string, "")[[1]] + +# Get unique letters +unique.letters <- unique(letters.vec) + +# Count occurrences of each unique letter (only for unique ones) +letter.counts <- table(letters.vec)[unique.letters] + +# Display results +cat("Unique letters and their counts:\n") +for (letter in unique.letters) { + cat(letter, ":", letter.counts[letter], "\n") +}