R Program to Count the Number of Vowels in a String
Last Updated :
26 Sep, 2023
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))
[tabbyending]
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))
[tabbyending]
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")
[tabbyending]
OutputNumber 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")
[tabbyending]
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.
Similar Reads
Program to count the number of vowels in a word
Write a program to count the number of vowels in a given word. Vowels include 'a', 'e', 'i', 'o', and 'u' (both uppercase and lowercase). The program should output the count of vowels in the word. Examples: Input: "programming"Output: 3Explanation: The word "programming" has three vowels ('o', 'a',
3 min read
Program to count the number of words in a sentence
Write a program to count the number of words in a given sentence. A word is defined as a sequence of characters separated by spaces. Examples: Input: "The quick brown fox"Output: 4Explanation: The sentence has four words - "The", "quick", "brown", "fox". Input: "The quick brown fox"Output: 4Explanat
3 min read
C# Program to Count Number of Vowels and Consonants in a Given String
C# is a general-purpose programming language it is used to create mobile apps, desktop apps, web sites, and games. As we know that a, e, i, o, u are vowels, and the remaining alphabet is known as a consonant in English so now using C# language we will create a program that will return us the total n
3 min read
Program to count the number of consonants in a word
Write a program to count the number of consonants in a given word. Consonants are the letters of the English alphabet excluding vowels ('a', 'e', 'i', 'o', 'u'). Examples: Input: "programming"Output: 8 Explanation: The eight consonants are: 'p', 'r', 'g', 'r', 'm', 'm', 'n', 'g') Input: "hello"Outpu
5 min read
Program to count the number of characters in a word
Write a program to count the number of characters in a word. Examples: Input: Word: "programming"Output: 11 Input: Word: "hello"Output: 5 Approach: To solve the problem, follow the below idea: To count the number of characters in a word, maintain a variable to count all the characters. Iterate throu
2 min read
How to count the number of sentences in a text in R
A fundamental task in R that is frequently used in text analysis and natural language processing is counting the number of sentences in a text. Sentence counting is necessary for many applications, including language modelling, sentiment analysis, and text summarization. In this article, we'll look
4 min read
Finding the length of string in R programming - nchar() method
nchar() method in R Programming Language is used to get the length of a character in a string object. Syntax: nchar(string) Where: String is object. Return: Returns the length of a string. R - length of string using nchar() ExampleExample 1: Find the length of the string in R In this example, we ar
2 min read
C# Program to Estimate the Frequency of the Word âisâ in a Sentence
Given a string as input, we need to write a program in C# to count the frequency of the word "is" in the string. The task of the program is to count the number of the occurrence of the given word "is" in the string and print the number of occurrences of "is". Examples: Input : string = "The most sim
2 min read
Count the number of times a letter appears in a text file in Python
In this article, we will be learning different approaches to count the number of times a letter appears in a text file in Python. Below is the content of the text file gfg.txt that we are going to use in the below programs: Now we will discuss various approaches to get the frequency of a letter in a
3 min read
Iterating Over Characters of a String in R
In R Language a string is essentially a sequence of characters. Iterating over each character in a string can be useful in various scenarios, such as analyzing text, modifying individual characters, or applying custom functions to each character. This article covers different methods to iterate over
3 min read