School-Coding-Cpp/sfusi/es1A.cpp

45 lines
778 B
C++

/*
Nome: Mario
Cognome: Montanari
Classe: 3AIN
Data: 07/03/2025
es1A: Scrivere una funzione che, ricevuta una stringa str ed
un carattere ch, restituisca il numero delle occorrenze di ch
Prototipo richiesto:
unsigned charcount(const char *src, const char ch);
Esempio: "taglia le aiuole", 'a' -> 3
*/
#include <iostream>
#define SIZE 100+1
using namespace std;
unsigned charcount(const char *src, const char ch);
int main(void) {
char src[SIZE];
char ch;
cin.getline(src, SIZE);
cin >> ch;
cout << charcount(src, ch);
return 0;
}
unsigned charcount(const char *src, const char ch) {
int contaChar = 0;
for (int i = 0; src[i] != '\0'; i++) {
if (src[i] == ch) {
contaChar++;
}
}
return contaChar;
}