473,503 Members | 1,656 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Can any one help me?

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

}

Mar 13 '06 #1
12 2000
join wrote:
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);

}


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

Mar 13 '06 #2
h_*******@hotmail-dot-com.no-spam.invalid (join) writes:
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);

}


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
Mar 13 '06 #3
On Monday 13 March 2006 19:06, join opined (in
<KI********************@giganews.com>):
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, 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.

Mar 13 '06 #4

"join" <h_*******@hotmail-dot-com.no-spam.invalid> wrote in message
news:KI********************@giganews.com...
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

Mar 13 '06 #5
On 2006-03-13, Rod Pemberton <do*********@sorry.bitbuck.cmm> wrote:

"join" <h_*******@hotmail-dot-com.no-spam.invalid> wrote in message
news:KI********************@giganews.com...
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


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...
Mar 13 '06 #6
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!
"join" <h_*******@hotmail-dot-com.no-spam.invalid> wrote in message
news:KI********************@giganews.com...
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


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"?
Mar 14 '06 #7
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.

Mar 14 '06 #8
join wrote:

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

Mar 14 '06 #9
join wrote:
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
Mar 14 '06 #10
"join" writes:
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[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";
Mar 14 '06 #11
join wrote:

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)
Mar 15 '06 #12
CBFalconer wrote:
join wrote:
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 */


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.

--
jay
Mar 15 '06 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
11176
by: William C. White | last post by:
Does anyone know of a way to use PHP /w Authorize.net AIM without using cURL? Our website is hosted on a shared drive and the webhost company doesn't installed additional software (such as cURL)...
2
5773
by: Albert Ahtenberg | last post by:
Hello, I don't know if it is only me but I was sure that header("Location:url") redirects the browser instantly to URL, or at least stops the execution of the code. But appearantely it continues...
3
22956
by: James | last post by:
Hi, I have a form with 2 fields. 'A' 'B' The user completes one of the fields and the form is submitted. On the results page I want to run a query, but this will change subject to which...
0
8430
by: Ollivier Robert | last post by:
Hello, I'm trying to link PHP with Oracle 9.2.0/OCI8 with gcc 3.2.3 on a Solaris9 system. The link succeeds but everytime I try to run php, I get a SEGV from inside the libcnltsh.so library. ...
1
8535
by: Richard Galli | last post by:
I want viewers to compare state laws on a single subject. Imagine a three-column table with a drop-down box on the top. A viewer selects a state from the list, and that state's text fills the...
4
18214
by: Albert Ahtenberg | last post by:
Hello, I have two questions. 1. When the user presses the back button and returns to a form he filled the form is reseted. How do I leave there the values he inserted? 2. When the...
1
6776
by: inderjit S Gabrie | last post by:
Hi all Here is the scenerio ...is it possibly to do this... i am getting valid course dates output on to a web which i have designed ....all is okay so far , look at the following web url ...
2
31340
by: Jack | last post by:
Hi All, What is the PHP equivilent of Oracle bind variables in a SQL statement, e.g. select x from y where z=:parameter Which in asp/jsp would be followed by some statements to bind a value...
3
23531
by: Sandwick | last post by:
I am trying to change the size of a drawing so they are all 3x3. the script below is what i was trying to use to cut it in half ... I get errors. I can display the normal picture but not the...
0
7072
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7271
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7319
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7449
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5570
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4998
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4666
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3160
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1498
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.