user3605367
user3605367

Reputation: 101

Compare two strings character by character in C

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;
}

Upvotes: 2

Views: 52272

Answers (5)

chux
chux

Reputation: 153338

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.

Upvotes: 1

Avery
Avery

Reputation: 2293

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.

Upvotes: 1

Jens
Jens

Reputation: 9130

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 :)

Upvotes: 2

unwind
unwind

Reputation: 399713

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.

Upvotes: 4

Kartik_Koro
Kartik_Koro

Reputation: 1287

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.....

Upvotes: 11

Related Questions