Comment trouver des voyelles, des consonnes, des chiffres et des caractères spéciaux dans une chaîne

Une chaîne est une séquence de caractères. Ces caractères peuvent être des voyelles, des consonnes, des chiffres ou tout autre caractère spécial. Dans cet article, vous apprendrez comment trouver le nombre total de voyelles, de consonnes, de chiffres et de caractères spéciaux dans une chaîne donnée.

Exemples pour comprendre le problème

Exemple 1 : Soit la chaîne donnée "Welcome 2 #MUO".

s = "Bienvenue 2 #MUO"

Il y a 5 voyelles dans la chaîne donnée : e , o , e , U et O .

Il y a 5 consonnes dans la chaîne donnée : W , l , c , m et M .

Il y a 1 chiffre dans la chaîne donnée : 2 .

Il y a 3 caractères spéciaux dans la chaîne donnée : # et deux espaces blancs.

Exemple 2 : Soit la chaîne donnée "This is @ inpuT String 2".

s = "Ceci est @ Chaîne d'entrée 2"

Il y a 5 voyelles dans la chaîne donnée : i , I , I , u et i .

Il y a 12 consonnes dans la chaîne donnée : T , h , s , s , n , p , T , S , t , r , n , et g .

Il y a 1 chiffre dans la chaîne donnée : 2 .

Il y a 6 caractères spéciaux dans la chaîne donnée : @ et cinq espaces blancs.

Remarque : l' espace blanc est traité comme un caractère spécial dans la chaîne.

Approche pour compter les voyelles, les consonnes, les chiffres et les caractères spéciaux dans une chaîne

Vous pouvez trouver le nombre total de voyelles, de consonnes, de chiffres et de caractères spéciaux dans une chaîne en suivant l'approche ci-dessous :

  1. Initialisez les variables pour compter le nombre total de voyelles, de consonnes, de chiffres et de caractères spéciaux.
  2. Parcourt la chaîne donnée caractère par caractère.
  3. Vérifiez si le caractère appartient à la famille alphabétique, à la famille de chiffres ou à la famille de caractères spéciaux.
  4. Si le caractère appartient à la famille de l'alphabet, convertissez d'abord le caractère en minuscule, puis vérifiez si le caractère est une voyelle ou une consonne.
    • Si le caractère est une voyelle, incrémentez la valeur de la variable qui stocke le nombre total de voyelles dans une chaîne.
    • Sinon, si le caractère est une consonne, incrémente la valeur de la variable qui stocke le nombre total de consonnes dans une chaîne.
  5. Si le caractère appartient à la famille de chiffres, incrémentez la valeur de la variable qui stocke le nombre total de chiffres dans une chaîne.
  6. Si le caractère appartient à la famille de caractères spéciaux, incrémentez la valeur de la variable qui stocke le nombre total de caractères spéciaux dans une chaîne.

Programme C++ pour compter les voyelles, les consonnes, les chiffres et les caractères spéciaux dans une chaîne

Vous trouverez ci-dessous le programme C++ pour compter les voyelles, les consonnes, les chiffres et les caractères spéciaux dans une chaîne :

En relation: Les meilleures chaînes YouTube codées pour apprendre la programmation

 #include <iostream>
