Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <string.h>
- #include <stdio.h>
- #include <stdlib.h>
- #define MAX 100
- int strcount(char* strA, char* strB);
- int doubleCheck(char* strA, char* strB);
- int main(void)
- {
- char*A = (char*)malloc(sizeof(char)*100);
- char*B = (char*)malloc(sizeof(char)*10);
- for(int i = 0; i < MAX; i++)
- {
- A[i] ='\0';
- }
- if(malloc == NULL)
- {
- printf("Error allocating memory");
- exit(1);
- }
- //A = "The friends of Ringo Ishikawa";
- //char*B = "Ringo Ishikawa";
- printf("Please enter your first string.");
- fgets(A, MAX, stdin);
- printf("Please enter your second string.");
- fgets(B, MAX, stdin);
- strcount(A, B);
- free(A);
- free(B);
- return 0;
- }
- int strcount(char* strA, char* strB)
- {
- int count = 0;
- char* ptr;
- char* loc = malloc(sizeof(char*)*100);
- char*A = ptr;
- char*B = strB;
- for(ptr = strA; *ptr!= '\0'; *(ptr++))
- {
- if((loc=strstr(ptr, strB)) != NULL)
- {
- if(*ptr == *strB)
- {
- if(doubleCheck(ptr, strB) == 1)
- count++;
- }
- }
- }
- printf("\nThe amount of times that %s appears in %s is %d", strB, strA, count);
- free(loc);
- }
- int doubleCheck(char* strA, char* strB)
- {
- int ccount = 0;
- printf("\nThe strings being compared are %s and %s", strA, strB);
- for(int i = 0; i < strlen(strB); i++)
- {
- if(strA[i] == strB[i]){
- ccount++;
- }else
- break;
- }
- if(ccount == strlen(strB))
- return 1;
- else
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement