Reputation: 3
I am currently studying for an exam and have come across the following question in a past paper:
"Write a function that accepts two strings. Count the number of characters in each, and return a pointer to the long string. You may extend the provided function to complete the solution:"
main()
{
return 0;
}
This is what I have so far, I am pretty sure I am close but it does not print anything to the stdout, I have also had this problem with other questions so if anyone could point me in the right direction it would help me a lot!
#include <stdio.h>
#include <string.h>
int main(void)
{
printf("The longest is %s", longest("HELLO", "HELLOO"));
return 0;
}
int longest(char *string1, char *string2)
{
char longer;
if (strlen(string1)>strlen(string2)){
longer=*string1;
}
else {
longer=*string2;
}
return longer;
}
Upvotes: 0
Views: 599
Reputation: 4247
You're so close!
Your longest()
function intends to return a string, but it's actually returning a single character, and this is where the wheels all fall off.
Instead we redefine the longer
variable from char
to char *
, which is a pointer to a string. Now it's going to return the longer of the two strings.
#include <stdio.h>
#include <string.h>
char *longest(char *, char *);
int main(void)
{
printf("The longest is %s\n", longest("HELLO", "HELLOO"));
return 0;
}
char * longest(char *string1, char *string2)
{
char *longer;
if (strlen(string1) > strlen(string2)){
longer = string1;
}
else {
longer = string2;
}
return longer;
}
This version also adds a bit of housekeeping to declare the longest
function before it's used so the compiler knows the types involved.
Turns out you can simplify the function a bit:
char *longest(char *s1, char *s2)
{
return strlen(s1) > strlen(s2) ? s1 : s2;
}
This version, like yours, decides arbitrarily that if the strings are actually the same length, it gives the win to the first string - it's not clear if this matters to you or not.
EDIT @Vlad has pointed out that this solution does not properly deal with const char *
pointers, and of course he's correct. I intentionally left this out because I was not writing a generally-useful function and believe this beyond what the OP was asking about.
But Vlad's right, so I'm including his version here:
char * longest( const char *s1, const char *s2 )
{
return ( char * )( strlen( s1 ) < strlen( s2 ) ? s2 : s1 );
}
This is one of those wiggy areas in C where it's hard to do it really well: the function longest()
does not actually modify any of the pointed-to strings, so it's able to easily handle these const
strings, but when it returns one of them, it's removed the const-ness from the pointer, and this is really the best you can do: other library functions work this way.
It would be nice if C had function overloading (one version for const
, one for not), or some special unicorn keyboard that carried the const
-ness of the parameters to the return value, but - alas - it does not.
This is a more advanced topic than the OP probably cares about, but it's worth noting for the wider audience. Thanks Vlad.
Upvotes: 1
Reputation: 165
Solution:
#include <stdio.h>
#include <string.h>
const char * longest(const char *string1, const char *string2)
{
const char * longer;
if (strlen(string1)>strlen(string2)){
longer=string1;
}
else {
longer=string2;
}
return longer;
}
int main(void)
{
printf("The longest is %s", longest("HELLO", "HELLOO"));
return 0;
}
Problems in your code:
In C, you have to declare (not necessarily implemented, but the signatures need to be defined already) the function before referring to it in your code. See this link;
Your return type is int
rather than pointer to string
requested by the assignment. Therefore, both variable longer
and the return type of function longest
need to be of type char *
.
Good to have:
const
assures the caller of the function that no modifications to the strings would be made.Upvotes: 0
Reputation: 311048
In the assignment there is written
...and return a pointer to the long string.
However your function has return type int
and within the function there are assignments that do not make sense
char longer;
//…
longer=*string1;
//…
return longer;
And the function must be declared before its usage.
The function can look the following way
char * longest( const char *s1, const char *s2 )
{
return ( char * )( strlen( s1 ) < strlen( s2 ) ? s2 : s1 );
}
Here is a demonstrative program.
#include <stdio.h>
#include <string.h>
char * longest( const char *s1, const char *s2 )
{
return ( char * )( strlen( s1 ) < strlen( s2 ) ? s2 : s1 );
}
int main(void)
{
char *s1 = "HELLO";
char *s2 = "HELLOO";
puts( longest( s1, s2 ) );
return 0;
}
Its output is
HELLOO
Upvotes: 1
Reputation: 17668
You need to fix several issues with longest function:
int longest(char *string1, char *string2) // 1) why return int ?
{
char longer; // 2) why char? it should be pointer
if (strlen(string1)>strlen(string2)){
longer=*string1; // 3) this is just one char, rather than a string
}
else {
longer=*string2;
}
return longer;
}
Here's a sample that fixes these issues:
const char* longest(const char *string1, const char *string2)
{
const char *longer;
if (strlen(string1)>strlen(string2)){
longer=string1;
}
else {
longer=string2;
}
return longer;
}
Upvotes: 0