JULIA
7a. Given a line of text as input, develop a Julia program to determine the frequency with which
each letter of the alphabet is used (make use of dictionary)
# Read input line
println("Enter a line of text:")
text = readline()
# Initialize dictionary
freq = Dict{Char, Int}()
# Process each character
for ch in lowercase(text)
if isletter(ch)
if haskey(freq, ch)
freq[ch] += 1
else
freq[ch] = 1
end
end
end
# Display results
println("\nLetter Frequencies:")
for key in sort(collect(keys(freq)))
println("$key => $(freq[key])")
end
7b. Develop a Julia program to fetch words from a file with arbitrary punctuation and keep
track of all the different words found (make use of set and ignore the case of the letters: e.g. to
and To are treated as the same word).
# Function to extract unique words from a file
function unique_words(filename)
# Create an empty Set
words_set = Set{String}()
# Open and read file
open(filename, "r") do file
for line in eachline(file)
# Convert to lowercase (ignore case)
line = lowercase(line)
# Replace punctuation with space
line = replace(line, r"[^a-z]" => " ")
# Split into words
words = split(line)
# Add words to set
for word in words
push!(words_set, word)
end
end
end
return words_set
end
# Main program
println("Enter file name:")
filename = readline()
try
result = unique_words(filename)
println("\nUnique words found:")
for word in sort(collect(result))
println(word)
end
println("\nTotal unique words: ", length(result))
catch e
println("Error: File not found or cannot be opened!")
end