Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C program Make sure it runs please and well documented It is a good practise to

ID: 3890960 • Letter: C

Question

C program Make sure it runs please and well documented

It is a good practise to use function for different logic implementation. So from this lab, try to implement different coding logic within different functions. It is not recommendec to implement all of your logic within the main function. In case you don't use function (in this assignment), your grading would be based on the 90 percent of the total marks. Problem 1 (10 points) Take two input string say string1 and string 2. Write down a C function CompareString(char string1, char * string2), which compares whether each of the string contains the same number of characters within them. Example: Output Yes Yes No No Input string 1 ="wqwwqqeewg" string2-"qeewqwwqwq" string1="abodeccda" string2="adcdeccea" string1 ="abcdeccda" string2="adcdecc" In the first example in string1 number of a,b, and c are 2, 6 and 3 respectively. In string2 number of a,b and c are 2, 6 and 3 too. That is why the output is Yes. In the last example in string1 number of a,b, c and d are 2, 1, 3 and 2 respectively. In string2 number of a, b, c and d are 1, 0, 3 and 2. That is why the output is No.

Explanation / Answer

# include <stdio.h>

# include <stdbool.h>

bool CompareString1(char *string1, char *string2)

{

// Create 2 count array each of size 256 and all the elements are initialize to 0

int count1[255] = {0};

int count2[255] = {0};

int i;

// Increase the count in the corresponding count array, for all elements one by one present in the string

//string1[i] && string2[i] condition will run the loop until end of the strings

for (i = 0; string1[i] && string2[i]; i++)

{

count1[string1[i]]++;

count2[string2[i]]++;

}

//To check if bothe the string are of same length. If not then false

if (string1[i] || string2[i])

return false;

// Compare each elements of count arrays and return false if any of the comparison fails

for (i = 0; i < 255; i++)

if (count1[i] != count2[i])

return false;

return true;

}

//It also uses the same logic as above but integer counter array has been taken

int CompareString2(char *string1, char *string2)

{

int c1[26] = {0}, c2[26] = {0}, c = 0;

while (string1[c] != '')

{

c1[string1[c]-'a']++;

c++;

}

c = 0;

while (string2[c] != '')

{

c2[string2[c]-'a']++;

c++;

}

for (c = 0; c < 26; c++)

{

if (c1[c] != c2[c])

return 0;

}

return 1;

}

int main()

{

char string1[] = "bbbcccbaaba";

char string2[] = "abbbcbccbab";

if ( CompareString2(string1, string2) )

printf("Both the string contain same number of character with in them");

else

printf("Both the string does not contain same number of character with in them");

return 0;

}