Connecting Tech Pros Worldwide Forums | Help | Site Map

Can any one help me?

join
Guest
 
Posts: n/a
#1: Mar 13 '06
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);

}


David Resnick
Guest
 
Posts: n/a
#2: Mar 13 '06

re: Can any one help me?


join wrote:[color=blue]
> 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);
>
> }[/color]

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

Ben Pfaff
Guest
 
Posts: n/a
#3: Mar 13 '06

re: Can any one help me?


h_u_s2002@hotmail-dot-com.no-spam.invalid (join) writes:
[color=blue]
> 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);
>
> }[/color]

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
Vladimir S. Oka
Guest
 
Posts: n/a
#4: Mar 13 '06

re: Can any one help me?


On Monday 13 March 2006 19:06, join opined (in
<KIednQy9--w3X4jZRVn-uA@giganews.com>):
[color=blue]
> 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)[/color]

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).
[color=blue]
> who I can test my function that is the problem without using the
> above library ?[/color]

I presume you mean "how I can test?".
[color=blue]
> the function is:
>[/color]

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

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:
[color=blue]
> int isPalindrome(char c[])[/color]

int isPalindrome(const char *c)

Is probably better, although not quite the same.
[color=blue]
> {
> int a, b;[/color]

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).
[color=blue]
> if (a >= b)
> return 1;
> else if (c[a] != c[b])
> return 0;
> else[/color]

In the else branch you prbably wanted to decrement `a` and increment
`b`.
[color=blue]
> return isPalindrome(c);[/color]

Also, never in your function you change the values of `a` and `b`.
[color=blue]
> }[/color]

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.

Rod Pemberton
Guest
 
Posts: n/a
#5: Mar 13 '06

re: Can any one help me?



"join" <h_u_s2002@hotmail-dot-com.no-spam.invalid> wrote in message
news:KIednQy9--w3X4jZRVn-uA@giganews.com...
[color=blue]
> but without using thr library from( string.h)[/color]

Your problem will be much easier to solve if you write functions to:
1) copy a string
2) reverse a string
3) compare a string


Rod Pemberton



Richard G. Riley
Guest
 
Posts: n/a
#6: Mar 13 '06

re: Can any one help me?


On 2006-03-13, Rod Pemberton <do_not_have@sorry.bitbuck.cmm> wrote:[color=blue]
>
> "join" <h_u_s2002@hotmail-dot-com.no-spam.invalid> wrote in message
> news:KIednQy9--w3X4jZRVn-uA@giganews.com...
>[color=green]
>> but without using thr library from( string.h)[/color]
>
> Your problem will be much easier to solve if you write functions to:
> 1) copy a string
> 2) reverse a string
> 3) compare a string
>
>
> Rod Pemberton
>[/color]

True.

Alternatively he could do none of the above and just do a pincer
movement compare from end of potential palindrome and start of
potential palindrome comparing the characters and exiting when the pointers
meet.

For extra bonus assignment points he might want to take into account
that a Palindrome is case insensitive...
Peter Shaggy Haywood
Guest
 
Posts: n/a
#7: Mar 14 '06

re: Can any one help me?


Groovy hepcat Rod Pemberton was jivin' on Mon, 13 Mar 2006 15:17:09
-0500 in comp.lang.c.
Re: Can any one help me?'s a cool scene! Dig it!
[color=blue]
>"join" <h_u_s2002@hotmail-dot-com.no-spam.invalid> wrote in message
>news:KIednQy9--w3X4jZRVn-uA@giganews.com...
>[color=green]
>> but without using thr library from( string.h)[/color]
>
>Your problem will be much easier to solve if you write functions to:
>1) copy a string
>2) reverse a string
>3) compare a string[/color]

Merely reversing a copy of the string and comparing it to the
original won't work correctly. The problem, as stated, is to determine
whether a string a a palidrome. No definition of palindrome has been
given, so the general meaning is assumed. A palindrome is a sentence
or phrase whose letters spell the same thing forward or reverse.
The function should ignore white space and punctuation. It should
also ignore differences in case. The following is a palandrome, but is
not the same when reversed:

Was it a car or a cat I saw?

Besides, you don't really need to make a copy of the string. You
only have to compare (case insensitively) the first as-yet-uncompared
alphabetic (or alphanumeric) character to the last as-yet-uncompared
alphabetic (or alphanumeric) character. That's trivial (and a clue for
the OP).
Now, if the OP wants to provide a more restrictive definition of
palindrome for the purpose of this project, then it may make things
easier.

--

Dig the even newer still, yet more improved, sig!

http://alphalink.com.au/~phaywood/
"Ain't I'm a dog?" - Ronny Self, Ain't I'm a Dog, written by G. Sherry & W. Walker.
I know it's not "technically correct" English; but since when was rock & roll "technically correct"?
join
Guest
 
Posts: n/a
#8: Mar 14 '06

re: Can any one help me?


think you for everu body here.

In fact , I am a new student for programming language , and I am
truing to learn it as I can as possible , and I am truing to slove
some problem like this , forget about my function.

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

}


