Count the number of characters in two strings, and return a pointer to the longer string. - C String

C examples for String:char array

Description

Count the number of characters in two strings, and return a pointer to the longer string.

Demo Code

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

char * compare_strings( char * first, char * second){
    int x, y;//w  w w  . jav a2  s  . com

    x = strlen(first);
    y = strlen(second);

    if( x > y)
       return(first);
    else
       return(second);
}

int main( void )
{
    char *a = "test test";
    char *b = "this is a test";
    char *longer;

    longer = compare_strings(a, b);

    printf( "The longer string is: %s\n", longer );

    return 0;
}

Result


Related Tutorials