CT 03

 #include <stdio.h>

#include <string.h>


#define MAX 1000  // Enough to hold the result


int main() {

    char s1[MAX] = "I scream, you scream, we all scream for ice cream!";

    char s2[] = "scream";

    char s3[] = "shout";


    char result[MAX] = "";  // Resulting string

    char *pos = s1;         // Current position in s1

    char *match;            // Pointer to next occurrence

    int count = 0;


    while ((match = strstr(pos, s2)) != NULL) {

        // Copy part before match

        strncat(result, pos, match - pos);

        // Add replacement word

        strcat(result, s3);


        // Move past the match

        pos = match + strlen(s2);


        count++;

    }


    // Add the remaining part after the last match

    strcat(result, pos);


    printf("Resulting string: %s\n", result);

    printf("Number of replacements: %d\n", count);


    return 0;

}

    


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

int main() {
    char str1[100], str2[1000];
    int count = 0;

    printf("Enter first string: ");
    gets(str1);

    printf("Enter second string: ");
    gets(str2);

    char *ptr = str2;
    char *result;
    while ((ptr = strstr(ptr, str1)) != NULL) {
        count++;
        ptr++; // Move pointer ahead to find overlapping substrings too
    }

    if (count > 0) {
        printf("'%s' is present in '%s' %d times.\n", str1, str2, count);
    } else {
        printf("'%s' is NOT present in '%s'.\n", str1, str2);
    }

    return 0;
}


Comments

Popular posts from this blog

Phy 129 - WM, CT-03

Online 3 CSE 102; Array Resources.