2

I have one 'simple' (I hope) question. I am actually coding some little program and I need to compare two strings, same length, but different letters like

Eagle

and

Hdjoh

I want to compare the first letter of the first string with the first letter of the second string, the second letter of the first string with the second letter of the second string etc..

I started to do like this:

for(i=0, i<N, i++){
        for(j=0, j<N, j++){
            if(string1[i]==string1[j] etc.. etc..
        }
    }

I see clearly that it doesn't compare first letter with first letter, second with second etc..

So maybe anyone have an idea how can I do this? (Without using any functions of string.h, i want to do this ''on my own'').

Maybe its a stupid question but im still a novice in C so...

Ah and the last thing, I define the two strings with 5 characters in my example, but it could be more than 5 vs 5..

Thanks by advance for the ideas.


Edit 1 :

#include <stdio.h>
#define N 20

int main()
{
    unsigned char string1[N], string2[N];
    int Answer=0, i=0;
    scanf("%s", string1);
    scanf("%s", string2);

    for(i=0; i<N; i++){
        if(string1[i]==string2[i]){
            Answer=1;
        }
        else{
            Answer=0;
        }
    }

    printf("Answer = %d", Answer);

    return 0;
}
user3605367
  • 101
  • 1
  • 2
  • 8

5 Answers5

11

Why are you using a nested for loop for this? If both strings are of size n do this:

for(int i=0;i<n;i++){
    if(string1[i]==string2[i]){
      //do something
    else if(// lesser than condition)
      //do something else
    else if(//greater than condition)
      //do something else other than the previous something
}

Here you when i=0, you are comparing string1[0] with string2[0], when i=1, you compare string1[1] with string2[1] and so on.....

Kartik_Koro
  • 1,277
  • 1
  • 11
  • 23
  • @user3605367 I would recommend you learn the use of the debugger in whatever IDE you use, it will be very helpful to trace what is happening in your program and you will easily be able to fix such errors by observing what is happening to the variables. Feel free to mark as correct answer! – Kartik_Koro Jun 05 '14 at 14:52
  • @user3605367 the green check is for marking correct answer – Kartik_Koro Jun 05 '14 at 15:08
  • Oupss. Yes sorry :) Done now – user3605367 Jun 05 '14 at 15:38
  • I made an edit on my first post (edit 1), with a new code, but this code always gaves me Answer = 1. Do you know why ? Thx. @Kartik_Koro – user3605367 Jun 05 '14 at 16:20
  • @user3605367 1 thing, this code will work only if BOTH strings are of length N. For example if you test with Eagle and Hdjoh after comparing uptil String1[4], String2[4] it goes on till String1[20] and String2[20], which may contain some garbage value and give you wrong results. What you would do is have something like int len1=strlen(String1) , len2=strlen(String2) after taking in the strings, then if(len1==len2) then for(i=0;i – Kartik_Koro Jun 06 '14 at 06:00
4

Your approach with nested loops isn't very well thought-out.

Clearly it will compare all letters of the second string against the first letter of the first string, then do the same for the second letter of the first string, and so on. Not at all the desired behavior.

Re-implementing strcmp() isn't very hard, here's a shot:

int my_strcmp(const char *a, const char *b)
{
  for(; *a && *b && *a == *b; ++a, ++b)
    ;
  if(*a < *b)
    return -1;
  return *a > *b;
}

Note that it returns zero when the strings are equal. A good way to write a test is:

if(my_strmcp(a, b) == 0)
{
  printf("two equal strings: '%s' and '%s'\n", a, b);
}

Some people write it as if(!my_strcmp()) but I don't recommend that, since it's mashing up so many concepts.

unwind
  • 391,730
  • 64
  • 469
  • 606
2

You want to use the same index for both strings to compare:

unsigned len = strlen(s1);
assert(len == strlen(s2) && "Strings not the same length");

for (unsigned i = 0; i < len; i += 1)
{
    if (s1[i] != s2[i])
        return false; /* strings are not equal */
}
return true; /* strings are equal */

Make sure that the strings have the same encoding, either ASCII or UTF8 or whatever. Comparing strings of different encoding will cause trouble :)

Jens
  • 8,423
  • 9
  • 58
  • 78
1

This code compares character by character. Note that this is not suitable for crypto code as it is vulnerable to a timing attack

for(i=0; i<N; i++){
    if(string1[i]==string2[i]){
         equal = 1;
    }else{
        equal = 0;
        break;
    }
}

Notes:
I am assuming same length (as stated in question)
I am also assuming strings are non-zero length

Both of these assumptions may not be true in other code.

Avery
  • 2,270
  • 4
  • 33
  • 35
1

Simple compare each element until the end of string is found or a difference.

size_t i = 0;
while (string1[i] != '\0' && string1[i] == string2[j]) i++;
int StringTheSame = string1[i] == string2[j];

This ignores N, but stops when either end-of-string ('\0') is encountered.


[Edit] @Kartik_Koro suggested a concern about a timing attack. Following is a constant time solution

int diff_bits = 0;
for(size_t i=0; i<N; i++) {
  diff_bits |= string1[i] ^ string2[i];
}
int equal = diff_bits == 0;

The above has a problem if either string's length is shorted than N-1, but per OP's requirements, that should not happen.

chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256