Search code examples
cc-stringsstrcmpsuffixfunction-definition

comparing the ending of the strings


I am writing a program to compare different strings. Specifically chemical elements that end with an OH. I have to return -1 if the string ends with OH. However, my program doesn't work. Where am I wrong?

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

int hydroxide(char *string);

int main() {
    char *string;
    printf("Enter String:");
    gets(string);
    printf("%d", hydroxide(string));
}

int hydroxide(char *string) {
    string = strrchr(string, 'O');
    if (string != NULL)
        return (strcmp(string, "OH"));
    return (-1);
 }

Solution

  • For starters the logic of the function is wrong.

    Usually such a function should return 1 (or a positive value) that corresponds to the logical true or 0 that corresponds to the logical false when it answers to a question like "yes or no".

    This call

    strcmp(string, "OH")
    

    returns 0 if two strings are equal. Otherwise the function can return any positive or negative value depending on whether the first string is greater or less than the second string.

    Apart from this the function parameter should have the qualifier const because the passed string is not changed within the function.

    You did not reserve a memory where you are going to read a string. The declared pointer

    char *string;
    

    is uninitialized and has an indeterminate value. Thus this call

    gets(string);
    

    invokes undefined behavior.

    Take into account that the function gets is an unsafe function and is not supported by the C Standard. Instead you should use the standard C function fgets.

    And it will be much better if the function will be more generic. That is when it can check any supplied suffix of a string. Always try to write more generic functions. In this case they can be reusable.

    Below there is a demonstrative program that shows how the function can be defined.

    #include <stdio.h>
    #include <string.h>
    
    int hydroxide( const char *s, const char *suffix )
    {
        size_t n1 = strlen( s );
        size_t n2 = strlen( suffix );
    
        return !( n1 < n2 ) && strcmp( s + n1 - n2, suffix )  == 0;
     }
    
    int main(void) 
    {
        enum { N = 100 };
        char s[N];
    
        while ( 1 )
        {
            printf( "Enter a String (empty string - exit): " );
    
            if ( fgets( s, N, stdin ) == NULL || s[0] == '\n' ) break;
    
            s[ strcspn( s, "\n" ) ] = '\0';
    
            printf( "%s\n", hydroxide( s, "OH" ) ? "true" : "false" );
        }
    
        return 0;
    }
    

    The program output might look like

    Enter a String (empty string - exit): brogrammerOH
    true
    Enter a String (empty string - exit):