Hi.
This is NOT a homework assignment (self-interest only).
The exercise asks me to write a function which accepts two strings and returns a pointer to the longer of the two.
#include <stdio.h>
#include <stdlib.h>
char* longer( char str1[], char str2[] );
char* str_ptr;
int main( void ) {
char str1[] = "I like cheese but I dislike brussel sprouts!";
char str2[] = "I like cheese and pasta!";
str_ptr = longer( str1, str2 );
printf( "The longer string is: %s\n", str_ptr );
return 0;
}
char* longer( char str1[], char str2[] ) {
int i = 0, count1 = 0, count2 = 0;
while( str1[i] != NULL ) {
i++;
count1++;
}
while( str2[i] != NULL ) {
i++;
count2++;
}
if( count1 < count2 )
return str2;
else if ( count1 > count2 )
return str1;
else
puts( "The strings are of equal length!" );
}
The compiler returns the following warnings:
ex106.c: In function `longer':
ex106.c:23: warning: comparison between pointer and integer
ex106.c:28: warning: comparison between pointer and integer
I'm unsure of how exactly to resolve the problems but am I correct in assuming it is a result of using an index on the str1 and str2 arrays?
Thanks,
java_girl