User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C section within the Software Development category of DaniWeb, a massive community of 427,676 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 4,272 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C advertiser: Programming Forums
Views: 5357 | Replies: 63
Reply
Join Date: Nov 2004
Posts: 123
Reputation: boujibabe is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
boujibabe boujibabe is offline Offline
Junior Poster

Terminating a String

  #1  
Oct 31st, 2006
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?
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Jun 2006
Location: India
Posts: 6,863
Reputation: ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold 
Rep Power: 23
Solved Threads: 344
Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Lazy, Useless & Apathetic

Re: Terminating a String

  #2  
Oct 31st, 2006
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
I don't accept change. I don't deserve to live.

Happiness corrupts people.

Failing to value the lives of others cheapens your own.
Reply With Quote  
Join Date: Nov 2004
Posts: 123
Reputation: boujibabe is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
boujibabe boujibabe is offline Offline
Junior Poster

Re: Terminating a String

  #3  
Nov 1st, 2006
ok but how can I ensure that that user input is terminated using punctuation marks such as ? or !
Reply With Quote  
Join Date: Jun 2006
Location: India
Posts: 6,863
Reputation: ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold 
Rep Power: 23
Solved Threads: 344
Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Lazy, Useless & Apathetic

Re: Terminating a String

  #4  
Nov 1st, 2006
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 10:44 am.
I don't accept change. I don't deserve to live.

Happiness corrupts people.

Failing to value the lives of others cheapens your own.
Reply With Quote  
Join Date: Nov 2004
Posts: 123
Reputation: boujibabe is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
boujibabe boujibabe is offline Offline
Junior Poster

Re: Terminating a String

  #5  
Nov 4th, 2006
So how ultimately what is the best way to remove or ignore white spaces in the array?
Reply With Quote  
Join Date: Jun 2006
Location: India
Posts: 6,863
Reputation: ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold 
Rep Power: 23
Solved Threads: 344
Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Lazy, Useless & Apathetic

Re: Terminating a String

  #6  
Nov 4th, 2006
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
}
I don't accept change. I don't deserve to live.

Happiness corrupts people.

Failing to value the lives of others cheapens your own.
Reply With Quote  
Join Date: Nov 2004
Posts: 123
Reputation: boujibabe is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
boujibabe boujibabe is offline Offline
Junior Poster

Re: Terminating a String

  #7  
Nov 4th, 2006
ok thaks, wait is this the method used in the earlier palindrone program?
Reply With Quote  
Join Date: Jun 2006
Location: India
Posts: 6,863
Reputation: ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold 
Rep Power: 23
Solved Threads: 344
Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Lazy, Useless & Apathetic

Re: Terminating a String

  #8  
Nov 4th, 2006
Huh , which earlier program ?
I don't accept change. I don't deserve to live.

Happiness corrupts people.

Failing to value the lives of others cheapens your own.
Reply With Quote  
Join Date: Nov 2004
Posts: 123
Reputation: boujibabe is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
boujibabe boujibabe is offline Offline
Junior Poster

Re: Terminating a String

  #9  
Nov 4th, 2006
Umm...The one in the link you provided here:

http://www.daniweb.com/techtalkforums/thread59789.html
Reply With Quote  
Join Date: Jun 2006
Location: India
Posts: 6,863
Reputation: ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold 
Rep Power: 23
Solved Threads: 344
Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Lazy, Useless & Apathetic

Re: Terminating a String

  #10  
Nov 4th, 2006
Yes its the same method.

For a well detailed and excellent algorithm see HERE.
I don't accept change. I don't deserve to live.

Happiness corrupts people.

Failing to value the lives of others cheapens your own.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Other Threads in the C Forum

All times are GMT -4. The time now is 11:13 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC