944,082 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 7606
  • C RSS
You are currently viewing page 1 of this multi-page discussion thread
Oct 31st, 2006
0

Terminating a String

Expand Post »
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?
Reputation Points: 10
Solved Threads: 0
Junior Poster
boujibabe is offline Offline
123 posts
since Nov 2004
Oct 31st, 2006
0

Re: Terminating a String

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
Super Moderator
Featured Poster
Reputation Points: 3241
Solved Threads: 719
Failure as a human
~s.o.s~ is offline Offline
8,873 posts
since Jun 2006
Nov 1st, 2006
0

Re: Terminating a String

ok but how can I ensure that that user input is terminated using punctuation marks such as ? or !
Reputation Points: 10
Solved Threads: 0
Junior Poster
boujibabe is offline Offline
123 posts
since Nov 2004
Nov 1st, 2006
0

Re: Terminating a String

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:
  1. // accept input
  2. // remove trailing newline and replace with null character '\0'
  3. int string_length = strlen( my_string ) ;
  4. if( my_string[string_length - 1] == '?' ||
  5. my_string[string_length - 1] == '!' )
  6. {
  7. // continue with the normal functionning
  8. }
  9. else
  10. {
  11. // duck out of program, using return 1 or exit(1) or again
  12. // ask for user input
  13. }
Last edited by ~s.o.s~; Nov 1st, 2006 at 11:44 am.
Super Moderator
Featured Poster
Reputation Points: 3241
Solved Threads: 719
Failure as a human
~s.o.s~ is offline Offline
8,873 posts
since Jun 2006
Nov 4th, 2006
0

Re: Terminating a String

So how ultimately what is the best way to remove or ignore white spaces in the array?
Reputation Points: 10
Solved Threads: 0
Junior Poster
boujibabe is offline Offline
123 posts
since Nov 2004
Nov 4th, 2006
0

Re: Terminating a String

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:
  1. while( ! isalpha( character ) )
  2. {
  3. // skip the character and move on
  4. }
Super Moderator
Featured Poster
Reputation Points: 3241
Solved Threads: 719
Failure as a human
~s.o.s~ is offline Offline
8,873 posts
since Jun 2006
Nov 4th, 2006
0

Re: Terminating a String

ok thaks, wait is this the method used in the earlier palindrone program?
Reputation Points: 10
Solved Threads: 0
Junior Poster
boujibabe is offline Offline
123 posts
since Nov 2004
Nov 4th, 2006
0

Re: Terminating a String

Huh , which earlier program ?
Super Moderator
Featured Poster
Reputation Points: 3241
Solved Threads: 719
Failure as a human
~s.o.s~ is offline Offline
8,873 posts
since Jun 2006
Nov 4th, 2006
0

Re: Terminating a String

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

http://www.daniweb.com/techtalkforums/thread59789.html
Reputation Points: 10
Solved Threads: 0
Junior Poster
boujibabe is offline Offline
123 posts
since Nov 2004
Nov 4th, 2006
0

Re: Terminating a String

Yes its the same method.

For a well detailed and excellent algorithm see HERE.
Super Moderator
Featured Poster
Reputation Points: 3241
Solved Threads: 719
Failure as a human
~s.o.s~ is offline Offline
8,873 posts
since Jun 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: string-structure
Next Thread in C Forum Timeline: Arrays: Finding Smallest String





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC