how to iterate over "thequickbrownfoxjumpsoverthelazydog" to check if it represents every letter of the english alphabet? in ruby

main.rb
def check_alphabet(str)
  alphabet = 'abcdefghijklmnopqrstuvwxyz'
  
  # Convert string to lowercase
  str = str.downcase
  
  # Iterate over each letter in the alphabet
  alphabet.chars.each do |letter|
    unless str.include?(letter)
      return false
    end
  end
  
  # If all letters are present
  return true
end

# Test the function
str = "thequickbrownfoxjumpsoverthelazydog"
if check_alphabet(str)
  puts "The string contains every letter of the English alphabet."
else
  puts "The string does not contain every letter of the English alphabet."
end
560 chars
25 lines

This code defines a function check_alphabet that takes in a string as input. It then performs the following steps:

  1. Defines a variable alphabet which contains all the letters of the English alphabet in lowercase.
  2. Converts the input string str to lowercase.
  3. Iterates over each letter in the alphabet string.
  4. Checks if the current letter is present in the input string using the include? method.
  5. If any letter is not found, it returns false, indicating that not all letters are present.
  6. If all letters are found, it returns true.

Finally, the code tests the function by passing in the given string and prints the appropriate message.

related categories

gistlibby LogSnag