Count number of vowels in a string
Here given code implementation process.
// Java program
// count number of vowels in a string
class MyString
{
//Check that whether given character is vowel or not
public boolean is_vowel(char data)
{
if (data == 'a' || data == 'e' || data == 'i' || data == 'o' || data == 'u')
{
return true;
}
return false;
}
//Count number of vowels in string
public void count_vowels(String text)
{
//Get the size
int size = text.length();
if (size <= 0)
{
return;
}
int result = 0;
for (int i = 0; i < size; i++)
{
if (is_vowel(text.charAt(i)) == true)
{
result = result + 1;
}
}
System.out.print("\n Given String : " + text);
System.out.print("\n Vowels : " + result);
}
public static void main(String[] args)
{
MyString obj = new MyString();
String text = "This is a difficult problem";
obj.count_vowels(text);
}
}
Output
Given String : This is a difficult problem
Vowels : 8
//Include header file
#include <iostream>
#include<string.h>
using namespace std;
// C++ program
// count number of vowels in a string
class MyString
{
public:
//Check that whether given character is vowel or not
bool is_vowel(char data)
{
if (data == 'a' || data == 'e' || data == 'i' || data == 'o' || data == 'u')
{
return true;
}
return false;
}
//Count number of vowels in string
void count_vowels(string text)
{
//Get the size
int size = text.size();
if (size <= 0)
{
return;
}
int result = 0;
for (int i = 0; i < size; i++)
{
if (this->is_vowel(text[i]) == true)
{
result = result + 1;
}
}
cout << "\n Given String : " << text;
cout << "\n Vowels : " << result;
}
};
int main()
{
MyString obj = MyString();
string text = "This is a difficult problem";
obj.count_vowels(text);
return 0;
}
Output
Given String : This is a difficult problem
Vowels : 8
//Include namespace system
using System;
// C# program
// count number of vowels in a string
class MyString
{
//Check that whether given character is vowel or not
public Boolean is_vowel(char data)
{
if (data == 'a' || data == 'e' || data == 'i' || data == 'o' || data == 'u')
{
return true;
}
return false;
}
//Count number of vowels in string
public void count_vowels(String text)
{
//Get the size
int size = text.Length;
if (size <= 0)
{
return;
}
int result = 0;
for (int i = 0; i < size; i++)
{
if (is_vowel(text[i]) == true)
{
result = result + 1;
}
}
Console.Write("\n Given String : " + text);
Console.Write("\n Vowels : " + result);
}
public static void Main(String[] args)
{
MyString obj = new MyString();
String text = "This is a difficult problem";
obj.count_vowels(text);
}
}
Output
Given String : This is a difficult problem
Vowels : 8
<?php
// Php program
// count number of vowels in a string
class MyString
{
//Check that whether given character is vowel or not
public function is_vowel($data)
{
if ($data == 'a' || $data == 'e' || $data == 'i' || $data == 'o' || $data == 'u')
{
return true;
}
return false;
}
//Count number of vowels in string
public function count_vowels($text)
{
//Get the size
$size = strlen($text);
if ($size <= 0)
{
return;
}
$result = 0;
for ($i = 0; $i < $size; $i++)
{
if ($this->is_vowel($text[$i]) == true)
{
$result = $result + 1;
}
}
echo "\n Given String : ". $text;
echo "\n Vowels : ". $result;
}
}
function main()
{
$obj = new MyString();
$text = "This is a difficult problem";
$obj->count_vowels($text);
}
main();
Output
Given String : This is a difficult problem
Vowels : 8
// Node Js program
// count number of vowels in a string
class MyString
{
//Check that whether given character is vowel or not
is_vowel(data)
{
if (data == 'a'
|| data == 'e'
|| data == 'i'
|| data == 'o'
|| data == 'u')
{
return true;
}
return false;
}
//Count number of vowels in string
count_vowels(text)
{
//Get the size
var size = text.length;
if (size <= 0)
{
return;
}
var result = 0;
for (var i = 0; i < size; i++)
{
if (this.is_vowel(text[i]) == true)
{
result = result + 1;
}
}
process.stdout.write("\n Given String : " + text);
process.stdout.write("\n Vowels : " + result);
}
}
function main()
{
var obj = new MyString();
var text = "This is a difficult problem";
obj.count_vowels(text);
}
main();
Output
Given String : This is a difficult problem
Vowels : 8
# Python 3 program
# count number of vowels in a string
class MyString :
# Check that whether given character is vowel or not
def is_vowel(self, data) :
if (data == 'a'
or data == 'e'
or data == 'i'
or data == 'o'
or data == 'u') :
return True
return False
# Count number of vowels in string
def count_vowels(self, text) :
# Get the size
size = len(text)
if (size <= 0) :
return
result = 0
i = 0
while (i < size) :
if (self.is_vowel(text[i]) == True) :
result = result + 1
i += 1
print("\n Given String : ", text, end = "")
print("\n Vowels : ", result, end = "")
def main() :
obj = MyString()
text = "This is a difficult problem"
obj.count_vowels(text)
if __name__ == "__main__": main()
Output
Given String : This is a difficult problem
Vowels : 8
# Ruby program
# count number of vowels in a string
class MyString
# Check that whether given character is vowel or not
def is_vowel(data)
if (data == 'a' ||
data == 'e' ||
data == 'i' ||
data == 'o' ||
data == 'u')
return true
end
return false
end
# Count number of vowels in string
def count_vowels(text)
# Get the size
size = text.length()
if (size <= 0)
return
end
result = 0
i = 0
while (i < size)
if (self.is_vowel(text[i]) == true)
result = result + 1
end
i += 1
end
print("\n Given String : ", text)
print("\n Vowels : ", result)
end
end
def main()
obj = MyString.new()
text = "This is a difficult problem"
obj.count_vowels(text)
end
main()
Output
Given String : This is a difficult problem
Vowels : 8
// Scala program
// count number of vowels in a string
class MyString
{
//Check that whether given character is vowel or not
def is_vowel(data: Char): Boolean = {
if (data == 'a'
|| data == 'e'
|| data == 'i'
|| data == 'o'
|| data == 'u')
{
return true;
}
return false;
}
//Count number of vowels in string
def count_vowels(text: String): Unit = {
//Get the size
var size: Int = text.length();
if (size <= 0)
{
return;
}
var result: Int = 0;
var i: Int = 0;
while (i < size)
{
if (is_vowel(text(i)) == true)
{
result = result + 1;
}
i += 1;
}
print("\n Given String : " + text);
print("\n Vowels : " + result);
}
}
object Main
{
def main(args: Array[String]): Unit = {
var obj: MyString = new MyString();
var text: String = "This is a difficult problem";
obj.count_vowels(text);
}
}
Output
Given String : This is a difficult problem
Vowels : 8
// Swift program
// count number of vowels in a string
class MyString
{
//Check that whether given character is vowel or not
func is_vowel(_ data: Character) -> Bool
{
if (data == "a"
|| data == "e"
|| data == "i"
|| data == "o"
|| data == "u")
{
return true;
}
return false;
}
//Count number of vowels in string
func count_vowels(_ data: String)
{
let text = Array(data);
//Get the size
let size: Int = text.count;
if (size <= 0)
{
return;
}
var result: Int = 0;
var i: Int = 0;
while (i < size)
{
if (self.is_vowel(text[i]) == true)
{
result = result + 1;
}
i += 1;
}
print(" Given String : ", data);
print(" Vowels : ", result);
}
}
func main()
{
let obj: MyString = MyString();
let text: String = "This is a difficult problem";
obj.count_vowels(text);
}
main();
Output
Given String : This is a difficult problem
Vowels : 8
Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.
New Comment