Home > Blockchain >  Identifies the differences between pairs of strings
Identifies the differences between pairs of strings

Time:09-20

I'm going to identify the difference between two string. I can't see what I'm missing here. The output from my code is correct by the sample. But when I test run it with other test, it fails. I can't see what the other tests are.

The input:

The first line of input contains an integer 1<= n <= 500, indicating the number of test cases that follow. Each test case is a pair of lines of the same length, 1 to 50 characters. Each string contains only letters (a-z,A-Z) or digits (0-9).

The Output:

For each test case, output the two lines in the order they appear in the input. Output a third line indicating similarities and differences as described above. Finally, output a blank line after each case.

Sample:

enter image description here

enter image description here

int main()
{
     
    int n;
    
    // scan the integer for number of test cases
    if(scanf("%d", &n) != 1) {
        return 1;
    }

    //Loop through the test cases
    for (int i = 0; i < n; i  )
    {
        char string1[1024], string2[1024], output[50];

        //Scan first and second string
        if(scanf("%s", string1) != 1) {
            return 1;
        }
        
        if(scanf("%s", string2) != 1) {
            return 1;
        }

        //Loop through the strings and compare them
        for (int i = 0; string1[i] != '\0' || string2[i] != '\0'; i  )
        {
            //Convert to lowercase
            string1[i] = tolower(string1[i]);
            string2[i] = tolower(string2[i]);

            //Compare
            if (string1[i] == string2[i])
            {
                output[i] = '.';
            } else {
                output[i] = '*';
            }
            
        }

        //Print the strings and the output.
        printf("%s\n%s\n%s\n", string1, string2, output);

        if(i   1 < n) {
            printf("\n");
        }
        

    }
    
    
    return 0;
}

CodePudding user response:

Maybe the problem is that when you have an input string in upper case ("ABCD") you print it in lowercase in the output ("abcd")?

CodePudding user response:

The output string is never terminated, a '\0' should be added after the loop is over, otherwise printf would read over to the memory filled by previous test cases if their inputs were longer.

CodePudding user response:

There is no great sense to declare the variable n as having the signed integer type int. Declare it at least as having type unsigned int.

unsigned int n;

// scan the integer for number of test cases
if(scanf("%u", &n) != 1) {
    return 1;
}

The three character array should be declared as having 51 elements

char string1[51], string2[51], output[51];

The calls of scanf will look like

    //Scan first and second string
    if(scanf(" Ps", string1) != 1) {
        return 1;
    }
    
    if(scanf(" Ps", string2) != 1) {
        return 1;
    }

Also you need to check that the entered strings have the same length as for example

    if( strlen( string1) != strlen( string2 ) ) {
        return 1;
    }

This for loop

   for (int i = 0; string1[i] != '\0' || string2[i] != '\0'; i  )

can invoke undefined behavior if the lengths of strings are not equal each other. If you will include the above shown if statement then the for loop can look the following way

size_t i = 0;
for ( ; string1[i] != '\0'; i   )

These statements change the original strings

//Convert to lowercase
string1[i] = tolower(string1[i]);
string2[i] = tolower(string2[i]);

that you should not do. Just compare corresponding characters like

if (string1[i] == string2[i])
{
    output[i] = '.';
} else {
    output[i] = '*';
}

If you want to compare characters independent on their cases then write

if ( tolower( ( unsigned char )string1[i] ) == tolower( ( unsigned char )string2[i] ) )
{
    output[i] = '.';
} else {
    output[i] = '*';
}

After the for loop write

output[i] = '\0';

tp form a string in the array output.

It seems this if statement

if(i   1 < n) {
    printf("\n");
}

is redundant. Just output the new line character '\n'

putchar( '\n' );

after each test case.

  • Related