Ignore this function.

Can any one solve this problem to me without using the library
<string.h> and give me the code?

thing you for every body.

David Resnick
Guest
 
Posts: n/a
#9: Mar 14 '06

re: Can any one help me?


join wrote:[color=blue]
>
> Can any one solve this problem to me without using the library
> <string.h> and give me the code?
>
> thing you for every body.[/color]

People here give hints for homework if you show some code (as you did)
and seem to be making an effort. But generally they don't do it for
you.

-David

Default User
Guest
 
Posts: n/a
#10: Mar 14 '06

re: Can any one help me?


join wrote:
[color=blue]
> think you for everu body here.
>
> In fact , I am a new student for programming language , and I am
> truing to learn it as I can as possible , and I am truing to slove
> some problem like this , forget about my function.[/color]
[color=blue]
> Can any one solve this problem to me without using the library
> <string.h> and give me the code?[/color]

Basically:

1. Yes, most of us can solve this problem.

2. No, we won't give the code.


It's an obvious student problem, which means its goal is to teach you
something about the C language. If we do your work for you, you don't
learn.

Work out the actual requirements of how the algorithm should work. One
way is to start with a string and do it by hand. Make note of how you
solve it that way. Turn that into a program.




Brian
osmium
Guest
 
Posts: n/a
#11: Mar 14 '06

re: Can any one help me?


"join" writes:
[color=blue]
> In fact , I am a new student for programming language , and I am
> truing to learn it as I can as possible , and I am truing to slove
> some problem like this , forget about my function.
>
> int isPalindrome(char c[])
> {
> int a, b;
> if (a >= b)
> return 1;
> else if (c[a] != c)
> return 0;
> else
> return isPalindrome(c);
>
> }
>
>
> Ignore this function.
>
> Can any one solve this problem to me without using the library
> <string.h> and give me the code?[/color]

You are going to have to find out how many characters are in the string,
first. Can you do that? Set a vaiable named n to be the index of the last
character. Then, assuming a "sanitized" string you can do something like
this:
int i, j;
for(i=0; j=n; i<j; i++; j--)
if(c[i] != c[j])
return 0; /*not a palindrome */
return 1;

By sanitized I mean, no spaces no upper case letters, no punctuation. For
testing, choose a palindrome you like and build it into the main part of the
program. So you don't have to keep re-typing it, and maybe typing it wrong.

Like this:

char test[] = "amanaplanacanalpanama";


CBFalconer
Guest
 
Posts: n/a
#12: Mar 15 '06

re: Can any one help me?


join wrote:[color=blue]
>
> 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)[/color]

string.h is not a library, it simply declares some functions,
types, etc. available in the standard library. With proper
declarations you can then use those routines in your code.

I don't know why I am doing this, but try this:

int isPalindrome(char *s) {
char *t;

t = s;
while (*t++) continue; /* find end of string */
t--; /* point to last char */
while (t > s) {
if (*t != *s) return 0;
s++; t--;
}
return 1;
} /* untested */

Notice what it does for the boundary conditions, i.e. strings of
length 0 and length 1. Note it also assumes the input string s is
correct and terminates in a '\0'. It DOES NOT test for, nor make
assumptions, about s not being NULL.

--
Some useful references about C:
<http://www.ungerhu.com/jxh/clc.welcome.txt>
<http://www.eskimo.com/~scs/C-faq/top.html>
<http://benpfaff.org/writings/clc/off-topic.html>
<http://anubis.dkuug.dk/jtc1/sc22/wg14/www/docs/n869/> (C99)
<http://www.dinkumware.com/refxc.html> (C-library}
<http://gcc.gnu.org/onlinedocs/> (GNU docs)
<http://clc-wiki.net> (C-info)


jaysome
Guest
 
Posts: n/a
#13: Mar 15 '06

re: Can any one help me?


CBFalconer wrote:[color=blue]
> join wrote:
>[color=green]
>>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)[/color]
>
>
> string.h is not a library, it simply declares some functions,
> types, etc. available in the standard library. With proper
> declarations you can then use those routines in your code.
>
> I don't know why I am doing this, but try this:
>
> int isPalindrome(char *s) {
> char *t;
>
> t = s;
> while (*t++) continue; /* find end of string */[/color]

When this loop exits, t is pointing to one char past the '\0' (which I
believe is not undefined behavior?).
[color=blue]
> t--; /* point to last char */[/color]

Now t points to the '\0'.
[color=blue]
> while (t > s) {
> if (*t != *s) return 0;[/color]

Therefore, this statement will be true for strings of strlen() greater
than 0. I think you need another "t--;" statement above. You did say it
was untested :)
[color=blue]
> s++; t--;
> Notice what it does for the boundary conditions, i.e. strings of
> length 0 and length 1. Note it also assumes the input string s is
> correct and terminates in a '\0'. It DOES NOT test for, nor make
> assumptions, about s not being NULL.[/color]

This is a convenient start. All that's left is to make it case
insensitive and punctuation insensitive. Ideally, case insensitivity
would take into account the current locale.

--
jay
Closed Thread