Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions string_manipulation/string.rotation.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# String Rotation Check in R
# Checks if one string is a rotation of another string.

is_rotation <- function(s1, s2) {
Copy link

Copilot AI Oct 11, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The function doesn't handle empty strings correctly. When both strings are empty, nchar() returns 0 for both, they pass the length check, and grepl('', '', fixed = TRUE) returns TRUE, which is incorrect since empty strings should not be considered rotations of each other.

Suggested change
is_rotation <- function(s1, s2) {
is_rotation <- function(s1, s2) {
# Explicitly handle both strings empty case
if (nchar(s1) == 0 && nchar(s2) == 0) {
return(FALSE)
}

Copilot uses AI. Check for mistakes.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please check

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What was the result? You haven't changed anything and haven't responded to the comment

# Check length first
if (nchar(s1) != nchar(s2)) {
return(FALSE)
}

# Concatenate s1 with itself and check if s2 is a substring
combined <- paste0(s1, s1)
return(grepl(s2, combined, fixed = TRUE))
}

# Interactive input
s1 <- tolower(readline("Enter first string: "))
s2 <- tolower(readline("Enter second string: "))

if (is_rotation(s1, s2)) {
cat("Yes, '", s2, "' is a rotation of '", s1, "'.\n", sep = "")
} else {
cat("No, '", s2, "' is not a rotation of '", s1, "'.\n", sep = "")
}
Comment on lines +16 to +23
Copy link

Copilot AI Oct 11, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The file name should use a lowercase .r extension instead of .R. According to the project guidelines, all code file names must use lowercase .r extension.

Copilot generated this review using guidance from repository custom instructions.