它是否正确 [英] is this correct

查看:96
本文介绍了它是否正确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题6:编写一个接受两个字符串的函数。计算每个字符中

字符的数量,并返回指向较长字符串的指针。

并且请评论


/ * LEN_STRING.C程序返回最长的字符串* /

#include< stdio.h>

#include< string.h>


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


int main(无效)

{


char a [100];

char b [100];


printf(" Enter string1 \ n");

fgets(a,100,stdin);


printf(" Enter string2 \ n");

fgets(b,100 ,stdin);


长度(a,b);


返回0;

}


void length(char string1 [],char string2 [])

{

char * line;


if((strlen(string1))>(strlen(string2)))

{

line = string1;

printf(" \ nstring 1是最长的\ n%s \ n"行);

}

else if(strlen(str) ing1)< strlen(string2))

{

line = string2;

printf(" \ nstring 2是最长的\\ n%s \ nn,行;

}

其他

printf(" \ nBoth字符串长度相同\ n");

}

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");
}

推荐答案

Darklight< ng ****** @ netscape.net> ;写道:
Darklight <ng******@netscape.net> writes:
Q6:编写一个接受两个字符串的函数。计算每个中
字符的数量,并返回指向较长字符串的指针。
并且请评论

/ * LEN_STRING.C程序返回最长的字符串* /
#include< stdio.h>
#include< string.h>

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


这个问题要求返回一个指向更长字符串的指针,但你的

`length''函数什么都不返回。另外,一个更好的函数名称是

就像`long_string'',因为这就是函数是什么,b $ b应该返回。

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) );


如果用户在第一次提示后输入100个或更多字符,则第二次fgets调用会读取这些

个字符。

长度(a,b);

返回0;
}

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

if((strlen(string1))>(strlen(string2)))
{
line = string1;
printf(\ nstring 1是最长的\ n%s \ n,行;
}
if if(strlen(string1)< strlen(string2) )
{
line = string2;
printf(" \ nstring 2是最长的\ n%s \ n"行);
} 否则
printf(\ nBoth字符串长度相同\ n);
}
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");
}




它可能是一个好主意不要在'length''函数中做任何输出,

但是只返回一个点呃更长的字符串(根据

问题的要求)。取而代之的是`main'的输出。


当两个字符串的长度相同时,你考虑过这个情况是好的,尽管问题是没有在这个

的情况下指定做什么。


为每个字符串调用两次strlen可能会降低性能。

使用变量


const size_t len1 = strlen(string1);

const size_t len2 = strlen(string2);


并比较这些。


并非真正需要`line''变量。在你知道/

的块中,'string1''更长,你可以使用`string1''。同样地,

`string2''。


最后,标识符以str开头,后跟小写字母

保留用于实施。考虑不同的名字,例如

`str1''和'str2''。


Martin

-

, - 。 Martin Dickopp,德国德累斯顿,=, - _-。 =。

/, - ) http://www.zero -based.org/ ((_ /)oo(\_))

\` - ''` - ''(。)` - ''

` - 。 Debian,GNU操作系统的一种变体。 \_ /



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
--
,--. Martin Dickopp, Dresden, Germany ,= ,-_-. =.
/ ,- ) http://www.zero-based.org/ ((_/)o o(\_))
\ `-'' `-''(. .)`-''
`-. Debian, a variant of the GNU operating system. \_/


Darklight< ng ****** @ netscape.net>写道:
Darklight <ng******@netscape.net> writes:
Mail-Copies-To: ng ****** @ netscape。 net
Mail-Copies-To: ng******@netscape.net




您在发布中设置上述标题。这导致我的新闻阅读器

发送了我的回复邮件副本给你,但由于

无效的电子邮件地址而被退回。


不要将Mail-Copies-To设置为无效的电子邮件地址!


Martin

-

, - 。 Martin Dickopp,德国德累斯顿,=, - _-。 =。

/, - ) http://www.zero -based.org/ ((_ /)oo(\_))

\` - ''` - ''(。)` - ''

` - 。 Debian,GNU操作系统的一种变体。 \_ /



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
--
,--. Martin Dickopp, Dresden, Germany ,= ,-_-. =.
/ ,- ) http://www.zero-based.org/ ((_/)o o(\_))
\ `-'' `-''(. .)`-''
`-. Debian, a variant of the GNU operating system. \_/





Darklight写道:


Darklight wrote:
Q6:写一个接受两个字符串的函数。计算每个中
字符的数量,并返回指向较长字符串的指针。
请评论

void length(char [],char []);
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 [ ]);




看看你的要求,想知道你想要什么

如果长度相等则返回。


渴望你的原型,它不符合要求

,因为它不会返回任何东西。另外,由于字符串

不会被修改,你可以制作参数

const char。


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


如果返回哪个字符串指针无关紧要

长度为相等,那么定义很简单。


#include< stdio.h>

#include< string.h>


const char * length(const char * string1,const char * string2)

{

return strlen(string1)> = strlen(string2) ?string1:string2;

}


int main(无效)

{

char * str1 =早安;

char * str2 =晚安;


printf(" \\长度为\% s \"和\"%s \" \ n"

"是\"%s \" \ n",str1,str2,长度(str1,str2));

返回0;

}


-

Al Bowers

美国佛罗里达州坦帕市

mailto: xa * *****@myrapidsys.com (删除x以发送电子邮件)
http://www.geocities.com/abowers822/



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;
}

--
Al Bowers
Tampa, Fl USA
mailto: xa******@myrapidsys.com (remove the x to send email)
http://www.geocities.com/abowers822/


这篇关于它是否正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