|
CS256 - Principles of Structured Design
Fall 2021
|
Displaying ./code/cs256su21code/midtermSol/p5.c
/*
5. Write a function named charFreq that takes in a character array and a single character, and returns
an integer that tells us how many times that character appears.
Hint: This is along the same lines as the myStrlen in the jun22 directory and the charExists in the
jun15 directory of the in-class code.
*/
#include <stdio.h>
int charFreq(char s[], char c);
int main(){
char str[256];
char ch;
int freq;
printf("Enter a string: ");
fgets(str, 256, stdin);
printf("Enter a character: ");
ch = getchar();
freq = charFreq(str, ch);
printf("The character %c appears %d times in the string.\n", ch, freq);
return 0;
}
int charFreq(char s[], char c){
int count = 0;
for(int i = 0; s[i] != '\0'; i++){
if(s[i] == c)
count++;
}
return count;
}
|