Please support our C advertiser: Programming Forums
Views: 5570 | Replies: 63
![]() |
First thing:
1. You cant define functinos within functions, its not allowed in C / C++.
2.
Also do read the link I had posted previously because it contains each and every step on how to preform the palindrome check.
Make updates and repost.
1. You cant define functinos within functions, its not allowed in C / C++.
2.
int main(void)
{
char palindrome[80];
int ispalindrome=1;/*boolean value True*/
int i=0;
int j=0;
/* get the user input */
do{
printf("Enter a string no more than 80 characters ");
fgets(palindrome,80,stdin);
// call remove_newline () function here
j = strlen(palindrome) - 1;
i = 0;
//////////The below one is a function !!!!///////////////////////
// you need to place it out of main( ) and then call it in main
// with the argument palindrome. Better name the string variable
// palindrome to "input" or "my_string" to avoid confusion.
/* Remove the trailing newline*/
void remove_newline( char* palindrome ){
char* p = 0 ;
if( p = strrchr( palindrome, '\n' ) )
*p = '\0' ;
}
/////////////////////////////////////////////////////////////
int string_length=strlen(palindrome);
if(palindrome[string_length-1]=='?'||palindrome[string_length-1]=='!'
||palindrome[string_length-1]=='.'){
/*If the last character is a ?,!or . continue with normal operation*/
j=string_length-1;
i = 0;
// this part is unnecessary since fgets() makes sure you dont
// accept more than 80 chars from the user.
if(j>80){
/*finds out if characters or more than 80*/
printf("Error your characters cannot be more than 80");
}
////////////////////
// create a seperate function named palindrome rather than
// doing all the things in main ( ) itself.
while((i <= j && ispalindrome && isalpha(i) && isalpha(j) )){
if(tolower (palindrome[i]) != tolower(palindrome[j])) {
ispalindrome = 0;
}
i++;
j--;
}
if(ispalindrome) {
printf("%s is a palindrome!\n", palindrome);
}
else {
printf("sorry, %s is not a palindrome\n", palindrome);
}
}
else
printf("Your input must be terminated by a punctuation mark");
}
}while(j>80);
return 0;
}Also do read the link I had posted previously because it contains each and every step on how to preform the palindrome check.
Make updates and repost.
I don't accept change. I don't deserve to live.
Happiness corrupts people.
Failing to value the lives of others cheapens your own.
Happiness corrupts people.
Failing to value the lives of others cheapens your own.
•
•
Join Date: Nov 2004
Posts: 123
Reputation:
Rep Power: 0
Solved Threads: 0
I saw your check but I like the way I did mine without functions so I'm trying to integrate the last character check here it is without the last character check I saw the method somewhere else
char string[80];
int ispalindrome = 1;
int i,j;
/* get the user input */
printf("Enter a string no more than 80 characters ");
gets(string);
j = strlen(string) - 1;
i = 0;
while((i <= j && ispalindrome && isalpha(i) && isalpha(j) )){
if(tolower (string[i]) != tolower(string[j])) {
ispalindrome = 0;
}
i++;
j--;
}
if(ispalindrome) {
printf("%s is a palindrome!\n", string);
}
else {
printf(" %s is not a palindrome\n", string);
}
} Hmm.. it is always a good practice to divide teh work to be done by the program into functions... something you will have to acccept as you move on ahead in your course.. but still if you dont want then...
Anyways, there are a few minor problems with your code.
1. Dont use gets ( ) . It allows the user to enter as many characters as he wants, thereby writing on the memory which is not of the users ( out of bounds array exception ) as well as leaving the input stream dirty.
Better use fgets( ) the way i mentioned, which is a more robust fuction.
2.
The problem with this code being that if it encounters a non character alphabet, it exits the loop to declare the result instead of ignoring the character. Pull the validation code i.e. the isalpha( ) part inside the while loop.
3. You are performign some redundant calculations in this part of the code.
[/code]
If the condition is not satisfied, there is no way the string is going to be palindrome, so why continue with the remaining calculations. Break teh loop prematurely is this condition is voilated.
Make the changes and repost.
Anyways, there are a few minor problems with your code.
1. Dont use gets ( ) . It allows the user to enter as many characters as he wants, thereby writing on the memory which is not of the users ( out of bounds array exception ) as well as leaving the input stream dirty.
Better use fgets( ) the way i mentioned, which is a more robust fuction.
2.
while((i <= j && ispalindrome && isalpha(i) && isalpha(j) ))
3. You are performign some redundant calculations in this part of the code.
if(tolower (string[i]) != tolower(string[j]))
{
ispalindrome = 0;
}If the condition is not satisfied, there is no way the string is going to be palindrome, so why continue with the remaining calculations. Break teh loop prematurely is this condition is voilated.
Make the changes and repost.
I don't accept change. I don't deserve to live.
Happiness corrupts people.
Failing to value the lives of others cheapens your own.
Happiness corrupts people.
Failing to value the lives of others cheapens your own.
•
•
Join Date: Nov 2004
Posts: 123
Reputation:
Rep Power: 0
Solved Threads: 0
I am getting comflicting information right now but i think i'm going to leave out the termination I just don't think it is necessary if it is phrases like
Ah! satan sees natasha will be invalid and that just isn't sensible that can't be what he means I think the method I used satisfies my assumptions except for the comma not being used as a terminating mark. Do you see what I mean
Ah! satan sees natasha will be invalid and that just isn't sensible that can't be what he means I think the method I used satisfies my assumptions except for the comma not being used as a terminating mark. Do you see what I mean
...which is ?
Dont decide things your way, the assignment states that "all " palindromes should be verified.
No he means exactly that thing. You are assuming wrong things.
I see that you are trying to give up on this .
Post your problems.
•
•
•
•
but i think i'm going to leave out the termination I just don't think it is necessary if it is phrases like Ah! satan sees natasha will be invalid
•
•
•
•
and that just isn't sensible that can't be what he means I think
•
•
•
•
Do you see what I mean
I see that you are trying to give up on this .
Post your problems.
I don't accept change. I don't deserve to live.
Happiness corrupts people.
Failing to value the lives of others cheapens your own.
Happiness corrupts people.
Failing to value the lives of others cheapens your own.
•
•
Join Date: Nov 2004
Posts: 123
Reputation:
Rep Power: 0
Solved Threads: 0
:rolleyes: ok its amazingly scary how u saw through my guise...Its just getting too complicated, i haven't even attempted the histogram thingy yet and a friend of mine told me I was thinking too hard so I tried to cop out. But I'll try...
how do I extract the isalpha() do I use another loop directly after
and where to i make sure the two letters being compared are not case sensitive if not there?
while((i <= j && ispalindrome && isalpha(i) && isalpha(j) ))
how do I extract the isalpha() do I use another loop directly after
while((i <= j && ispalindrome )) if((isalpha(i) && isalpha(j))
and where to i make sure the two letters being compared are not case sensitive if not there?
Last edited by boujibabe : Nov 5th, 2006 at 4:21 pm.
•
•
•
•
:rolleyes: ok its amazingly scary how u saw through my guise...Its just getting too complicated,

while((i <= j && ispalindrome ))
{
while ( ! isalpha( my_string[i] ) ) // you had forgotten to write the string name
{
++ i ; // move ahead
}
while( ! isalpha( my_string[j] ) )
{
--j ;
}
// perform check here and exit if not same
}
Incorporate this somethign like this in your code and repost.
PS: Its almost 3.30 AM here so now I would be signing off. would solve your queries tomo. Bye.
Last edited by ~s.o.s~ : Nov 5th, 2006 at 5:42 pm.
I don't accept change. I don't deserve to live.
Happiness corrupts people.
Failing to value the lives of others cheapens your own.
Happiness corrupts people.
Failing to value the lives of others cheapens your own.
•
•
Join Date: Nov 2004
Posts: 123
Reputation:
Rep Power: 0
Solved Threads: 0
Ok, can I ask where I would include a provision for the comma?
/* function definition*/
void remove_newline( char* input ){
char* p = 0 ;
if( p = strrchr( input, '\n' ) )
*p = '\0' ;
int main(void)
{
char my_string[80];
int ispalindrome=1;/*boolean value True*/
int i=0;
int j=0;
void remove_newline( char* input);
/* get the user input */
do{
printf("Enter a string no more than 80 characters ");
fgets(palindrome,80,stdin);
j = strlen(palindrome) - 1;
i = 0;
/*remove trailing line*/
void remove_newline( char* my_string)
int string_length=strlen(my_string);
if(my_string[string_length-1]=='?'||my_string[string_length-1]=='!'
||my_string[string_length-1]=='.'){
/*If the last character is a ?,!or . continue with normal operation*/
j=string_length-1;
i = 0;
if(j>80){
/*finds out if characters are more than 80*/
printf("Error your characters cannot be more than 80");
}
while((i <= j && ispalindrome )){
while ( ! isalpha( my_string[i] ) )
{
++ i ;
}
while( ! isalpha( my_string[j] ) )
{
--j ;
}
if(tolower (my_string[i]) != tolower(my_string[j])) {
ispalindrome = 0;
}
i++;
j--;
}
if(ispalindrome) {
printf("%s is a palindrome!\n", palindrome);
}
else {
printf("sorry, %s is not a palindrome\n", palindrome);
}
}
else
printf("Your input must be terminated by a punctuation mark");
}while(j>80);
return 0;
} OK, it's time to back up and (sort of) start over. I haven't gone thru all your code, but you seem to be getting lost trying to solve 5 things at once. You never get anywhere that way.
Keep what you have because you can probably use most of it. Let's just rearrange stuff and do one step at a time.
#1:
Start with a new project.
Write the input portion of the code.
Input a string.
Output the string
When the string input and output are correct, move on, but not before!
#2:
Add the test whether the last character of the string is correct.
If not, do what you think you need to do.
Look at what you already have and use what you can.
When you can test the sentence correctly, move on, but not until!
#3:
Copy the input into another string character by character.
Copy only the characters you want to test as a palindrome.
Do not copy characters that aren't palindromish (space, comma, etc)
If you use string you should be OK. If you used char be sure to add that ending '\0'
Print out what you copied. When this looks correct, move on, but not before!
#4:
Add the ability during the copy to convert all uppercase characters to lower case.
Print out what you copied. When this looks correct, move on, but not until!
#5:
Now test for the palindrome.
Keep what you have because you can probably use most of it. Let's just rearrange stuff and do one step at a time.
#1:
Start with a new project.
Write the input portion of the code.
Input a string.
Output the string
When the string input and output are correct, move on, but not before!
#2:
Add the test whether the last character of the string is correct.
If not, do what you think you need to do.
Look at what you already have and use what you can.
When you can test the sentence correctly, move on, but not until!
#3:
Copy the input into another string character by character.
Copy only the characters you want to test as a palindrome.
Do not copy characters that aren't palindromish (space, comma, etc)
If you use string you should be OK. If you used char be sure to add that ending '\0'
Print out what you copied. When this looks correct, move on, but not before!
#4:
Add the ability during the copy to convert all uppercase characters to lower case.
Print out what you copied. When this looks correct, move on, but not until!
#5:
Now test for the palindrome.
Got a cough? Go home tonight and eat a whole box of Ex-Lax. Tomorrow, you'll be afraid to cough.
-- Pearl Williams
-- Pearl Williams
![]() |
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)






Linear Mode