Can any one help me?

J

join

I went to solve a problem relate
to Palindrom

int isPalindrome(char *c)

returns back 1 if the string is a palindrome and returns back zero i
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 th
above library ?

the function is:

int isPalindrome(char c[]

int a, b
if (a >= b
return 1
else if (c[a] != c
return 0
els
return isPalindrome(c)
 
B

Ben Pfaff

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)
return 0;
else
return isPalindrome(c);

}


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.
 
D

David Resnick

join said:
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)
return 0;
else
return isPalindrome(c);

}


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
 
V

Vladimir S. Oka

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 said:
int isPalindrome(char c[])
{
int a, b;
if (a >= b)
return 1;
else if (c[a] != c)
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)
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, take my suggestions with a grain of salt (i.e. double-check the
logic), as I did not test them.
 
R

Rod Pemberton

but without using thr library from( string.h)

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
 
R

Richard G. Riley

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

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...
 
P

Peter Shaggy Haywood

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!
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

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"?
 
J

join

think you for everu body here

In fact , I am a new student for programming language , and I a
truing to learn it as I can as possible , and I am truing to slov
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
els
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
 
D

David Resnick

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

thing you for every body.

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
 
D

Default User

join said:
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.
Can any one solve this problem to me without using the library
<string.h> and give me the code?

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
 
O

osmium

join said:
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?

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 != 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";
 
C

CBFalconer

join said:
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)

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

jaysome

CBFalconer said:
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 */

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

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

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 :)
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.

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.
 

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,754
Messages
2,569,527
Members
44,998
Latest member
MarissaEub

Latest Threads

Top