I'm setting up a program where if I type in a string, I need to use a function with these 2 formal parameters (a pointer and a character). Every time I run through the program, the code won't run through my declared function with the actual parameters.
How this works is...
1) Input my string
2) Input the character I want to see is repeated
3) The function will run a for-loop to see which characters in my string (which is in an array) contain the repeated character; every time it does, it will increment and total the number of times it was repeated. Below is my function code...
int main(void)
{
char string[100], rep_char = 'c', *ptr = string[0];
int charcnt(char *ptr, char c);
printf("Input your string: ");
gets(string);
printf("%i", strlen(string));
printf("\nWhich character in the string are you checking for repetition? ");
scanf_s("%c", &rep_char);
charcnt(*ptr, rep_char);
getch();
return 0;
}
int charcnt(char *ptr, char c)
{
int rep = 0;
char string[100];
for (int i = 0; i < strlen(string); i++)
{
*ptr = string[i];
if (string[i] == c)
{
rep++;
}
}
return rep++;
}
I expect to run like so....
[Expected]:
Input your string: hello there.
Which character in the string are you checking for repetition? l
2
Instead I get...
[Actual]:
Input your string: hello there.
Which character in the string are you checking for repetition? l