954,190 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Terminating a String

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?

boujibabe
Junior Poster
123 posts since Nov 2004
Reputation Points: 10
Solved Threads: 0
 

Hmm.. a palindrome actually doesnt consider spaces, punctuation marks and character case (upper or lower).

Maybe for a more detailed explanation, you might want to look here:
http://www.daniweb.com/techtalkforums/thread59789.html

~s.o.s~
Failure as a human
Administrator
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 733
 

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

boujibabe
Junior Poster
123 posts since Nov 2004
Reputation Points: 10
Solved Threads: 0
 

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
}

~s.o.s~
Failure as a human
Administrator
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 733
 

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

boujibabe
Junior Poster
123 posts since Nov 2004
Reputation Points: 10
Solved Threads: 0
 

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
}
~s.o.s~
Failure as a human
Administrator
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 733
 

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

boujibabe
Junior Poster
123 posts since Nov 2004
Reputation Points: 10
Solved Threads: 0
 

Huh , which earlier program ?

~s.o.s~
Failure as a human
Administrator
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 733
 

Umm...The one in the link you provided here:

http://www.daniweb.com/techtalkforums/thread59789.html

boujibabe
Junior Poster
123 posts since Nov 2004
Reputation Points: 10
Solved Threads: 0
 

Yes its the same method.

For a well detailed and excellent algorithm see HERE.

~s.o.s~
Failure as a human
Administrator
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 733
 
For a well detailed and excellent algorithm see HERE.

Thanks,S.O.S. :o

WaltP
Posting Sage w/ dash of thyme
Moderator
10,492 posts since May 2006
Reputation Points: 3,348
Solved Threads: 943
 

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?

boujibabe
Junior Poster
123 posts since Nov 2004
Reputation Points: 10
Solved Threads: 0
 
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.

WaltP
Posting Sage w/ dash of thyme
Moderator
10,492 posts since May 2006
Reputation Points: 3,348
Solved Threads: 943
 

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;
}
boujibabe
Junior Poster
123 posts since Nov 2004
Reputation Points: 10
Solved Threads: 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

boujibabe
Junior Poster
123 posts since Nov 2004
Reputation Points: 10
Solved Threads: 0
 

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.

~s.o.s~
Failure as a human
Administrator
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 733
 

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?

boujibabe
Junior Poster
123 posts since Nov 2004
Reputation Points: 10
Solved Threads: 0
 

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.

~s.o.s~
Failure as a human
Administrator
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 733
 

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?

boujibabe
Junior Poster
123 posts since Nov 2004
Reputation Points: 10
Solved Threads: 0
 

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.

~s.o.s~
Failure as a human
Administrator
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 733
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You