Question:
C programming problem.?
c-noob
2008-10-14 03:43:26 UTC
I'm trying to write the following string function:
int palindrome(char string[]),
which returns 1 if the string is a palindrome and 0 otherwise. (A string is a palindrome if it reads the same either backward or forward.) I have to use functions in . I'm somewhere struck and I need some help. Here's my program:

#include
int palindrome(char string[]){
/* Totally no idea on how to write the body of this function here.*/
}
main(int argc, char *argv[]) {
if (palindrome(argv[1])==1) printf(“Is %s a palindrome? %s\n”, argv[1], “Yes” );
else printf(“Is %s a palindrome? %s\n”, argv[1], “No” );
}

Can someone teach me how to write it in codes? Thanks.
Five answers:
Tizio 008
2008-10-14 04:01:43 UTC
i have already given code for palindromicity check in two different ways: "direct" (looping) and using a recursive function!!

but since my answers are deeply hidden under a stack of other answers...



function in string.h can be avoided... i can't see how they turn in utility... but if you "must" use them... hm let me think



you can split the string in two segment of the same length (for this you need copying... string.h funcs!!); you need strlen too, in order to check where to break the word.



then you have a LEFT and RIGHT part. you reverse one of these, (e.g. the right one) and use strcmp. if strcmp says they are the same, you can return 1.





another way is using a loop: the index i through string[i] gives a char, the "specular" char will be string[N-i-1], where N=strlen(string). you loop from 0 to the middle (or so) of the string comparing the char and its specular, and if you reach the end of the loop, the word is palindrome, otherwise you can return 0 (false) before reaching the end (it is enough a couple of different chars to make it not palindrome)





third method: recursion.

if strlen(s)==1, return 1 (a singlelchar string is the minimal palindrome...)

if strlen(s)==2 AND s[0]==s[1] return 1, else 0



otherwise:

cmp the first and the last char, if they are the same, strip them and pass the resulting string to the same function again... ( pseudo lang: return palindror(strippedstring) )

if they are not the same, return 0





i hope i have given you good ideas...

now you can try to implement them, and if you have problems, you can ask again...



~add~

o yes, you don't need to split in two in the first case, it is enough to rverse the whole string :)
cja
2008-10-14 04:07:06 UTC
An easy way to do it is to reverse the string and check if the result is the same as what you started with. Like this:



#include

#include

#include



void strrev(char * const,char *);



int main(int argc, char *argv[]) {

if (argc > 1) {

char *sRev = malloc(strlen(argv[1]));

strrev(argv[1],sRev);

printf("%s reversed = %s\n",argv[1],sRev);

printf("%s is ",argv[1]);

if (strcmp(argv[1],sRev) != 0) printf("not ");

puts("a palindrome.");

free(sRev);

}

return 0;

}



void strrev(char * const s,char *sRev) {

int i, len = strlen(s);

char *p = &s[len-1];

for (i = 0; i < len; i++) {

sRev[i] = *p--;

}

sRev[i] = '\0';

}



Plenty of opportunities here to use string.h functions, which you wanted to do.
iqbal
2008-10-14 04:49:38 UTC
Hi,

int palindrome(char *p)

{

char *r;

strcpy(r,p);

strrev(p);



if(strcmp(r,p)==0)

return(1);

else

return(0);

}
Venkata R
2008-10-14 04:04:12 UTC
int palindrome(char string[]){

int res=1;

int len=strlen(string); //Since used twice, better to save it

for(int i=0,,j=len-1;i<=ceil(len/2-1);i++, j--)

{

if(string[i]!=string[j])

{

res=0;

break;

}

}

return res;

}
?
2016-10-25 11:31:58 UTC
write a module to opposite a given string "i'm perplexed" could change into "desufnoc ma i" Then split the string on areas and bypass each and every note back to an identical module. "desufnoc ma i" could change into "perplexed am i" Thats one way that would want to artwork


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...