Come trovare vocali, consonanti, cifre e caratteri speciali in una stringa
Una stringa è una sequenza di caratteri. Questi caratteri possono essere vocali, consonanti, cifre o qualsiasi carattere speciale. In questo articolo imparerai come trovare il conteggio totale di vocali, consonanti, cifre e caratteri speciali in una determinata stringa.
Esempi per comprendere il problema
Esempio 1 : Sia la stringa data “Welcome 2 #MUO”.
s = “Benvenuto 2 #MUO”
Ci sono 5 vocali nella stringa data: e , o , e , U e O .
Ci sono 5 consonanti nella stringa data: W , l , c , m e M .
C’è 1 cifra nella stringa data: 2 .
Ci sono 3 caratteri speciali nella stringa data: # e due spazi bianchi.
Esempio 2: Lascia che la stringa data sia “This is @ input String 2”.
s = “Questo è @ stringa di input 2”
Ci sono 5 vocali nella stringa data: i , I , I , u e i .
Ci sono 12 consonanti nella stringa data: T , h , s , s , n , p , T , S , t , r , n e g .
C’è 1 cifra nella stringa data: 2 .
Ci sono 6 caratteri speciali nella stringa data: @ e cinque spazi bianchi.
Nota: lo spazio vuoto viene trattato come un carattere speciale nella stringa.
Approccio al conteggio di vocali, consonanti, cifre e caratteri speciali in una stringa
Puoi trovare il numero totale di vocali, consonanti, cifre e caratteri speciali in una stringa seguendo l’approccio seguente:
- Inizializza le variabili per contare il numero totale di vocali, consonanti, cifre e caratteri speciali.
- Attraversa la stringa data carattere per carattere.
- Controlla se il carattere appartiene alla famiglia alfabetica, alla famiglia di cifre o alla famiglia di caratteri speciali.
- Se il carattere appartiene alla famiglia dell’alfabeto, converti prima il carattere in minuscolo e poi controlla se il carattere è una vocale o una consonante.
- Se il carattere è una vocale, incrementa il valore della variabile che memorizza il conteggio totale delle vocali in una stringa.
- Altrimenti, se il carattere è una consonante, incrementa il valore della variabile che memorizza il conteggio totale delle consonanti in una stringa.
- Se il carattere appartiene alla famiglia di cifre, incrementa il valore della variabile che memorizza il conteggio totale delle cifre in una stringa.
- Se il carattere appartiene alla famiglia di caratteri speciali, incrementa il valore della variabile che memorizza il conteggio totale dei caratteri speciali in una stringa.
Programma C++ per contare vocali, consonanti, cifre e caratteri speciali in una stringa
Di seguito è riportato il programma C++ per contare vocali, consonanti, cifre e caratteri speciali in una stringa:
#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;
}
Produzione:
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
Programma Python per contare vocali, consonanti, cifre e caratteri speciali in una stringa
Di seguito è riportato il programma Python per contare vocali, consonanti, cifre e caratteri speciali in una stringa:
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)
Produzione:
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
Programma C per contare vocali, consonanti, cifre e caratteri speciali in una stringa
Di seguito è riportato il programma C per contare vocali, consonanti, cifre e caratteri speciali in una stringa:
#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;
}
Produzione:
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
Programma JavaScript per contare vocali, consonanti, cifre e caratteri speciali in una stringa
Di seguito è riportato il programma JavaScript per contare vocali, consonanti, cifre e caratteri speciali in una stringa:
<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>
Produzione:
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
Se vuoi dare un’occhiata al codice sorgente completo utilizzato in questo articolo, ecco il repository GitHub .
Esercitati sui problemi delle stringhe per le tue interviste
I problemi con le stringhe sono una delle domande più frequenti nei concorsi e nelle interviste di codifica. Comprendi le basi degli archi e metti in pratica problemi famosi per diventare un ingegnere migliore.
Rimuovere i caratteri duplicati da una stringa, trovare il carattere massimo che si verifica in una stringa e controllare se una stringa è palindroma sono alcuni dei famosi problemi di stringa.
Perché non provare anche questi problemi?