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:
- Make sure that R or RStudio is installed on your PC. R and RStudio can be downloaded and installed from their respective official websites, respectively (https://www.r-project.org/ and https://rstudio.com/).
- Basic knowledge of R: Get to know the fundamental elements of R programming, including variables, functions, loops, and conditional statements. You can efficiently understand and change the code with the aid of this understanding.
Methodology:
- Iteratively going through each character of the string to determine whether it is a vowel is the standard methodology for counting the number of vowels in a string. A detailed description of the process is provided below:
- Initialise Variables: Create a variable to hold the input string, then initialise a second variable to 0 to keep track of the vowel count.
- Repeat the String Iteratively: Utilise a loop to iterate through the input string’s characters, such as a for loop.
- Examine the vowels: Verify whether each character inside the loop is a vowel. Vowels are commonly “a,” “e,” “i,” “o,” and “u,” and you can accomplish this check using conditional expressions (for example, if-else).
- Increment Count: Increase the vowel count variable if the character is a vowel.
- Repeat: Once you have tested every character in the string, repeat this procedure for each subsequent character.
- Depending on your particular use case, you can either display or return the final vowel count from the function after the loop.
- This method allows you to use R to precisely count the number of vowels in a given string.
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"
- Create a vector vowels containing the vowel characters ‘a’, ‘e’, ‘i’, ‘o’, and ‘u’.
- Initialize a variable count to 0 to keep track of the number of vowels.
- Split the input_string into individual characters using strsplit(input_string, “”)[[1]].
- Iterate through each character in the split string using a for loop.
- Check if the character is present in the vowels vector using the %in% operator.
- If the character is a vowel, increment the count variable by 1.
- After the loop, return the final count value.
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")
Number of vowels: 5
- Using gsub() and Regular Expression
- The function count_vowels uses gsub() to remove all non-vowel characters from the input string, leaving only vowels.
- It then calculates the count of characters using nchar().
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
- Using str_count() from the stringr Package
- The function count_vowels uses the str_count() function from the stringr package with a regular expression to count occurrences of vowels.
- It returns the sum of vowel counts.
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.
- A function called “countVowels” was presented as part of the “For Loop Approach.”
– 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
- Using the ‘gsub()’ function in combination with a regular expression to eliminate non-vowel characters is a different approach that is provided.
– The adjusted string is then used to run ‘nchar()’ to determine the vowel count.
- Using str_count() from the stringr package: This alternative method makes use of the str_count() function, which counts the occurrences of vowels based on a regular expression, from the stringr package.
– 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.