Introduction
Converting a string to uppercase is a common text processing task. In R, you can easily convert a string to uppercase using the toupper() function. This guide will walk you through writing an R program that converts a user-provided string to uppercase.
Problem Statement
Create an R program that:
- Prompts the user to enter a string.
- Converts the string to uppercase.
- Displays the uppercase string.
Example:
- Input: A string:
"Hello, World!" - Output: Uppercase string:
"HELLO, WORLD!"
Solution Steps
- Prompt the User for Input: Use the
readline()function to take a string as input from the user. - Convert the String to Uppercase: Use the
toupper()function to convert the string to uppercase. - Display the Uppercase String: Use the
print()function to display the result.
R Program
# R Program to Convert String to Uppercase
# Author: Ramesh Fadatare
# Step 1: Prompt the user to enter a string
input_string <- readline(prompt = "Enter a string: ")
# Step 2: Convert the string to uppercase
uppercase_string <- toupper(input_string)
# Step 3: Display the uppercase string
print(paste("Uppercase String:", uppercase_string))
Explanation
Step 1: Prompt the User to Enter a String
- The
readline()function prompts the user to enter a string, storing the input in the variableinput_string.
Step 2: Convert the String to Uppercase
- The
toupper()function is used to convert all characters in the stringinput_stringto uppercase. The result is stored in the variableuppercase_string.
Step 3: Display the Uppercase String
- The
print()function is used to display the uppercase version of the string along with a descriptive message.
Output Example
Example:
Enter a string: Hello, World!
[1] "Uppercase String: HELLO, WORLD!"
Conclusion
This R program demonstrates how to convert a string to uppercase using the toupper() function. It covers basic operations such as taking user input, processing the string to convert it to uppercase, and displaying the result. Converting strings to uppercase is a useful operation in text processing and data manipulation, making this example valuable for anyone learning R programming.