Open In App

R Program to Count the Number of Vowels in a String

In this article, we will discuss how to Count the Number of Vowels in a String with its working example in the R Programming Language. It is a fundamental in programming that allows us to repeatedly execute a block of code as long as a specified condition remains true. It’s often used for tasks like iterating over elements in a data structure, such as printing the elements of a vector. Vowels are the letters ‘a’, ‘e’, ‘i’, ‘o’, and ‘u’. We have to count no. of vowels in a string where a sample string is given.

Prerequisites:

You should have the following in place before using the R programme to count the number of vowels in a string:



Methodology:

Syntax:

countVowels <- function(input_string) {
vowels <- c("a", "e", "i", "o", "u")
count <- 0

for (char in strsplit(input_string, "")[[1]]) {
if (char %in% vowels) {
count <- count + 1
}
}

return(count)
}

Example 1:

R



countVowels <- function(input_string) {
  vowels <- c("a", "e", "i", "o", "u")
  count <- 0
  
  for (char in strsplit(input_string, "")[[1]]) {
    if (char %in% vowels) {
      count <- count + 1
    }
  }
  
  return(count)
}

input_string <- "Hello Geeks"
result <- countVowels(input_string)
print(paste("Number of vowels:", result))

Output:

[1] "Number of vowels: 4"

Example 2:

R

countVowels <- function(input_string) {
  vowels <- c("a", "e", "i", "o", "u")
  count <- 0
  
  for (char in strsplit(input_string, "")[[1]]) {
    if (char %in% vowels) {
      count <- count + 1
    }
  }
  
  return(count)
}

input_string <- "This is my sample string"
result <- countVowels(input_string)
print(paste("Number of vowels:", result))

Output:

[1] "Number of vowels: 5"

Example 3:

R

count_vowels <- function(input_string) {
  vowel_count <- nchar(gsub("[^aeiouAEIOU]", "", input_string))
  return(vowel_count)
}

input_string <- "This is my sample string"
vowel_count <- count_vowels(input_string)
cat("Number of vowels:", vowel_count, "\n")
Output
Number of vowels: 5 

Example 4:

R

library(stringr)

count_vowels <- function(input_string) {
  vowel_count <- str_count(input_string, "[aeiouAEIOU]")
  return(sum(vowel_count))
}

input_string <- "This is my sample string"
vowel_count <- count_vowels(input_string)
cat("Number of vowels:", vowel_count, "\n")

Output:

Number of vowels: 5

Conclusion

In summary, this article has presented a variety of R programming techniques for counting the amount of vowels in a string. Here, vowel counting is discussed using a variety of methods. Vowel counting is a fundamental programming task.

– The code initialises a vector called “vowels” with the vowel characters “a,” “e,” “i,” “o,” and “u,” as well as a count variable that is set to 0.

– The input string is broken up into its component characters, then iterated through.

– The code determines whether a vowel is present in each character and increases the count accordingly.

– The last count is given back

– The adjusted string is then used to run ‘nchar()’ to determine the vowel count.

– The function directly returns the total number of vowels.

These techniques offer versatility and effectiveness for counting vowels in a string, accommodating various tastes and needs of R programmers. One can select the best strategy for their own needs based on the situation and task’s intricacy.


Article Tags :