I need to make the program:
Write a program to ask for a string and a single character. Read in the string using getchar(). Read in the character using scanf(). Count and report the number of occurrences of the character in the string. Add a brief comment at the beginning of the file describing what the program does.
An interactive session with the program should look like (note the space at the end of the prompts, that the string appears on a separate line, indented and quoted, and that the character is printed within single quotes; sample user input is in red):
Enter any string: I could have entered anything, but this is what I typed.
Enter any character: t
In the following string:
"I could have entered anything, but this is what I typed."
The character 't' appears 6 times
This is what I got so far, but I'm completely stuck now because I keep getting errors:
#include <stdio.h>
#include <string.h>
int main (int argc, char**argv)
{
int i;
int letterCount;
char LetterToMatch[1];
char string[220];
letterCount = 0;
for(i = 0; i < strlen(string); i++)
{
if (string[i] = getchar[LetterToMatch])
letterCount++;
if (string[i] == '\n')
{
break;
}
}
printf("Enter any string: ");
scanf (" %s", &string);
printf("Enter any character: ");
scanf (" %s", &LetterToMatch);
printf("In the following string:\n");
printf(" \"%s\"\n", string);
printf("The character '%c" appears %d time%s\n",
LetterToMatch, letterCount, letterCount == 1 ?"":"s");
return(0);
}
I'm not even positive I have the right syntax, but here are the errors I'm getting for this code:
15:subscripted value is neither array nor pointer
26: warning: char format, different type arg (arg 2)
28: warning: char format, different type arg (arg 2)
31: parse error before "appears"
31: stray '\' in program
31:55: warning: multi-line string literals are deprecated
32:54: warning: multi-line string literals are deprecated
32:54: missing terminating " character
31:55: possible start of unterminated string literal
Please keep in mind that I am a beginner so please try and explain in a way a beginner would understand. Thanks!