Sample Programs - Strings in R

 

Laboratory Exercises on Strings in R

The following programs are designed to familiarize students with string creation, manipulation, searching, comparison, and pattern matching in R.


1. Find the Length of a String

Aim

Write a program to find the number of characters in a string.

Program

str <- readline("Enter a string: ")

len <- nchar(str)

cat("Length =", len)

Sample Output

Enter a string: Computer Science
Length = 16

2. Convert a String to Uppercase and Lowercase

Aim

Write a program to convert a string to uppercase and lowercase.

Program

str <- readline("Enter a string: ")

cat("Uppercase =", toupper(str), "\n")
cat("Lowercase =", tolower(str))

Sample Output

Enter a string: Computer Science
Uppercase = COMPUTER SCIENCE
Lowercase = computer science

3. Concatenate Two Strings

Aim

Write a program to concatenate two strings.

Program

str1 <- readline("Enter first string: ")
str2 <- readline("Enter second string: ")

result <- paste(str1, str2)

print(result)

Sample Output

Enter first string: R
Enter second string: Programming
[1] "R Programming"

4. Reverse a String

Aim

Write a program to reverse a string.

Program

str <- readline("Enter a string: ")

chars <- strsplit(str, "")[[1]]

rev_str <- ""

for(i in length(chars):1)
{
rev_str <- paste0(rev_str, chars[i])
}

print(rev_str)

Sample Output

Enter a string: HELLO
[1] "OLLEH"

5. Check Whether a String is a Palindrome

Aim

Write a program to determine whether a string is a palindrome.

Program

str <- readline("Enter a string: ")

chars <- strsplit(str, "")[[1]]

rev_str <- paste(rev(chars), collapse="")

if(str == rev_str)
{
print("Palindrome")
}
else
{
print("Not a Palindrome")
}

Sample Output

Enter a string: madam
[1] "Palindrome"

Another method

str <- readline("Enter a string:") rstr="" for ( i in nchar(str):1) { rstr=paste0(rstr,substr(str,i,i)) } if (str==rstr) cat ("pal\n") else cat ("not pal\n")

6. Count the Number of Vowels in a String

Aim

Write a program to count vowels.

Program

str <- tolower(readline("Enter a string: "))

chars <- strsplit(str, "")[[1]]

count <- 0

for(ch in chars)
{
if(ch %in% c("a","e","i","o","u"))
{
count <- count + 1
}
}

cat("Number of vowels =", count)

Sample Output

Enter a string: Computer Science
Number of vowels = 6

7. Count Uppercase, Lowercase, Digits and Special Characters

Aim

Write a program to count uppercase letters, lowercase letters, digits, and special characters.

Program

str <- readline("Enter a string: ")

upper <- lower <- digit <- special <- 0

chars <- strsplit(str, "")[[1]]

for(ch in chars)
{
if(grepl("[A-Z]", ch))
upper <- upper + 1
else if(grepl("[a-z]", ch))
lower <- lower + 1
else if(grepl("[0-9]", ch))
digit <- digit + 1
else
special <- special + 1
}

cat("Uppercase =", upper, "\n")
cat("Lowercase =", lower, "\n")
cat("Digits =", digit, "\n")
cat("Special Characters =", special)

8. Count the Number of Words in a String

Aim

Write a program to count the number of words in a sentence.

Program

str <- readline("Enter a sentence: ")

words <- strsplit(str, " ")[[1]]

cat("Number of words =", length(words))

Sample Output

Enter a sentence: R Programming Language
Number of words = 3

9. Find the Frequency of Each Character

Aim

Write a program to find the frequency of characters in a string.

Program

str <- "programming"

chars <- strsplit(str, "")[[1]]

print(table(chars))

Sample Output

chars
a g i m n o p r
1 2 1 2 1 1 1 2

10. Find Whether One String is a Substring of Another

Aim

Write a program to determine whether a string contains another string.

Program

str1 <- readline("Enter main string: ")
str2 <- readline("Enter substring: ")

if(grepl(str2, str1))
{
print("Substring found")
}
else
{
print("Substring not found")
}

Sample Output

Enter main string: Computer Science
Enter substring: Science
[1] "Substring found"

Comments

Popular posts from this blog

Statistical Methods Lab ( R Language) PCCBL308 Semester 3 KTU BTech CB and CU 2024 Scheme - Dr Binu V P

Programs in R - using control statements - Assignment 2

Basic R Programs to Try - Assignment 1