How do I know how many changes do I need to make in order to make a string be a permutation of an other string in c

Viewed 20

I was asked to make a program in c that will detect how many chars you need to remove, add, replace (replacing is a priority) in order to make a a case and space insensitive string of character of another string of characters. This is what I came up with , but I cannot figure out how to make the replacing function work.


#include <stdio.h>
#include <string.h>
#define LENGTH 100

int main()
{
    char str1[LENGTH];
    char str2[LENGTH];
    int str3[128]={0};
    int str4[128]={0};
    char c1=0,c2=0;
    int strlen1=0,strlen2=0,strlenMAX=0;
    int missingChars=0,additionalChars=0,replacableChars=0;
    int i,j,k;


    printf("Please enter the scrambled sentence:\n");

    for(strlen1=0;strlen1<LENGTH-1 && (c1=getchar()) != EOF && c1 != '\n';strlen1++){
        if(c1>=65 && c1<=90)
            str1[strlen1]=c1+32;
        else if(c1==32)
            strlen1--;
        else
            str1[strlen1]=c1;
    }
    /*inputing the string into str1*/

    printf("Please enter the ordered sentence:\n");

    for(strlen2=0;strlen2<LENGTH-1 && (c2=getchar()) != EOF && c2 != '\n';strlen2++){
        if(c2>=65 && c2<=90)
            str2[strlen2]=c2+32;
        else if(c2==32)
            strlen2--;
        else
            str2[strlen2]=c2;


    }
    /*inputing the string into str2*/



    if(strlen1>strlen2){
        missingChars=strlen1-strlen2;
        strlenMAX=strlen1;
    }

    else if(strlen2>strlen1){
        additionalChars=strlen2-strlen1;
        strlenMAX=strlen2;
    }
    else
        strlenMAX=strlen1;


    for(i=0;i<strlen1;i++)
    {
        str3[str1[i]]++;
    }
    for(j=0;j<strlen2;j++)
    {
         str4[str2[j]]++;
    }

    for(i=0;i<128;i++){
        printf("%d %d %d\n",i,str3[i],str4[i]);
    }


    for(k=0;k<128;k++)
    {
        if(str3[k]!=str4[k])
            replacableChars++;
    }

    if(missingChars==0&&additionalChars==0&&replacableChars==0)
       printf("str2 IS a case and space insensitive permutation of str1");
    else if(missingChars>0&&additionalChars==0&&replacableChars==0)
       printf("str2 is NOT a case and space insensitive permutation of str1. Necessary changes in str2 : add %d char(s)",missingChars);
    else if(missingChars==0&&additionalChars>0&&replacableChars==0)
        printf("str2 is NOT a case and space insensitive permutation of str1. Necessary changes in str2 : remove %d char(s)",additionalChars);
    else if(missingChars==0&&additionalChars==0&&replacableChars>0)
        printf("str2 is NOT a case and space insensitive permutation of str1. Necessary changes in str2 : replace %d char(s)",replacableChars);
    else if(missingChars==0&&additionalChars>0&&replacableChars>0)
        printf("str2 is NOT a case and space insensitive permutation of str1. Necessary changes in str2 : replace %d char(s)\n remove %d char(s)",replacableChars,additionalChars);
    else if(missingChars>0&&additionalChars==0&&replacableChars>0)
        printf("str2 is NOT a case and space insensitive permutation of str1. Necessary changes in str2 : replace %d char(s)\n add %d char(s)",replacableChars,missingChars);






    return 0;
}
0 Answers
Related