I have a palindrome program.Is there a way to ensure user input terminated in a string by a punctuation mark (e.g. ‘!’, ‘.’, or ‘?’.) in an array? And how do i get the program to exclude commas?

eg A man, a plan, a canal, Panama! bosters a problem because when read backwards the commas are placed incorrectly whereas
A man , a plan , a cana l, Panama! would read correctly how do i make it work either way?

ok but how can I ensure that that user input is terminated using punctuation marks such as ? or !

Hmm.. maybe something like:

  • Accpet the input from the user using fgets()
  • Remove the trailing newline at the end of the string accepted from the user. (must do, check my function remove_newline())
  • Check if the last character of the string is ! or ? by using something like:
// accept input
// remove trailing newline and replace with null character '\0'
int string_length = strlen( my_string ) ;
if( my_string[string_length - 1] == '?' || 
                          my_string[string_length - 1] == '!' )
{
    // continue with the normal functionning
}
else
{
    // duck out of program, using return 1 or exit(1) or again 
   // ask for user input
}

So how ultimately what is the best way to remove or ignore white spaces in the array?

Palindromes are basically related to alphabets, so to ignore whitespaces in general you can traverse the whole character array, processing characters only when the isalpha( my_character ) condition is satisfied, otherwise skip that character.

Something like:

while( ! isalpha( character ) )
{
    // skip the character and move on
}

ok thaks, wait is this the method used in the earlier palindrone program?

Dumb question: I don't see the isalpha() being utilized in the final solution yet the program ignores spaces. Are you sure thats what he used?

Dumb question: I don't see the isalpha() being utilized in the final solution yet the program ignores spaces. Are you sure thats what he used?

Not a dumb question. And no, he didn't use it. But you might as well because it's easier.

heres something like it but I'm trying to integrate the punctuation mark that terminates the user input (but is not included in the assumption that the phrase is a palindrome)suggested above, but looking at it now i'm thinking the condition is better suited to while

#include <stdio.h>
#include <ctype.h>
#include <string.h>


int main(void)
{
	char palindrome[80];
	int ispalindrome=1;/*boolean value True*/
	int i=0;
	int j=0;
	void remove_newline( char* input );


 /* get the user input */
  printf("Enter a word or sentence that is no more than 80 characters ");

  fgets(palindrome,80,stdin);

	   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]=='.'){
   
	j=string_length-1;
	i = 0;

    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);/* here i am supposed to print a histogram with number frequency but 'll ask later*/  }
  else {
    printf("sorry, %s is not a palindrome\n", palindrome);
  }
 } 
 else{
printf("Enter a word or sentence that is no more than 80 characters ");

  fgets(palindrome,80,stdin);
 }
 
  return 0;
}

Ok let me ask your opinon I am to assume

1.The user input is terminated by a punctuation mark eg '!', '?','.'
from the way its worded i am to assumed that if the palindrome is not terminated by a punctuation mark it won't be a evaluated.Like if deed! would be a palindrome and deed would not be? I'm thinking that this assignment is poorly worded because that makes no sense a word does not need a punctuation mark.
2.The terminating punctuation mark is not used in determining if the user input is a palindrome
3. a comma cannot be used to terminate user input

Just one question before we move ahead... why do you need to terminate the string with a ! .

deed is a palindrome but deed! is not ( reverse them and you will know).
Also the algo which i described will not work for things like:

!!deed!

since that algo ignores anything other than alphabets, hence both:

!deed!!! and deed and deed!! will be considered as palindromes.

Exactly my assumptions state that the user input is terminated by punctution marks but then it states that the terminating puncuation mark is not used in detmining if the user input is apalindrome. Isn't that ambiguous?

Okay try this: ( according to the requirements )

1. Accept the input from the user.
2. Check if the last character is a !
3. If it is continue with the normal palindrome processing as I have pointed out in the link before.
4. If the last char is not ! print out the message that the input is not in the specified format and then either exit the program or ask the user to reenter the string.

I tried this before above in my messy attempt but do you think that this is necessary I mean is that what the assumptims mean? No processing unless string is terminated by proper punctuation?

A string is terminatd by proper punctuations probably it is terminated using a full stop or should be somthing like:

Ah! Satan sees Natasha.

Can you write down here the actual requirements given to you, coz this is very confusing for me. Just copy and paste the requirements given to you for me to read it.

A palindrome is a word, phrase, verse, or sentence that reads the same backward or forward. For example: A man, a plan, a canal, Panama! You are required to develop a complete programming solution to determine whether the user-input is a palindrome. If this is the case, then a histogram (see Figure 1) showing the distribution of each character in the input stream is to be displayed on the screen. Only characters within the input stream should be on the vertical axis of the histogram, and each character occurrence should be represented as a special character (e.g. ‘♪’). If the user-input is not a palindrome then the user should be presented with a suitable alert message.

YOU ARE TO ASSUME:
1. The user input is no more than 80 characters long.
2. The user input may include spaces, underscores etc….
3. The user input is terminated by a punctuation mark (e.g. ‘!’, ‘.’, or ‘?’.)
4. A comma cannot be used to terminate the user-input.
5. The terminating punctuation mark is not used in determining if the user input is a palindrome.
6. The solution is case insensitive (i.e. ‘a’ and ‘A’ are treated as being the same).

Okay the algorithm I pointed to you applies in this case, you just need to keep in mind that the last char can be a ".", "?" or a "!" but cant be a ",".

so what if I leave out puntuation what haapens then will deed still be a palindrone?

Okay it seems like you are confused between the requirement for the string to end in puctuation marks with teh concept of palindrome.

Palindrome doest care about the character case ( be it upper case or lower case ), it ignores whitespaces ( tabs, newlines, etc. ) and also ignores special characters and puctuations. It only and only cares about characters.

Terminating the string with punctutations is just a requirement of hte program, dont confuse it with palindromes.

So based on the explanatino above I ask you to tell me whether the strings given below are palindromes so that i can know you have grasped the concepts well.

De e , d
DEed
!!Deed !!!!
Deed.
Deed ?
?deed?
__de__ed__?

Give me the answers ?

Bravo !!

Now that you have grasped the concept of what exactly a palindrome is , you can start coding it and then post any problems you encounter.
And please indent your code properly, so that finding errors and giving suggestions becomes simpler.

It looks a fright and I can't figure out where I went wrong can you understand it?

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);
	j = strlen(palindrome) - 1;
	i = 0;
 /* 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;

	if(j>80){
 /*finds out if characters or more than 80*/
	printf("Error your characters cannot be more than 80");
	}

	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;
}
This article has been dead for over six months. Start a new discussion instead.