Write a function that takes two string as inputs and returns the number of occur
ID: 3828820 • Letter: W
Question
Write a function that takes two string as inputs and returns the number of occurrences of the permutations of the second string in the first string. The function should also be CASE INSENTIVE. The prototype for this function called countPermutation is given below: int countPermutation(char s1[], char s2[]); Ex: If I were to run the following snippet of code calling countPermuation, int main(int argc, char *argv[]) { char s1[] = “There was a sudden explosion in the value of the USD in the country Dsumadeupname.”; char s2[] = “USD”; int count; count = countPermutation (s1, s2); printf(“%d”, count); } the output of the program would produce: 3 (sud-den, USD, Dsu-madeupname)
Explanation / Answer
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <limits.h>
#include <stdbool.h>
int countPermutation(char* s1,char* s2)
{
int p1;
int count=0,temp,i,j,k;
int no_of_permutations=1;
scanf("%d", &p1);
for(i=1;i<p1+1;i++)
{
no_of_permutations *= i;
}
int a = no_of_permutations/((p1-1));
for (i=0; i < a; i++)
{
for(j=0; j<p1-1; j++)
{
char temp;
temp=s1[j];
s1[j]=s1[j+1];
s1[j+1] = temp;
char *s3 = s2;
while(s3 = (strstr(s3, s1)) )
{
count++;
printf(" %s -- %s -- %d",s3,s1,count);
s3++;
}
}
}
return count;
}
void main()
{
int x;
char* s1;
s1 = (char *)malloc(512000 * sizeof(char));
scanf(" %[^ ]",s1);
char* s2;
s2 = (char *)malloc(512000 * sizeof(char));
scanf(" %[^ ]",s2);
x = countPermutation(s1,s2);
printf(" %d", x);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.