谁能帮我? [英] Can any one help me?

查看:81
本文介绍了谁能帮我?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我去解决了与Palindrome相关的问题



int isPalindrome(char * c);

$ b如果字符串是回文,则$ b返回1,如果

它不是回文,则返回零。


但不使用thr库(字符串) .h)


我可以测试我的功能是什么问题而不使用上面的

库?


功能是:


int isPalindrome(char c [])

{

int a,b;

if(a> = b)

返回1;

否则if(c [a]!= c [b])

返回0;

其他

返回isPalindrome(c);


}

I went to solve a problem related
to Palindrome

int isPalindrome(char *c);

returns back 1 if the string is a palindrome and returns back zero if
it is not a palindrome.

but without using thr library from( string.h)

who I can test my function that is the problem without using the
above library ?

the function is:

int isPalindrome(char c[])
{
int a, b;
if (a >= b)
return 1;
else if (c[a] != c[b])
return 0;
else
return isPalindrome(c);

}

推荐答案

join写道:
我去解决了与Palindrome相关的问题

int isPalindrome(char * c);

如果字符串是回文,则返回1,如果它不是palin,则返回零drome。

但是没有使用来自(string.h)的thr库我可以在不使用上面的库的情况下测试我的函数是什么问题?<这个函数是:

int isPalindrome(char c [])
{
int a,b;
if(a> = b)
返回1;
如果(c [a]!= c [b])
返回0;

返回isPalindrome(c);

}
I went to solve a problem related
to Palindrome

int isPalindrome(char *c);

returns back 1 if the string is a palindrome and returns back zero if
it is not a palindrome.

but without using thr library from( string.h)

who I can test my function that is the problem without using the
above library ?

the function is:

int isPalindrome(char c[])
{
int a, b;
if (a >= b)
return 1;
else if (c[a] != c[b])
return 0;
else
return isPalindrome(c);

}




您的代码存在一些问题:


1)a和b永远不会被初始化。

2)递归无法退出


如果你想使用递归,传入a和b,用初始值

为0,比字符串中的字符数少一个

(使用你自己实现的strlen版本找到,因为

strlen你的家庭作业要求分别禁止使用string.h)。您的递归通话需要增加

a并递减b ...


根据代码我的​​印象是你还在学习

C,您可能会考虑编写迭代版本而不是

或者除了递归版本之外,因为它可能更直接

供您调试。


至于测试它,为什么你需要字符串库呢?

stdio可能会报告结果?


-David



There are a few issues with your code:

1) a and b are never initialized.
2) recursion has no way to exit

If you want to use recursion, pass in a and b, with the initial values
as 0 and one less than the number of characters in the string
(found using a version of strlen you implement yourself, since
strlen is forbidden by your appparent homework requirements about not
using string.h) respectively. Your recursive call needs to increment
a and decrement b...

As based on the code my impression is that you are still learning
C, you might consider writing an iterative version instead of
or in addition to the recursive one, as it may be more straightforward
for you to debug.

As for testing it, why do you need the string library for that?
stdio to report results perhaps?

-David


h _ ******* @ hotmail-dot-com.no-spam.inva lid(join)写道:
h_*******@hotmail-dot-com.no-spam.invalid (join) writes:
我可以测试我的功能这是没有使用上面库的问题吗?

这个函数是:

int isPalindrome(char c [])
{
int a,b;
if(a> = b)
返回1;
否则if(c [a]!= c [b])
返回0;

返回isPalindrome(c);

}
who I can test my function that is the problem without using the
above library ?

the function is:

int isPalindrome(char c[])
{
int a, b;
if (a >= b)
return 1;
else if (c[a] != c[b])
return 0;
else
return isPalindrome(c);

}




我已经可以告诉你这个功能没有了不行。它使用了'a''和'b''的值,而没有先初始化它们。


另外,我不明白你为什么要这样做避免在测试你的函数时使用字符串函数

,即使你写的函数应该不使用它们。代码和测试的限制是相关的,但是是分开的。

-

我错过了什么吗?

--Dan Pop



I can already tell you that the function doesn''t work. It uses
the values of `a'' and `b'' without first initializing them.

Also, I don''t see why you should avoiding using string functions
in testing your function, even if the function you wrote should
not use them. Constraints on code and on its testing are
related, but separate.
--
"Am I missing something?"
--Dan Pop