using namespace std;
void countCharactersCategory(string s)
{
int totalSpecialCharacters = 0, totalDigits = 0, totalVowels = 0, totalConsonants = 0;
for (int i = 0; i < s.length(); i++)
{
char c = s[i];
// Alphabets family
if ( (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') )
{
// Converting character to lower case
c = tolower(c);
// Vowels
if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u')
{
totalVowels++;
}
// Consonants
else
{
totalConsonants++;
}
}
// Digits family
else if (c >= '0' && c <= '9')
{
totalDigits++;
}
// Special characters family
else
{
totalSpecialCharacters++;
}
}
cout << "Total no. of vowels in the given string: " << totalVowels << endl;
cout << "Total no. of consonants in the given string: " << totalConsonants << endl;
cout << "Total no. of digits in the given string: " << totalDigits << endl;
cout << "Total no. of special characters in the given string: " << totalSpecialCharacters << endl;
}
// Driver code
int main()
{
// Test case: 1
string s1 = "Welcome 2 #MUO";
cout << "Input string: " << s1 << endl;
countCharactersCategory(s1);
// Test case: 2
string s2 = "This Is @ InpuT String 2";
cout << "Input string: " << s2 << endl;
countCharactersCategory(s2);
return 0;
}

Production:

 Input string: Welcome 2 #MUO
Total no. of vowels in the given string: 5
Total no. of consonants in the given string: 5
Total no. of digits in the given string: 1
Total no. of special characters in the given string: 3
Input string: This Is @ InpuT String 2
Total no. of vowels in the given string: 5
Total no. of consonants in the given string: 12
Total no. of digits in the given string: 1
Total no. of special characters in the given string: 6

Programme Python pour compter les voyelles, les consonnes, les chiffres et les caractères spéciaux dans une chaîne

Vous trouverez ci-dessous le programme Python pour compter les voyelles, les consonnes, les chiffres et les caractères spéciaux dans une chaîne :

Connexe : Apprendre Python ? Voici comment manipuler des chaînes

 
def countCharactersCategory(s):
totalSpecialCharacters = 0
totalDigits = 0
totalVowels = 0
totalConsonants = 0

for i in range(0, len(s)):
c = s[i]
# Alphabets family
if ( (c >= 'a' and c = 'A' and c = '0' and c <= '9'):
totalDigits += 1
# Special characters family
else:
totalSpecialCharacters += 1
print("Total no. of vowels in the given string: ", totalVowels)
print("Total no. of consonants in the given string: ", totalConsonants)
print("Total no. of digits in the given string: ", totalDigits)
print("Total no. of special characters in the given string: ", totalSpecialCharacters)

# Driver code
# Test case: 1
s1 = "Welcome 2 #MUO"
print("Input string: ", s1)
countCharactersCategory(s1)
# Test case: 2
s2 = "This Is @ InpuT String 2"
print("Input string: ", s2)
countCharactersCategory(s2)

Production:

 Input string: Welcome 2 #MUO
Total no. of vowels in the given string: 5
Total no. of consonants in the given string: 5
Total no. of digits in the given string: 1
Total no. of special characters in the given string: 3
Input string: This Is @ InpuT String 2
Total no. of vowels in the given string: 5
Total no. of consonants in the given string: 12
Total no. of digits in the given string: 1
Total no. of special characters in the given string: 6

En relation: Comment valider des chaînes à l'aide de méthodes booléennes en Python

Programme C pour compter les voyelles, les consonnes, les chiffres et les caractères spéciaux dans une chaîne

Vous trouverez ci-dessous le programme C pour compter les voyelles, les consonnes, les chiffres et les caractères spéciaux dans une chaîne :

 #include <stdio.h>
#include <ctype.h>
#include <string.h>
void countCharactersCategory(char s[])
{
int totalSpecialCharacters = 0, totalDigits = 0, totalVowels = 0, totalConsonants = 0;
for (int i = 0; i < strlen(s); i++)
{
char c = s[i];
// Alphabets family
if ( (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') )
{
// Converting character to lower case
c = tolower(c);
// Vowels
if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u')
{
totalVowels++;
}
// Consonants
else
{
totalConsonants++;
}
}
// Digits family
else if (c >= '0' && c <= '9')
{
totalDigits++;
}
// Special characters family
else
{
totalSpecialCharacters++;
}
}
printf("Total no. of vowels in the given string: %d ⁠n",totalVowels);
printf("Total no. of consonants in the given string: %d ⁠n",totalConsonants);
printf("Total no. of digits in the given string: %d ⁠n",totalDigits);
printf("Total no. of special characters in the given string: %d ⁠n",totalSpecialCharacters);
}
// Driver code
int main()
{
// Test case: 1
char s1[] = "Welcome 2 #MUO";
printf("Input string: %s
",s1);
countCharactersCategory(s1);
// Test case: 2
char s2[] = "This Is @ InpuT String 2";
printf("Input string: %s
",s2);
countCharactersCategory(s2);
return 0;
}

Production:

 Input string: Welcome 2 #MUO
Total no. of vowels in the given string: 5
Total no. of consonants in the given string: 5
Total no. of digits in the given string: 1
Total no. of special characters in the given string: 3
Input string: This Is @ InpuT String 2
Total no. of vowels in the given string: 5
Total no. of consonants in the given string: 12
Total no. of digits in the given string: 1
Total no. of special characters in the given string: 6

Programme JavaScript pour compter les voyelles, les consonnes, les chiffres et les caractères spéciaux dans une chaîne

Vous trouverez ci-dessous le programme JavaScript pour compter les voyelles, les consonnes, les chiffres et les caractères spéciaux dans une chaîne :

 <script>
function countCharactersCategory(s) {
var totalSpecialCharacters = 0, totalDigits = 0, totalVowels = 0, totalConsonants = 0;
for (var i = 0; i < s.length; i++) {
var c = s[i];
// Alphabets family
if ( (c >= "a" && c <= "z") || (c >= "A" && c <= "Z") ) {
// Converting character to lower case
c = c.toLowerCase();
// Vowels
if (c == "a" || c == "e" || c == "i" || c == "o" || c == "u") {
totalVowels++;
}
// Consonants
else {
totalConsonants++;
}
}
// Digits family
else if (c >= "0" && c <= "9") {
totalDigits++;
}
// Special characters family
else {
totalSpecialCharacters++;
}
}
document.write("Total no. of vowels in the given string: " + totalVowels + "<br>");
document.write("Total no. of consonants in the given string: " + totalConsonants + "<br>");
document.write("Total no. of digits in the given string: " + totalDigits + "<br>");
document.write("Total no. of special characters in the given string: " + totalSpecialCharacters + "<br>");
}
// Test case: 1
var s1 = "Welcome 2 #MUO";
document.write("Input string: " + s1 + "<br>");
countCharactersCategory(s1);
// Test case: 2
var s2 = "This Is @ InpuT String 2";
document.write("Input string: " + s2 + "<br>");
countCharactersCategory(s2);
</script>

Production:

 Input string: Welcome 2 #MUO
Total no. of vowels in the given string: 5
Total no. of consonants in the given string: 5
Total no. of digits in the given string: 1
Total no. of special characters in the given string: 3
Input string: This Is @ InpuT String 2
Total no. of vowels in the given string: 5
Total no. of consonants in the given string: 12
Total no. of digits in the given string: 1
Total no. of special characters in the given string: 6

Si vous souhaitez consulter le code source complet utilisé dans cet article, voici le référentiel GitHub .

Pratiquez les problèmes de cordes pour vos entretiens

Les problèmes de cordes sont l'une des questions les plus posées dans les concours de codage et les interviews. Comprenez les bases des cordes et pratiquez des problèmes célèbres pour devenir un meilleur ingénieur.

Supprimer les caractères en double d'une chaîne, trouver le caractère maximal dans une chaîne et vérifier si une chaîne est un palindrome sont quelques-uns des problèmes de chaîne célèbres.

Pourquoi ne pas essayer ces problèmes aussi ?