is this correct

D

Darklight

Q6: Write a function that accepts two strings. Count the number of
characters in each, and return a pointer to the longer string.
and please comment

/* LEN_STRING.C PROGRAM TO RETURN LONGEST STRING */
#include<stdio.h>
#include<string.h>

void length(char [ ], char [ ]);

int main(void)
{

char a[100];
char b[100];

printf("Enter string1\n");
fgets(a,100,stdin);

printf("Enter string2\n");
fgets(b,100,stdin);

length(a,b);

return 0;
}

void length(char string1[ ], char string2[ ])
{
char *line;

if((strlen(string1)) > (strlen(string2)))
{
line = string1;
printf("\nstring 1 is the longest\n%s\n",line);
}
else if(strlen(string1) < strlen(string2))
{
line = string2;
printf("\nstring 2 is the longest\n%s\n",line);
}
else
printf("\nBoth strings are the same length\n");
}
 
M

Martin Dickopp

Darklight said:
Q6: Write a function that accepts two strings. Count the number of
characters in each, and return a pointer to the longer string.
and please comment

/* LEN_STRING.C PROGRAM TO RETURN LONGEST STRING */
#include<stdio.h>
#include<string.h>

void length(char [ ], char [ ]);

The question asks to return a pointer to the longer string, but your
`length' function returns nothing. Also, a better function name would be
something like `longer_string', since that's what the function is
supposed to return.
int main(void)
{

char a[100];
char b[100];

printf("Enter string1\n");
fgets(a,100,stdin);

printf("Enter string2\n");
fgets(b,100,stdin);

If the user enters 100 or more characters after the first prompt, these
additional characters are read by the second `fgets' call.
length(a,b);

return 0;
}

void length(char string1[ ], char string2[ ])
{
char *line;

if((strlen(string1)) > (strlen(string2)))
{
line = string1;
printf("\nstring 1 is the longest\n%s\n",line);
}
else if(strlen(string1) < strlen(string2))
{
line = string2;
printf("\nstring 2 is the longest\n%s\n",line);
}
else
printf("\nBoth strings are the same length\n");
}

It's probably a good idea not to do any output in the `length' function,
but just return a pointer to the longer string (as required in the
question). Do the output in `main' instead.

It is good that you thought about the case when both strings have the
same length, although the question doesn't specify what to do in this
case.

Calling `strlen' twice for each string could degrade the performance.
Use variables instead

const size_t len1 = strlen (string1);
const size_t len2 = strlen (string2);

and compare these.

The `line' variable is not really needed. In the block where you /know/
that `string1' is longer, you can just use `string1'. Likewise for
`string2'.

Finally, identifiers starting with `str' followed by a lowercase letter
are reserved for the implementation. Consider different names, e.g.
`str1' and `str2'.

Martin
 
M

Martin Dickopp

Darklight said:
Mail-Copies-To: (e-mail address removed)

You set the above header in your posting. This caused my newsreader to
send a mail copy of my reply to you, however it was returned due to an
invalid email address.

Don't set Mail-Copies-To to an invalid email address, please!

Martin
 
A

Al Bowers

Darklight said:
Q6: Write a function that accepts two strings. Count the number of
characters in each, and return a pointer to the longer string.
and please comment
void length(char [ ], char [ ]);

Looking at your requirements, one wonders what you want to
return should the lengths be equal.

Longing at your prototype, it does not meet the requirements
because it does not return anything. Also, since the strings
are not going to be modified, you can make the parameters
const char.

const char *length(const char *s1, const char *s2);

If it doesn't matter which string pointer is returned should
the lengths be equal, then the definition is simple.

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

const char *length(const char *string1, const char *string2)
{
return strlen(string1)>=strlen(string2)?string1:string2;
}

int main(void)
{
char *str1 = "Good Morning";
char *str2 = "Good Night";

printf("The longer of \"%s\" and \"%s\"\n"
"is \"%s\"\n",str1,str2,length(str1,str2));
return 0;
}
 
D

Darklight

Is this any better

/* LEN_STRING1.C PROGRAM TO RETURN LONGEST STRING */
#include<stdio.h>
#include<string.h>

char *length(char [ ], char [ ]);

int main(void)
{

char a[100];
char b[100];
char *c;

printf("Enter string1\n");
fgets(a,100,stdin);

printf("Enter string2\n");
fgets(b,100,stdin);

c = length(a,b);

printf("\nThe longest string is %s\n",c);
return 0;
}

char *length(char str1[ ], char str2[ ])
{
char *line;

if((strlen(str1)) > (strlen(str2)))
{
line = str1;
}
else
{
line = str2;
}

return line;
}
 
M

Malcolm

Darklight said:
/* LEN_STRING1.C PROGRAM TO RETURN LONGEST STRING */
#include<stdio.h>
#include<string.h>

char *length(char [ ], char [ ]);

int main(void)
{

char a[100];
char b[100];
char *c;

printf("Enter string1\n");
fgets(a,100,stdin);

printf("Enter string2\n");
fgets(b,100,stdin);
You still haven't fixed the fgets() problem. fgets() is actually quite a
difficult function to use.
c = length(a,b);

printf("\nThe longest string is %s\n",c);
return 0;
}

char *length(char str1[ ], char str2[ ])
It's much mode idiomatic to use the char *str1, char *str2 notation. The
pointers can also be const since they are not modified.
{
char *line;

if((strlen(str1)) > (strlen(str2)))
{
line = str1;
}
else
{
line = str2;
}

return line;
}
You do need to know what is supposed to happen if the strings are of equal
length. This is an oversight in your specification.
 
P

Papadopoulos Giannis

Darklight said:
Q6: Write a function that accepts two strings. Count the number of
characters in each, and return a pointer to the longer string.
and please comment

Is this a homework? Cause if it is, you got it wrong..

In a homework situation, the tutor is probably suggesting that YOU
create the strlen() function and not to use the ready one...
 
S

sugaray

// here's my solution to this homework assignment

#include <stdio.h>
#include <stdlib.h>

size_t Strlen(const char *str)
{
const char *p=str;
while(*p) ++p;
return p-str;
}

const char *LongestString(const char (*s1),const char (*s2)) {
size_t l1=Strlen(s1),l2=Strlen(s2);
return (l1>l2) ? s1 : (l1<l2) ? s2 : NULL;
}

int main(int argc,char **argv)
{
const char *longest;

if(argc!=3) {
fprintf(stderr,"Usage:\n\t%s <string1> <string2>\n",argv[0]);
exit(EXIT_FAILURE);
}

longest=LongestString(argv[1],argv[2]);
if(longest)
printf("The longest string is \"%s\"\n",longest);
else printf("Both strings are equal length.\n");

return 0;
}
 
D

Dan Pop

In said:
You set the above header in your posting. This caused my newsreader to
send a mail copy of my reply to you, however it was returned due to an
invalid email address.

Don't set Mail-Copies-To to an invalid email address, please!

Which RFC defines this header?

AFAIK, the right header for this purpose is, and has always been,
Followup-To.

Dan
 
D

Darklight

Malcolm said:
You still haven't fixed the fgets() problem. fgets() is actually quite a
difficult function to use.

i was not aware there was a fgets problem could you please explain.
 
D

Darklight

Papadopoulos said:
Is this a homework? Cause if it is, you got it wrong..

In a homework situation, the tutor is probably suggesting that YOU
create the strlen() function and not to use the ready one...
Question taken from a book hence i need people like your
self to look at my code. And correct me if i am wrong
 
M

Martin Dickopp

Which RFC defines this header?

AFAIKT it's a non-standard, but widely implemented header.
The RFC apparently never made it beyond draft stage
(cf. said:
AFAIK, the right header for this purpose is, and has always been,
Followup-To.

As I understand RFC 1036, Followup-To doesn't allow it to request
followups to be posted to newsgroup(s) /and/ mailed to an email address.

Martin
 
M

Martin Dickopp

Darklight said:
i was not aware there was a fgets problem could you please explain.

If the user enters 100 or more characters after the first prompt, the
first `fgets' will read 99 characters, store them in the buffer, and
append a '\0' character. The remaining (again up to 99) characters will
be read by the second `fgets' call, so the user never has a chance to
enter a second string in this case. Part of what the user considers the
first string will be stored in `b'.

If this is not clear, just try it. Enter more than 99 characters and see
what happens.

Martin
 
T

those who know me have no need of my name

in comp.lang.c i read:
In <[email protected]> Martin Dickopp


Which RFC defines this header?

much of usenet operates without proper rfc's.
AFAIK, the right header for this purpose is, and has always been,
Followup-To.

mail-copies-to is orthogonal to followup-to (except that m-c-t shouldn't be
used when followup-to poster is used), it controls follow-up copies, either
requesting them and specifying the address to use, or requesting that they
never be sent, it does not change the venue of the discussion.
 
C

CBFalconer

Martin said:
Once again, please don't do that.

I am confused. I see no such phrase in the article to which you
are replying. That address appears only in the 'From:' header
line. There is such a line in the headers, but that seems to be
something peculiar to his system. He has no reply-to: header.
 
M

Martin Dickopp

CBFalconer said:
I am confused. I see no such phrase in the article to which you
are replying.

That line was not in the message body, but in the headers. I didn't
mention that fact because I had already asked the OP to remove the
header or point it to valid address in an earlier reply. Sorry for the
confusion.

My apologies to the OP for pointing it out /twice/. I realize now that
that was perhaps unnecessarily harsh.
That address appears only in the 'From:' header line. There is such a
line in the headers, but that seems to be something peculiar to his
system. He has no reply-to: header.

Are we talking about the same message (<[email protected]>)?
It has that address in the From, Reply-To, and Mail-Copies-To headers.
However, only Mail-Copies-To asks for mail copies of replies, while
Reply-To states the address that mail should be send to if the person
replying decides to do so by mail.

Martin
 
D

Dan Pop

In said:
in comp.lang.c i read:

much of usenet operates without proper rfc's.
^^^^^^^^^^^^^^
Care to provide some concrete examples?

AFAICT, the Usenet is misused/abused when its usage goes beyond its
relevant RFCs.

Dan
 
D

Dan Pop

In said:
AFAIKT it's a non-standard, but widely implemented header.

I.e. it's a bug in the software implementing it. If it didn't make its
way into an RFC, there must be a reason.

Even the Followup-to header is a mistake, IMHO. The poster has no
business controlling the options of the people who decide to reply.

He may kindly suggest followups to a subset of the Newsgroups header or
even by private email, but not enforce it.

Dan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,118
Latest member
LatishaWhy
Top