w3resource

Print ASCII values of Lowercase letters in R

R Programming: Control Structure Exercise-18 with Solution

Write a R program to print the ASCII value of all lowercase letters using a for loop.

Sample Solution :

R Programming Code :

# Define a function to print ASCII values of lowercase letters
print_ascii_lowercase <- function() {
    # Iterate through lowercase letters from 'a' to 'z'
    for (letter in letters) {
        # Convert the letter to its ASCII value using 'utf8ToInt' function
        ascii_value <- utf8ToInt(letter)
        
        # Print the letter and its corresponding ASCII value
        cat("Character:", letter, ", ASCII Value:", ascii_value, "\n")
    }
}

# Call the function to print ASCII values of lowercase letters
print_ascii_lowercase()

Output:

Character: a , ASCII Value: 97 
Character: b , ASCII Value: 98 
Character: c , ASCII Value: 99 
Character: d , ASCII Value: 100 
Character: e , ASCII Value: 101 
Character: f , ASCII Value: 102 
Character: g , ASCII Value: 103 
Character: h , ASCII Value: 104 
Character: i , ASCII Value: 105 
Character: j , ASCII Value: 106 
Character: k , ASCII Value: 107 
Character: l , ASCII Value: 108 
Character: m , ASCII Value: 109 
Character: n , ASCII Value: 110 
Character: o , ASCII Value: 111 
Character: p , ASCII Value: 112 
Character: q , ASCII Value: 113 
Character: r , ASCII Value: 114 
Character: s , ASCII Value: 115 
Character: t , ASCII Value: 116 
Character: u , ASCII Value: 117 
Character: v , ASCII Value: 118 
Character: w , ASCII Value: 119 
Character: x , ASCII Value: 120 
Character: y , ASCII Value: 121 
Character: z , ASCII Value: 122         

Explatnaion:

In the exercise above,

  • The function "print_asci_lowercase()" function prints the ASCII values of all lowercase letters.
  • Inside the function, a for loop iterates through each lowercase letter from 'a' to 'z'.
  • For each letter, the "utf8ToInt()" function converts it to its ASCII value.
  • The letter and its corresponding ASCII value are then printed to the console using the "cat()" function.
  • Finally, the function is called to execute the code and print the ASCII values of lowercase letters.

R Programming Code Editor:



Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Power Calculation with Recursion in R.
Next: Check Palindrome number using Recursion in R.

Test your Programming skills with w3resource's quiz.

What is the difficulty level of this exercise?



Follow us on Facebook and Twitter for latest update.