1

I am new to coding and started to learn strings in C (you might find a lot of mistakes in my code but I am still trying to learn from my mistakes).

I am trying to write a code that takes 2 strings and check for intersection between the 2 strings and then returns the count of identical indexes; right now, the problem with this code is that it prints out extra space in output and does not calculate it right.

I believe it checks each index with the whole string looking for identical match, but I want it to only check indexes according to their places.

E.g., for input :

str1: Abc 1223 $#& cDE\0

str2: AB 12 34 #$ c\0

the output should be:

Found 6 Matches. (in this case they are index numbers(0,1,6,9,11,12))

Here's the Faulty code I came up with so far:

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#define MAX 80
int main()
{
    char str1[MAX];
    char str2[MAX];
    int i = 0, j = 0;
    int Counter = 0;
    printf("Enter Your First String : \n  ");
    gets(str1);
    printf("Enter Your Second String : \n ");
    gets(str2);
    while (str1[i] != '\0' && str2[i] != '\0')
    {
        
        if (str1[i] >= 65 && str1[i] <= 90)
        {
            str1[i] = str1[i] + 32;
        }

        if (str2[i] >= 65 && str2[i] <= 90)
        {
            str2[i] = str2[i] + 32;
        }

        if (str1[i] = str2[i])
        {
            Counter++;
        }
        i++;
    }
    printf("Similarities Between 2 Strings Count Is : %d \n", Counter);
    return 0;
}
Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
Steve
  • 13
  • 2
  • I'm not entirely sure how you think the answer for the given input strings should be 6. With the spaces you have included, there are only 3 places where the characters match. Did you want something else done to remove or discount spaces? – Adrian Mole Dec 17 '20 at 16:05
  • i wanted the 2 strings to be checked accordingly say both of them check index number 0 then number 1 and so on ... the guy downbelow solved my problem ! you can check it thanks for taking time to look and ask – Steve Dec 17 '20 at 16:40

1 Answers1

1

The main issue with your code is the if (str1[i] = str2[i]) line. This uses the assignment operator (=), thus giving str1[i] the value in str2[i]. What you need, instead, is the comparison operator (==):

        if (str1[i] = str2[i])
        {
            Counter++;
        }

Also, be aware that the gets function has long been declared unsafe and removed from the standard library. Use fgets instead, specifying the length of your input arrays and the stdin 'file' for input:

    printf("Enter Your First String : \n  ");
    fgets(str1, MAX, stdin);
    printf("Enter Your Second String : \n ");
    fgets(str2, MAX, stdin);

Another issue with your code (though not really an error) is the use of so-called "magic numbers" for your lowercase letter checks. It is better to use the actual characters (as literals) in such cases. Thus, instead of:

    if (str1[i] >= 65 && str1[i] <= 90)
    //...

using this is much clearer:

    if (str1[i] >= 'a' && str1[i] <= 'z')
    //...

But you should really consider using the toupper() and/or islower() functions, here.

Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
  • I Thought of using so but i can't because the range of stuff i can use (functions and libraries) is limited in this course im taking , i really appreciate this it helped me fix it even though now im experiencing some problem with it after i fixed it according to what you told me , it works and all good but i think the method i used to convert uppercase into lowercase isn't the best thing because this function is gathered with another that does rotate the strings and after rotation what used to be uppercase is outputted as lowercase , still working on fixing it , thanks a lot anyway !!! – Steve Dec 17 '20 at 16:45