back to embedded

C Compare String

 

BOOL compare_string(unsigned char *first, unsigned char *second){
    while(*first == *second){
        if(*first == '\0' || *second == '\0'){
            break;
        }
        first++;
        second++;
    }
    if(*first == '\0' && *second == '\0'){
        return TRUE;
    }
    return FALSE;
}

 

Compares two pointer strings and returns true or false if they're the same. Comparison is terminated when a null (\0) is detected. To ensure reliable operation, make sure to terminate your strings with a null character.

 

Usage:
unsigned char command[25] = "getstatus\0";
if(compare_string(command, "getstatus\0")){
    //do something } else{
    //not the same }