0

need help with this question

Write a program whose input is a character and a string, and whose output indicates the number of times the character appears in the string. You may assume that the string does not contain spaces and will always contain less than 50 characters. Ex: If the input is: n Monday the output is: 1 Ex: If the input is: z Today is Monday the output is: 0 Ex: If the input is: n It's a sunny day the output is: 2 Case matters. n is different than N. Ex: If the input is: n Nobody the output is: 0 Your program must define and call the following function that returns the number of times the input character appears in the input string. int CountCharacters(char userChar, char* userString)

5th Oct 2020, 5:28 PM
Manesh Divedia
Manesh Divedia - avatar
5 ответов
0
this is what i have down so far #include <stdio.h> #include <string.h> int main() { char ch; char str[50]; int count = 0, i; scanf(" %c", &ch); scanf("%s", str); for (i = 0; i < strlen(str); ++i) { if (str[i] == ch) { ++count; } } printf("%d\n", count); return 0; }
5th Oct 2020, 5:28 PM
Manesh Divedia
Manesh Divedia - avatar
0
https://imgur.com/Y2Jvp45 for some reason im getting error messages which I don't understand
5th Oct 2020, 5:47 PM
Manesh Divedia
Manesh Divedia - avatar
0
so where exactly would implement those in my query
5th Oct 2020, 8:02 PM
Manesh Divedia
Manesh Divedia - avatar
0
should i remove; scanf(" %c", &ch); scanf("%s", str); and put; scanf("%49[^\n]%*c", str);
5th Oct 2020, 8:03 PM
Manesh Divedia
Manesh Divedia - avatar
0
so that means i have to rework my entire query
5th Oct 2020, 8:35 PM
Manesh Divedia
Manesh Divedia - avatar