1

I am trying to compare strings received from the command line arguments without using the string library. I have created a function to compare char*'s and it works for strings I create in my program, but not on strings passed in the command line. Here is the code for reference.

#include <stdio.h>

int comparestr(char* a, char* b) {
    
    printf("string 1 = %s, string 2 = %s\n", a, b);
    
    int len_a =0, len_b=0;
    while(*a) {
        len_a++;
        a++;    
    }
    while(*b) {
        len_b++;
        b++;    
    }
    printf("len a = %d, len b = %d\n", len_a, len_b);
    if (len_a == len_b) {
        for(int i=0; i<len_a; i++) {
            if(*(a+i) != *(b+i)) {
                return 0;
            }
        }
        return 1;   
    }
    else
        return 0;
}

int main(int argc, char**argv) {
    
    char* str1 = "hello";
    char* str2 = "hello";
    
    if(comparestr(str1,str2))
        printf("Same string.\n");
    else
        printf("Different strings.\n");
    
    if(comparestr(*(argv+1),"hell"))
        printf("Same cl string.\n");
    else
        printf("Different cl strings.\n");
    
    return 0;
}

Here is an example of the output.

./a.out hell hell
string 1 = hello, string 2 = hello
len a = 5, len b = 5
Same string.
string 1 = hell, string 2 = hell
len a = 4, len b = 4
Different cl strings.
7
  • 2
    One obvious problem in comparestr is that it irreversibly modifies its arguments when computing their lengths, and it consequently compares garbage later. Just use standard functions from string.h instead of re-inventing the wheel badly. Commented Feb 8, 2021 at 3:40
  • I'm trying to only use pointers and no libraries so I'm afraid I will have to reinvent the wheel badly. I realize now that the function is modifying the contents though so I will make some changes. Commented Feb 8, 2021 at 3:44
  • 1
    I tried subtracting a and b pointers by len_a and len_b respectively after calculating the length so it could go back to its original starting point and it worked. When you calculate the length, you actually bring a and b pointers to the NULL terminators, so the comparison is all undefined behavior from then on. Commented Feb 8, 2021 at 3:45
  • 1
    You should also break the habit of writing things like *(a+i). Instead, write a[i], which is equivalent and much clearer. Commented Feb 8, 2021 at 3:54
  • 1
    Why do you feel that it's necessary to compute the lengths before doing the comparisons? C strings are null-terminated, as you know, so it's easy enough to write a loop which stops at the end; your length counter does just that. (It should be a function rather than duplicating the code, but that's a side issue.) The way you've written it could be extremely inefficient. Suppose a is the string "hello" and b has a billion characters, starting with hello. It's only necessary to look at 12 characters to render a judgement. But your code examines 1,000,000,007 characters. Commented Feb 8, 2021 at 5:24

1 Answer 1

2

The issue with your code is that you change the pointer by incrementing it when you try to get the length of the strings. Once you change the pointers, the pointers will not point to the base of your strings.

Instead of changing your pointer, make a copy of it, and use the copy instead to calculate the length of the string, that way you don't lose the base address of your strings.

New code with changes.

//Copy of the pointer, to change this instead and keep the original pointer address
char* ptr_a = a;
char* ptr_b = b;
int len_a =0, len_b=0;
while(*ptr_a) {
    len_a++;
    ptr_a++;    
}
while(*ptr_b) {
    len_b++;
    ptr_b++;    
}

The whole code.

#include <stdio.h>

int comparestr(char* a, char* b) {
    
    printf("string 1 = %s, string 2 = %s\n", a, b);
    
    //Copy of the pointer, to change this instead and keep the original pointer address
    char* ptr_a = a;
    char* ptr_b = b;
    int len_a =0, len_b=0;
    while(*ptr_a) {
        len_a++;
        ptr_a++;    
    }
    while(*ptr_b) {
        len_b++;
        ptr_b++;    
    }
    printf("len a = %d, len b = %d\n", len_a, len_b);
    if (len_a == len_b) {
        for(int i=0; i<len_a; i++) {
            if(*(a+i) != *(b+i)) {
                return 0;
            }
        }
        return 1;   
    }
    else
        return 0;
}


int main(int argc, char**argv) {
    
    char* str1 = "hello";
    char* str2 = "hello";
    
    if(comparestr(str1,str2))
        printf("Same string.\n");
    else
        printf("Different strings.\n");
    
    if(comparestr(*(argv+1),"hell"))
        printf("Same cl string.\n");
    else
        printf("Different cl strings.\n");
    
    return 0;
}

New Output

Command-line arguments: hell hell

string 1 = hello, string 2 = hello
len a = 5, len b = 5
Same string.
string 1 = hell, string 2 = hell
len a = 4, len b = 4
Same cl string.
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.