-3

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

Sami Kuhmonen
  • 30,146
  • 9
  • 61
  • 74
E. G.
  • 1
  • 1
    This code shouldn’t even compile. You’re assigning a char to a char pointer, giving a function char when it wants a char pointer etc. Which compiler are you using? Does it really not complain about these things? Also always tag the language you’re using. – Sami Kuhmonen Apr 19 '19 at 04:20
  • I was using C on Microsoft Visual Studio 2017. My professor said the code should be able to run, but it's not for some reason. On a sidenote, I'm new to StackOverflow. – E. G. Apr 19 '19 at 04:37
  • And the compiler really doesn’t give any warnings? Turn them to the maximum and fix everything it warns about – Sami Kuhmonen Apr 19 '19 at 04:39
  • See: [Why gets() is so dangerous it should never be used!](https://stackoverflow.com/questions/1694036/why-is-the-gets-function-dangerous-why-should-it-not-be-used). ***Validate*** every input by checking the return Avoid `getch()` and `conio.h` which makes your code 100% non-portable to anything other than ancient DOS/windows. Instead, just use `stdio.h` and `getchar()` to hold the terminal window upon at the end of your code (same result). – David C. Rankin Apr 19 '19 at 05:27

2 Answers2

-2

These are following issues that I found in your program.

  1. You are not saving or even printing the result return by your charcnt function.
  2. You don't event need to assign anything. Your if condition can be if(ptr[i] == c)
  3. You are not even printing the result anywhere in your code.
Vaibhav
  • 198
  • 1
  • 7
-2

Check this out!

int main() {
char string[100], rep_char = 'c';
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("%c", & rep_char);

printf("\n%d", charcnt(string, rep_char));

getch();

return 0;
}

int charcnt(char * ptr, char c) {
int rep = 0;

for (int i = 0; i < strlen(ptr); i++) {

    if (ptr[i] == c) {
        rep++;
    }
}
return rep++;
}
zainul ogna
  • 160
  • 2
  • 4