LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-10-2008, 03:30 AM   #1
fm_hyudin
LQ Newbie
 
Registered: Jun 2008
Posts: 17

Rep: Reputation: 0
Question need help with the c programming code?


I already did overall coding for this question but i don't understand last statement saying [Enter a five-digit number ( -1 to end ): -1].Can anyone plz tell me and edit my coding for me plz.Thanks alot for your help.

Code:
#include <stdio.h> 
#include <string.h> 

#define TRUE 1 
#define FALSE 0 

main () { 

char str[30]; 
int isPalindrome = TRUE; 
int i,j; 


printf("Enter a five-digit number : "); 
gets(str); 
j = strlen(str) - 1; 
i = 0; 


while(i <= j && isPalindrome) { 

if(str[i] != str[j]) { 
isPalindrome = FALSE; 
} 
i++; 
j--; 
} 

if(isPalindrome) { 
printf("%s is a palindrome!\n", str); 
} 
else { 
printf("%s is not a palindrome\n", str); 
} 
return 0; 
}
Question 1

A palindrome is a number or a text phrase that reads the same backward as forward. For example, each of the following five-digit integers is a palindrome: 12321, 55555, 45554 and 11611. Write a program that reads in a five-digit integer and determines whether or not it is a palindrome. [HINT: Use the division and remainder operators to separate the number into its individual digits.]
(5 marks)

Sample output:

Enter a five-digit number ( -1 to end ): 18181
18181 is a palindrome

Enter a five-digit number ( -1 to end ): 16738
16738 is not a palindrome

Enter a five-digit number ( -1 to end ): -1
 
Old 09-10-2008, 03:47 AM   #2
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
Seeing as you've already written some code.. I think the Sample output implies (!) that your prog should be an infinite loop that repeatedly asks for a number to test, and only exits if you give it the value '-1' to test.
 
Old 09-10-2008, 03:52 AM   #3
billymayday
LQ Guru
 
Registered: Mar 2006
Location: Sydney, Australia
Distribution: Fedora, CentOS, OpenSuse, Slack, Gentoo, Debian, Arch, PCBSD
Posts: 6,678

Rep: Reputation: 122Reputation: 122
It also gives a pretty strong hint to read the input as an integer, not a string, so you may want to comply.

I wouldn't use an infinite loop though, I'd use use a while/do loop and test the input at the start.
 
Old 09-10-2008, 03:57 AM   #4
fm_hyudin
LQ Newbie
 
Registered: Jun 2008
Posts: 17

Original Poster
Rep: Reputation: 0
thanks for the help.any more guidance on where i should adjust the codes plz?
 
Old 09-10-2008, 04:00 AM   #5
billymayday
LQ Guru
 
Registered: Mar 2006
Location: Sydney, Australia
Distribution: Fedora, CentOS, OpenSuse, Slack, Gentoo, Debian, Arch, PCBSD
Posts: 6,678

Rep: Reputation: 122Reputation: 122
Did you write that code yourself? Surely you know enough to do a loop with a simple test. If not, you should if you expect to pass.
 
Old 09-10-2008, 08:10 PM   #6
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
@billy, y I was a bit loose with the word 'infinite' there
Your soln is basically what I meant.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:26 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration