Question: So I have this code, and its purpose was to fulfill this requirement: Write a function to take a string of no more than 50

So I have this code, and its purpose was to fulfill this requirement:

Write a function to take a string of no more than 50 characters and a character and returns the count of a letter the user inputs. If the letter is not found, the function returns -1. Use the function in main to get two inputs from the user, a sentence and a character, then show the count of that character.

However, I keep getting this error, Exception thrown at 0x0FA1C70E (ucrtbased.dll) in Assignment10.exe: 0xC0000005: Access violation writing location 0x001F0000.

#include #include int getCount(char *st, char c);

int main() { char inputSt[50]; char ch; int count = 0;

printf("Enter a string[not more than 50 characters]: ");// input the sentence scanf("%49[^ ]", inputSt);// maximum number of character that could be stored in the array is 50

printf("Enter a character to find the frequency[function is case sensitive]: "); // input the letter whose occurrence has to be found; mind that function is case sensitive

scanf(" %c", &ch);

count = getCount(inputSt,ch);// calling the funtion

printf("The total number of occurrence of %c in the input sentence is = %d", ch, count);

return 0; } //function definition int getCount(char *st, char c) { int i,n=0; for(i = 0; st[i] != '\0'; i++) { if(c == st[i]) n=n+1; } if (n>0) return n; else return -1; }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!