![]() |
VOOZH | about |
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.
You should have the following in place before using the R programme to count the number of vowels in a string:
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:
RcountVowels <- 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))
[tabbyending]
Output:
[1] "Number of vowels: 4"Example 2:
RcountVowels <- 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))
[tabbyending]
Output:
[1] "Number of vowels: 5"
Example 3:
Rcount_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")
[tabbyending]
Number of vowels: 5
Example 4:
Rlibrary(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")
[tabbyending]
Output:
Number of vowels: 5
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.