2006年3月13日星期一19:06,加入opined(

< KI * *******************@giganews.com>):
On Monday 13 March 2006 19:06, join opined (in
<KI********************@giganews.com>):
我去解决相关的问题
Palindrome

int isPalindrome(char * c);

如果字符串是回文,则返回1,如果它不是回文,则返回零。

但是没有使用来自(string.h)的thr库


这听起来与几个星期前在这里提出的问题非常相似。

你可能想在Google上搜索这个小组。


这听起来像是一个家庭作业......为什么你会羞于stanandard

库函数?一个原因可能是您正在使用嵌入式

实现,以及< string.h>功能太贵了,但是很少是一个嵌入式系统,想要知道一个字符串是否是一个
palindrome(也许是一个5岁以下的玩具)。 />
我可以在不使用上面的库的情况下测试我的功能是什么问题?


我认为你的意思是我怎么测试?。

功能是:


#include< stdio.h>

int isPalindrome(char c [])
{
int a,b;
if(a> = b)
返回1;
如果(c [a]!= c [b])
返回0;

返回isPalindrome(c);

}


int main(无效)

{

printf(" isPalindrome =%d \ n,isPalindrome(anavolimilovana));


返回0;

}


将是这样做的一种方式。您会发现该函数没有工作,因为以下原因:

int isPalindrome(char c [])


int isPalindrome(const char * c)


可能更好,但不完全相同。

{
int a,b ;


你永远不会初始化`a`和`b`。在这个函数的每次调用中,他们都假设完全随机的值(它们是未初始化的)。根据你的方法来判断
,你可能希望`a`开始指向

最后一个字符(你想在这里使用`strlen`,或者滚动你自己),并且

`b`开始为0.如果字符串

为空,你也想立即退出(c [0] = =''\''或更好的strlen(c)== 0)确定

之后指向它的指针不是NULL(c!= NULL)。

如果(a> = = b)
返回1;
否则if(c [a]!= c [b])
返回0;
否则


在else分支中你可能想减少`a`并增加

`b`。

return isPalindrome(c);


另外,在你的函数中你永远不会改变`a`和`b`的值。

}
I went to solve a problem related
to Palindrome

int isPalindrome(char *c);

returns back 1 if the string is a palindrome and returns back zero if
it is not a palindrome.

but without using thr library from( string.h)
This sounds remarkably similar to something asked here a few weeks back.
You may want to search the group on Google.

It also sounds like a homework... Why else would you shy from stnandard
library functions? One reason may be that you''re using an embedded
implementation, and the <string.h> functions are too expensive, but
then, rare is an embedded system wanting to know whether a string is a
palindrome (an under-5 toy, perhaps).
who I can test my function that is the problem without using the
above library ?
I presume you mean "how I can test?".
the function is:

#include <stdio.h>
int isPalindrome(char c[])
{
int a, b;
if (a >= b)
return 1;
else if (c[a] != c[b])
return 0;
else
return isPalindrome(c);

}
int main(void)
{
printf("isPalindrome = %d\n",isPalindrome("anavolimilovana"));

return 0;
}

Would be one way of doing it. You''ll find that the function does not
work, for the following reasons:
int isPalindrome(char c[])
int isPalindrome(const char *c)

Is probably better, although not quite the same.
{
int a, b;
You never initialise `a` and `b`. On every invokation of this function
they assume completely random values (they are uninitialised). Judging
by your approach, you may have wanted `a` to start off pointing to the
last character (you''d want to use `strlen` here, or roll your own), and
`b` to start as 0. You''d also want to exit immediately if the string
was empty (c[0] == ''\0'', or better strlen(c) == 0) after determining
that pointer to it is not NULL (c != NULL).
if (a >= b)
return 1;
else if (c[a] != c[b])
return 0;
else
In the else branch you prbably wanted to decrement `a` and increment
`b`.
return isPalindrome(c);
Also, never in your function you change the values of `a` and `b`.
}




NB,带上我的建议(即仔细检查

逻辑),因为我没有测试它们。


-

BR,弗拉基米尔


那些认为你知道一切的人对我们来说非常讨厌

我们谁呢。



NB, take my suggestions with a grain of salt (i.e. double-check the
logic), as I did not test them.

--
BR, Vladimir

Those of you who think you know everything are very annoying to those
of us who do.


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

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