-5

I have a problem writing the code to this problem and i can't seem to wrap my head around on how to do it. It's for my university assignment and I've tried a program that gives the same output but still says i got the wrong answer.

For example,

Input:

aabbaabbaacc
aa

Output:

3

here is my code:

#include <stdio.h>
#include <string.h>

int main()
{
    char str[] = "aabbaaccaadd";
    char substr[] = "aa";

    char* ptr = strstr(str, substr);

    int count = 0;

    while (ptr != NULL) {
        count++;
        ptr = strstr(ptr + 1, substr);
        {

            printf("%d", count);

            return 0;
        }
    }
}
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • If the question is marked C++, why the code is C? – Moia Aug 24 '21 at 08:34
  • 2
    This code doesn't compile. Only post code you actually tested. And you haven't said what its output is. – interjay Aug 24 '21 at 08:34
  • @Rreaper Music There is a typo ptr = strstr(ptr + 1, substr); { .Must be ptr = strstr(ptr + 1, substr); } That is you need to use the closing brace. – Vlad from Moscow Aug 24 '21 at 08:38
  • Are you sure your university/course policy allows you to ask for help here? – Yun Aug 24 '21 at 08:40
  • Now that the code is properly indented is your typo more obvious? – Alan Birtles Aug 24 '21 at 08:50
  • @AlanBirtles Your edit completely changed the code. The original code simply had a `{` instead of a `}`, while your edit added an extra scope and put the `printf` inside the loop. Please don't change anything other than formatting when editing someone else's code. – interjay Aug 24 '21 at 09:39
  • @interjay I just added the missing trailing braces and formatted it, I didn't change anything – Alan Birtles Aug 24 '21 at 09:44
  • @AlanBirtles My point is that you don't know there were missing braces. It's just as likely (if not more) that OP accidentally switched one `}` to `{` when posting. – interjay Aug 24 '21 at 09:46

1 Answers1

1

You need to move the call of printf and the return statement from the while loop

while (ptr != NULL) {
    count++;
    ptr = strstr(ptr + 1, substr);
}

printf("%d\n", count);

return 0;

You could write a separate function. For example

#include <stdio.h>
#include <string.h>

size_t count_substring( const char *s1, const char *s2 )
{
    size_t n = 0;
    
    for ( const char *p = strstr( s1, s2 ); p; p = strstr( p + 1, s2 ) )
    {
        ++n;
    }

    return n;   
}

int main(void) 
{
    char str[] = "aabbaaccaadd";
    char substr[] = "aa";

    printf( "%zu\n", count_substring( str, substr ) );

    return 0;
}

An alternative approach is when the substring is counted when it is not overlapped with itself.

size_t count_substring( const char *s1, const char *s2 )
{
    size_t len = strlen( s2 );
    
    size_t n = 0;
    
    for ( const char *p = strstr( s1, s2 ); p; p = strstr( p + len, s2 ) )
    {
        ++n;
    }

    return n;   
} 
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • Your answer is about an edit of the code made by someone other than OP which completely changed its meaning. The original code simply had a `{` instead of `}`, and fixing that seems to make it work correctly. – interjay Aug 24 '21 at 09:38