0

This is a function that compares two strings to check if they are equal , everything works fine till the point they aren't. I want to calculate difference in value between the first two non-matching characters. Function returns a strange number. The idea is to create a pointer that saves first appeance of not matching character and another one which does the same in the next iteration , then return substraction of them. Basing on what I found on this site and google research I was able to build this so far.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>


int str_eq(const char *s1,const char *s2)
{
int i = 0;
int d = 0;
char *c ,*cp;
while(*s1 != 0 && *s1 != 0)
{
    if(*s1 != *s2)
    {
        *c = s1[i];
        *cp = s2[i + d];
        d++;
        return ((int)(*cp) - (*c));
    }
    s1++;
    s2++;
    i++;
}
return (0);

}


main()
{
const char *s1 = "awerrf";
const char *s2 = "awedre";

if(!(str_eq(s1 , s2)))
{
    printf("strings are equal");

}
else
{
    printf("strings aren't equal %d" ,str_eq(s1 , s2));

}


}

@edit I've done small update to the function code so the c points to array index where the first non-matching character is spotted , but now how do I go around creating another one that does same with next non-matching character.

int str_eq(const char *s1,const char *s2)
{
int i = 0;

char *c ,*cp;
while(*s1 != 0 && *s2 != 0)
{
    if(*s1 != *s2)
    {
        c = &s1[i];
        return (*c);

    }

    s1++;
    s2++;
    i++;
}
return (0);

}

expected result (test case)

    const char *s1 = "dogydf";
    const char *s2 = "dosydg";


    s1[0] = s2[0]
    s1[1] = s2[1]
    s1[2] != s2[2] pointer c = &s1[2];
    s1[3] = s2[3]
    s1[4] = s2[4]
    s1[5] != s2[5] pointer cp = %s1[5] , break loop;

    return (*cp) - (*c) //(5 - 2)

@edit_2 The solution (c) RoadRunner.

#include <stdio.h>
#include <stdlib.h>

#define SIZE 2

int str_eq(char *s1, char *s2) {
    int indexes[SIZE];
    int count = 0, i = 0, j = 0;

    while (s1[i] != '\0' && s2[j] != '\0' && count < 2) {
        if (s1[i] != s2[j]) {
            indexes[count++] = i;
        }
        i++;
        j++;
    }

    return indexes[1] - indexes[0];
} 

int main(void) {
    char *s1 = "doedz";
    char *s2 = "dogdyy";

    printf("Differences = %d\n", str_eq(s1, s2));

    return 0;
}
NiKZAA
  • 11
  • 4
  • 1
    SO isn't a debugging service. Compile with symbols, run the code inside a debugger to trace through the program line by line inspecting the values of the relevant variables to learn what is *really* going on. If then a *specific* question arises feel free to come back here. – alk Feb 25 '17 at 08:16
  • When you do `*c = s1[i];`, where does `c` point? Since it's a pointer it needs to actually point somewhere for it to be valid to dereference it. Perhaps you should *not* be using pointers for `c` and `cp`? – Some programmer dude Feb 25 '17 at 08:17
  • Why not just `for (; *s1 && *s2 && *s1 == *s2; s1++, s2++) {} return *s1 - *s2;`? (after of course preliminary checks that both `s1` and `s2` are valid pointers) – David C. Rankin Feb 25 '17 at 08:23
  • Please turn on compiler warnings and fix them! There's no point in asking help with code, where you use uninitialized variables, which is already pointed out by your compiler! – hyde Feb 25 '17 at 08:31
  • ...or then you question should be something like "compiler gives me this warning, I don't understand it, why do I get it and how do I fix it?"... – hyde Feb 25 '17 at 08:33
  • Also, should this return 0 for strings of different length? – hyde Feb 25 '17 at 08:37
  • @NIKZAA, what do you mean by the difference in value between first two non matching ? In your example you gave: 1. awerrf 2. awedre So they have two differences: `xxxrxf xxxdxe` Now what ? `(r-d) + (f-e)` ? – E235 Feb 25 '17 at 08:43
  • What i mean by difference in value is the difference of the array indexes , if you substract two pointers you end up with a value that indicates distance from each other. @hyde compiler doesn't give me any error , it compiles fine just the value returned if strings aren't equal is strange. – NiKZAA Feb 25 '17 at 09:19

2 Answers2

1
int str_eq(const char *s1,const char *s2){
    do{
        if(*s1 != *s2){
            return ((int)(*s1) - (*s2));
        }
    }while(*s1++ != 0 && *s2++ != 0);
    return (0);
}
Vineet1982
  • 7,730
  • 4
  • 32
  • 67
0

Try this:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int str_eq(const char *s1, const char *s2, int *diff) {
    if (strlen(s1) != strlen(s2)) {
        return 0;
    }

    if (*s1 == '\0' || *s2 == '\0') {
        return 0;
    }

    while (*s1 != '\0' && *s2 != '\0') {
        if (*s1 != *s2) {
            *diff = *s1 - *s2;
            return 0;
        }
        s1++;
        s2++;
    }

    return 1;
}

int main(void) {
    const char *s1 = "aaa";
    const char *s2 = "aab";
    int diff = 0, result;

    result = str_eq(s1, s2, &diff);

    if (result) {
        printf("Strings are equal\n");
    } else if (!result && diff) {
        printf("strings aren't equal %d\n" , diff);
    } else {
        printf("Invalid string inputs\n");
    }

    return 0;
}

UPDATE:

Difference between indexes:

#include <stdio.h>
#include <stdlib.h>

#define SIZE 2

int str_eq(char *s1, char *s2) {
    int indexes[SIZE];
    int count = 0, i = 0, j = 0;

    while (s1[i] != '\0' && s2[j] != '\0' && count < 2) {
        if (s1[i] != s2[j]) {
            indexes[count++] = i;
        }
        i++;
        j++;
    }

    return indexes[1] - indexes[0];
} 

int main(void) {
    char *s1 = "doedz";
    char *s2 = "dogdyy";

    printf("Difference = %d\n", str_eq(s1, s2));

    return 0;
}
RoadRunner
  • 25,803
  • 6
  • 42
  • 75