R Program to Convert String to Uppercase

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

  1. Prompt the User for Input: Use the readline() function to take a string as input from the user.
  2. Convert the String to Uppercase: Use the toupper() function to convert the string to uppercase.
  3. 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 variable input_string.

Step 2: Convert the String to Uppercase

  • The toupper() function is used to convert all characters in the string input_string to uppercase. The result is stored in the variable uppercase_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.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top