Open In App

How to reverse a string in R

Last Updated : 06 Sep, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Reversing a string means changing its order so that the last character becomes the first, the second last character becomes the second, and so on. A while loop is a control flow statement used in R programming to execute a block of code repeatedly as long as a specified condition is true. By using a while loop, we can easily reverse a given string in various programming languages.

Example:

input string : geeks

reversed string : skeeg

Concepts related to the topic:

  1. Strings: A sequence of characters in programming, which can be manipulated and processed.
  2. While Loop: A control structure that repeatedly executes a block of code as long as a given condition is true.
  3. Indexing: The ability to access individual characters in a string using their position or index.

Steps needed:

  1. Initialize two variables, one for the original string and one for the reversed string.
  2. Start a while loop with the condition that the length of the original string is greater than 0.
  3. Inside the loop, extract the last character of the original string and append it to the reversed string.
  4. Remove the last character from the original string.
  5. Repeat steps 3 and 4 until all characters are processed.
  6. Print or return the reversed string.

Example 1 : Reverse a string using a while loop

R




reverseStr <- function(str) {
  reversedStr <- ""
  while (nchar(str) > 0) {
    reversedStr <- paste0(reversedStr, substr(str, nchar(str), nchar(str)))
    str <- substr(str, 1, nchar(str) - 1)
  }
  return(reversedStr)
}
 
# Example usage
str <- "Hello, R!"
reversedStr <- reverseStr(str)
print(reversedStr)


Output:

[1] "!R ,olleH"
  • Input: The function reverseStr takes a single argument str, which represents the input string that you want to reverse.
  • Variable Initialization: Inside the function, you initialize an empty string reversedStr to store the reversed version of the input string.
  • While Loop: The while loop runs as long as the length of the input string (str) is greater than 0.
  • Reversing the String: Inside the loop:
  • substr(str, nchar(str), nchar(str)): Extracts the last character of the current str.
  • paste0(reversedStr, ...): Concatenates the last character to the reversedStr, effectively reversing the string.
  • str <- substr(str, 1, nchar(str) - 1): Removes the last character from the str.
  • Loop Continuation: The loop continues to extract characters from the end of the input string and build the reversed string until the input string becomes empty.
  • Return: After the loop completes, the function returns the reversedStr.

Example 2: Reverse a string using a repeat loop

R




reverseStr <- function(str) {
  reversedStr <- ""
  repeat {
    if (nchar(str) == 0) break
    reversedStr <- paste0(reversedStr, substr(str, nchar(str), nchar(str)))
    str <- substr(str, 1, nchar(str) - 1)
  }
  return(reversedStr)
}
 
# Example usage
str <- "R is awesome!"
reversedStr <- reverseStr(str)
print(reversedStr)


Output:

[1] "!emosewa si R"
  • reverseStr <- function(str).
  • This line defines the function named reverseStr that takes one parameter, str, which represents the input string.
  • reversedStr <- "":
  • Initializes an empty string named reversedStr. This variable will be used to store the reversed string.
  • repeat { ... }:
  • This initiates a repeat loop, which is a type of loop that continues executing until the break statement is encountered.
  • if (nchar(str) == 0) break:
  • This checks whether the length of the string str is zero. If it is, the loop breaks because there’s nothing left to reverse.
  • reversedStr <- paste0(reversedStr, substr(str, nchar(str), nchar(str))):
  • This line takes the last character of the original str and appends it to the reversedStr.
  • The paste0() function is used to concatenate strings without adding any space or separator between them.
  • substr(str, nchar(str), nchar(str)) retrieves the last character of the string.
  • str <- substr(str, 1, nchar(str) - 1):
  • This line modifies the original str by removing its last character.
  • It uses the substr() function to extract characters from position 1 to nchar(str) - 1.

Example 3: Reverse a string using a while loop with pointers

R




reverseStr <- function(str) {
  reversedStr <- ""
  ptr <- nchar(str)
  while (ptr > 0) {
    reversedStr <- paste0(reversedStr, substr(str, ptr, ptr))
    ptr <- ptr - 1
  }
  return(reversedStr)
}
 
# Example usage
str <- "Loop in R!"
reversedStr <- reverseStr(str)
print(reversedStr)


Output:

[1] "!R ni pooL"
  • reverseStr <- function(str).
  • This line defines the function named reverseStr that takes one parameter, str, which represents the input string.
  • reversedStr <- "":
  • Initializes an empty string named reversedStr. This variable will be used to store the reversed string.
  • repeat { ... }:
  • This initiates a repeat loop, which is a type of loop that continues executing until the break statement is encountered.
  • if (nchar(str) == 0) break:
  • This checks whether the length of the string str is zero. If it is, the loop breaks because there’s nothing left to reverse.


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads