Print number of words, vowels and frequency of each character
Given a string str with uppercase, lowercase and special characters. The input string is to end with either a space or a dot. The problem is to calculate the number of words, vowels and frequency of each character of the string in a separate line.
Example :
Input : How Good GOD Is.
Output :
Number of words = 4
Number of vowels = 5
Number of upper case characters = 6
Character = Frequency = 3
Character = . Frequency = 1
Character = D Frequency = 1
Character = G Frequency = 2
Character = H Frequency = 1
Character = I Frequency = 1
Character = O Frequency = 1
Character = d Frequency = 1
Character = o Frequency = 3
Character = s Frequency = 1
Character = w Frequency = 1
Approach : We use a TreeMap to store characters and their frequencies. TreeMap is used to get the output in sorted order.
Below is Java implementation of above approach :
C++
#include <bits/stdc++.h>
using namespace std;
void words(string str)
{
int wcount = 0, ucount = 0, vcount = 0;
for ( int i = 0; i < str.length(); i++)
{
char c = str[i];
switch (c)
{
case ' ' :
case '.' :
wcount++;
}
switch (c)
{
case 'A' :
case 'E' :
case 'I' :
case 'O' :
case 'U' :
case 'a' :
case 'e' :
case 'i' :
case 'o' :
case 'u' :
vcount++;
}
if (c >= 65 and c <= 90) ucount++;
}
cout << "Number of words = "
<< wcount << endl;
cout << "Number of vowels = "
<< vcount << endl;
cout << "Number of upper case characters = "
<< ucount << endl;
}
void frequency(string str)
{
map< char , int > hmap;
for ( int i = 0; i < str.length(); i++)
hmap[str[i]]++;
for ( auto i : hmap)
{
cout << "Character = " << i.first;
cout << " Frequency = "
<< i.second << endl;
}
}
int main( int argc, char const *argv[])
{
string str = "Geeks for Geeks." ;
words(str);
frequency(str);
return 0;
}
|
Java
import java.util.*;
import java.lang.*;
import java.io.*;
public class Stringfun
{
String str = "Geeks for Geeks." ;
void words()
{
int wCount = 0 , uCount = 0 , vCount = 0 ;
for ( int i = 0 ; i < str.length(); i++)
{
char c = str.charAt(i);
switch (c)
{
case ' ' :
case '.' :
wCount++;
}
switch (c)
{
case 'A' :
case 'E' :
case 'I' :
case 'O' :
case 'U' :
case 'a' :
case 'e' :
case 'i' :
case 'o' :
case 'u' :
vCount++;
}
if (c >= 65 && c <= 90 )
{
uCount++;
}
}
System.out.println( "Number of words = " + wCount);
System.out.println( "Number of vowels = " + vCount);
System.out.println( "Number of upper case characters = "
+ uCount);
}
void frequency()
{
TreeMap<Character, Integer> hmap =
new TreeMap<Character, Integer>();
for ( int i = 0 ; i < str.length(); i++)
{
Integer c = hmap.get(str.charAt(i));
if (hmap.get(str.charAt(i)) == null )
hmap.put(str.charAt(i), 1 );
else
hmap.put(str.charAt(i), ++c);
}
for (Map.Entry m:hmap.entrySet())
System.out.println( "Character = " + m.getKey() +
" Frequency = " + m.getValue());
}
public static void main(String args[]) throws IOException
{
Stringfun obj = new Stringfun();
obj.words();
obj.frequency();
}
}
|
Python 3
def words( str ):
wcount = vcount = ucount = i = 0
while i < len ( str ):
ch = str [i]
if (ch = = " " or ch = = "." ):
wcount + = 1
if (ch = = "a" or ch = = "e" or
ch = = "i" or ch = = 'o' or ch = = "u" ):
vcount + = 1
if (ch = = "A" or ch = = "E" or
ch = = "I" or ch = = 'O' or ch = = "U" ):
vcount + = 1
if ( ord (ch) > = 65 and ord (ch) < = 90 ):
ucount + = 1
i + = 1
print ( "number of words = " , wcount)
print ( "number of vowels = " , vcount)
print ( "number of upper case characters = " ,
ucount)
def frequency( str ):
i = 1
while i < 127 :
ch1 = chr (i)
c = 0
j = 0
while j < len ( str ):
ch2 = str [j]
if (ch1 = = ch2):
c + = 1
j + = 1
if c > 0 :
print ( "Character:" , ch1 +
" Frequency:" , c)
i + = 1
s = "Geeks for Geeks."
words(s)
frequency(s)
|
C#
using System;
using System.Collections.Generic;
public static class GFG
{
public static void words( string str)
{
int wcount = 0;
int ucount = 0;
int vcount = 0;
for ( int i = 0; i < str.Length; i++) {
char c = str[i];
switch (c) {
case ' ' :
case '.' :
wcount++;
break ;
}
switch (c) {
case 'A' :
case 'E' :
case 'I' :
case 'O' :
case 'U' :
case 'a' :
case 'e' :
case 'i' :
case 'o' :
case 'u' :
vcount++;
break ;
}
if (c >= 65 && c <= 90) {
ucount++;
}
}
Console.Write( "Number of words = " );
Console.Write(wcount);
Console.Write( "\n" );
Console.Write( "Number of vowels = " );
Console.Write(vcount);
Console.Write( "\n" );
Console.Write( "Number of upper case characters = " );
Console.Write(ucount);
Console.Write( "\n" );
}
public static void frequency( string str)
{
Dictionary< char , int > hmap
= new Dictionary< char , int >();
for ( int i = 0; i < str.Length; i++) {
if (hmap.ContainsKey(str[i])) {
hmap[str[i]] = hmap[str[i]] + 1;
}
else {
hmap.Add(str[i], 1);
}
}
foreach ( var i in hmap)
{
Console.Write( "Character = " );
Console.Write(i.Key);
Console.Write( " Frequency = " );
Console.Write(i.Value);
Console.Write( "\n" );
}
}
public static void Main( string [] args)
{
string str = "Geeks for Geeks." ;
words(str);
frequency(str);
}
}
|
Javascript
<script>
function words(str){
let wcount = 0,vcount = 0,ucount =0,i = 0
while (i < str.length){
let ch = str[i]
if (ch == " " || ch == "." )
wcount += 1
if (ch == "a" || ch == "e" ||
ch == "i" || ch == 'o' || ch == "u" )
vcount += 1
if (ch == "A" || ch == "E" ||
ch == "I" || ch == 'O' || ch == "U" )
vcount += 1
if (ch.charCodeAt(0) >= 65 && ch.charCodeAt(0) <= 90){
ucount += 1
}
i += 1
}
document.write( "number of words = " , wcount, "</br>" )
document.write( "number of vowels = " , vcount, "</br>" )
document.write( "number of upper case characters = " ,
ucount, "</br>" )
}
function frequency(str){
let i = 1
while (i < 127){
let ch1 = String.fromCharCode(i);
let c = 0
let j = 0
while (j < str.length){
let ch2 = str[j]
if (ch1 == ch2)
c += 1
j += 1
}
if (c > 0)
document.write( "Character:" , ch1 + " Frequency:" , c, "</br>" )
i += 1
}
}
let s = "Geeks for Geeks."
words(s)
frequency(s)
</script>
|
Output
Number of words = 3
Number of vowels = 5
Number of upper case characters = 2
Character = Frequency = 2
Character = . Frequency = 1
Character = G Frequency = 2
Character = e Frequency = 4
Character = f Frequency = 1
Character = k Frequency = 2
Character = o Frequency = 1
Character = r Frequency = 1
Character = s Frequency = 2
Time Complexity : O(n), where n is the number of characters in the string.
Auxiliary Space : O(1).
Last Updated :
27 Jul, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...