Program to Count Vowel, Consonant and White Space from String

Program to Count Vowel, Consonant and White Space from String

  • Write a program to Count Vowel, Consonant and White Space from String in C
  • Write a program to Count Vowel, Consonant and White Space from String in C++
  • Write a program to Count Vowel, Consonant and White Space from String in Python
  • Write a program to Count Vowel, Consonant and White Space from String in PHP
  • Write a program to Count Vowel, Consonant and White Space from String in Java
  • Write a program to Count Vowel, Consonant and White Space from String in Java Script
  • Write a program to Count Vowel, Consonant and White Space from String in C#

Explanation:

We must iterate through a string and classify each character as either a vowel, consonant, or white space in order to count the number of vowels, consonants, and white spaces in the string. Every character will be examined, and vowel, consonant, and white space counters will be maintained. Vowels, consonants, and spaces can be counted without considering any other characters, such as punctuation or numbers.

Algorithm:

  1. Initialize counters for vowels, consonants, and white spaces to 0.
  2. Loop through each character in the string.
  3. If the character is a vowel, increment the vowel counter.
  4. If the character is a consonant (alphabetic but not a vowel), increment the consonant counter.
  5. If the character is a white space, increment the white space counter.
  6. Return the final counts.

Program to Count Vowel, Consonant and White Space from String

#include <stdio.h>
#include <ctype.h>

void countCharacters(const char *str, int *vowels, int *consonants, int *spaces) {
    *vowels = *consonants = *spaces = 0;
    for (int i = 0; str[i] != '\0'; i++) {
        char ch = tolower(str[i]);
        if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') {
            (*vowels)++;
        } else if (isalpha(ch)) {
            (*consonants)++;
        } else if (isspace(ch)) {
            (*spaces)++;
        }
    }
}

int main() {
    char str[100];
    int vowels, consonants, spaces;

    printf("Enter a string: ");
    fgets(str, sizeof(str), stdin);

    countCharacters(str, &vowels, &consonants, &spaces);

    printf("Vowels: %d, Consonants: %d, White Spaces: %d\n", vowels, consonants, spaces);
    return 0;
}

#include <iostream>
#include <cctype>
using namespace std;

void countCharacters(const string &str, int &vowels, int &consonants, int &spaces) {
    vowels = consonants = spaces = 0;
    for (char ch : str) {
        ch = tolower(ch);
        if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') {
            vowels++;
        } else if (isalpha(ch)) {
            consonants++;
        } else if (isspace(ch)) {
            spaces++;
        }
    }
}

int main() {
    string str;
    int vowels, consonants, spaces;

    cout << "Enter a string: ";
    getline(cin, str);

    countCharacters(str, vowels, consonants, spaces);

    cout << "Vowels: " << vowels << ", Consonants: " << consonants << ", White Spaces: " << spaces << endl;
    return 0;
}

def count_characters(string):
    vowels = consonants = spaces = 0
    for ch in string.lower():
        if ch in 'aeiou':
            vowels += 1
        elif ch.isalpha():
            consonants += 1
        elif ch.isspace():
            spaces += 1
    return vowels, consonants, spaces

string = input("Enter a string: ")
vowels, consonants, spaces = count_characters(string)
print(f"Vowels: {vowels}, Consonants: {consonants}, White Spaces: {spaces}")

<?php
function countCharacters($string) {
    $vowels = $consonants = $spaces = 0;
    $string = strtolower($string);

    for ($i = 0; $i < strlen($string); $i++) {
        $ch = $string[$i];
        if (in_array($ch, ['a', 'e', 'i', 'o', 'u'])) {
            $vowels++;
        } elseif (ctype_alpha($ch)) {
            $consonants++;
        } elseif (ctype_space($ch)) {
            $spaces++;
        }
    }

    return [$vowels, $consonants, $spaces];
}

echo "Enter a string: ";
$string = trim(fgets(STDIN));
list($vowels, $consonants, $spaces) = countCharacters($string);
echo "Vowels: $vowels, Consonants: $consonants, White Spaces: $spaces\n";
?>

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter a string: ");
        String str = scanner.nextLine();

        int vowels = 0, consonants = 0, spaces = 0;

        for (char ch : str.toLowerCase().toCharArray()) {
            if ("aeiou".indexOf(ch) != -1) {
                vowels++;
            } else if (Character.isLetter(ch)) {
                consonants++;
            } else if (Character.isWhitespace(ch)) {
                spaces++;
            }
        }

        System.out.println("Vowels: " + vowels + ", Consonants: " + consonants + ", White Spaces: " + spaces);
    }
}

function countCharacters(str) {
    let vowels = 0, consonants = 0, spaces = 0;

    for (let ch of str.toLowerCase()) {
        if ("aeiou".includes(ch)) {
            vowels++;
        } else if (/[a-z]/.test(ch)) {
            consonants++;
        } else if (/\s/.test(ch)) {
            spaces++;
        }
    }

    return { vowels, consonants, spaces };
}

const str = prompt("Enter a string:");
const { vowels, consonants, spaces } = countCharacters(str);
console.log(`Vowels: ${vowels}, Consonants: ${consonants}, White Spaces: ${spaces}`);

using System;

class Program {
    static void Main() {
        Console.Write("Enter a string: ");
        string str = Console.ReadLine().ToLower();

        int vowels = 0, consonants = 0, spaces = 0;

        foreach (char ch in str) {
            if ("aeiou".Contains(ch)) {
                vowels++;
            } else if (char.IsLetter(ch)) {
                consonants++;
            } else if (char.IsWhiteSpace(ch)) {
                spaces++;
            }
        }

        Console.WriteLine($"Vowels: {vowels}, Consonants: {consonants}, White Spaces: {spaces}");
    }
}

List of All Programs