strocc function

Eng. theFOX 0 Tallied Votes 167 Views Share

count the number of occurrences of a sub-string in another string.

a good example for learning how to make your own functions like strcpy, strlen and strcmp

/*
    This program will calc the number of occurrences of a substring
    in another string.
*/

#include <stdio.h>
#include <conio.h>
#include <malloc.h>

int  stroc  ( const char*, const char* );
int  strcmp1 ( const char*, const char* );
int  strlen1 ( const char* );
void strcopy ( char*, const char*, int );

int main()
{
    // "we whe wae waew we wwe w e wewe" ;
    // "we";

    char str1[255];
    char str2[255];

    printf("This program will count the number of occurrences of str2 in str1\n\n");
    
    printf("str1 = ");
    gets(str1);
    
    fflush(stdin);

    printf("str2 = ");
    gets(str2);

    printf("%d", stroc(str1, str2));

    printf("\n\nPress any key to exit...\n");
    getch();

    return 0;
}


// -----------------------------------------------------------------
// count the number of occurrences of a substring in another string
// -----------------------------------------------------------------
int stroc(const char* str1,const char* str2)
{
    int     count=0;                    // this is our counter

    char*   temp;                       // a buffer to hold a part of the 
                                        // original string with the same length 
                                        // of the substring that we search for.

    int     len = strlen1(str2);        // hold the length of our substring.

    temp = (char*) malloc(len + 1);     // reserve a size for temp that equals 
                                        // to the length of our substring
                                        // +1 is for the NULL char

    for(; *str1 ; str1++)               // loop while our pointer doesn't point at 
                                        // the NULL char yet, move the pointer to 
                                        // the next offset
    {
        strcopy(temp, str1, len);       // copy a part of the original string to
                                        // the temp with the length of our 
                                        // substring

        if (strcmp1(temp, str2) == 0)   // compare our 2 strings, the temp and
                                        // the substring, if the comparrison 
                                        // returned 0 then we have an occurrence, 
                                        // so inc counter by 1
            count++;
    }

    return count;
}


// -----------------------------------------------------------------
// compare two strings
// return 0         : they are indetical
// retunr non-zero  : they are not identical
// -----------------------------------------------------------------
int strcmp1(const char* str1, const char* str2)
{
    for(; *str1 == *str2; str1++, str2++)
        if (*str1 == 0)
            return 0;

    return *str1 - *str2;
}


// -----------------------------------------------------------------
// count the length of a string
// -----------------------------------------------------------------
int strlen1(const char* str)
{
    int len=0;
    while( *str++ )
        len++;

    return len;
}


// -----------------------------------------------------------------
// copy a specified number of characters from a string to another 
//
// IMPORTANT:   You must reserve an appropriated size for the
//              destination string. in other words, use malloc to 
//              allocate a size for the destination string before 
//              passing it to the function.
// -----------------------------------------------------------------
void strcopy(char* str1, const char* str2, int len)
{
    while(len--)            // loop (len) times
        *str1++ = *str2++;

    *str1++ = '\0';         // add the NULL char to the end of the 
                            // end of the destination string
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.