strokemypol
strokemypol

Reputation: 11

Compare command line arguments in C without string library

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.

Upvotes: 1

Views: 161

Answers (1)

Bybit360
Bybit360

Reputation: 166

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.

Upvotes: 2

Related Questions