Removing punctuation from a string

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Sep 2007
Posts: 26
Reputation: LanierWexford is an unknown quantity at this point 
Solved Threads: 0
LanierWexford LanierWexford is offline Offline
Light Poster

Removing punctuation from a string

 
0
  #1
Dec 4th, 2007
Is there a function to remove punctuation from a string, or check to see if a char is a letter?

This is a home work assignment to:

1 Write a class stack and a class queue
2 Test your classes by writing a program that will ask the user for a string and then check to see if the string is a palindrome by loading into a stack and queue and comparing the top of the stack to the front of the queue.

I have done part 1, but I can not see how to get around the punctuation for part 2.

Thanx in advance

Lanier
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 2,462
Reputation: zandiago is on a distinguished road 
Solved Threads: 25
Featured Poster
zandiago's Avatar
zandiago zandiago is offline Offline
Nearly a Posting Maven

Re: Removing punctuation from a string

 
0
  #2
Dec 4th, 2007
Good day and welcome aboard. With regards to checking to see if a char is a letter, we use the isalpha() function. It's also good to post your code here, so that we can look it over. More information can be found here:
http://www.cprogramming.com/fod/isalpha.html
With regards to removing a punctuation from a string, you'll perhaps have to decalre the punctuations as an array of characters. Hope this helps!
I shot the sheriff....but I didn't shoot the deputy
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 26
Reputation: LanierWexford is an unknown quantity at this point 
Solved Threads: 0
LanierWexford LanierWexford is offline Offline
Light Poster

Re: Removing punctuation from a string

 
0
  #3
Dec 4th, 2007
After a little more digging I found what I was looking for, which is the C functions ispunct() and isalpha(). But, if anyone has another idea they want to throw at me let's have it! Alway's good to know multiple ways to the same answer.
Last edited by LanierWexford; Dec 4th, 2007 at 10:14 am. Reason: Spelling
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,575
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1485
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Removing punctuation from a string

 
0
  #4
Dec 4th, 2007
To remove punctuation you will normally have to rewrite the entire string to avoid the many problems with modifying string literals passed to the function. Just iterate through the string one character at a time and copy the character to the new string only if it is not a punctuation character.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 26
Reputation: LanierWexford is an unknown quantity at this point 
Solved Threads: 0
LanierWexford LanierWexford is offline Offline
Light Poster

Re: Removing punctuation from a string

 
0
  #5
Dec 6th, 2007
Just wanted to say thanx for the help. Program is running great!


lanier
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 2,462
Reputation: zandiago is on a distinguished road 
Solved Threads: 25
Featured Poster
zandiago's Avatar
zandiago zandiago is offline Offline
Nearly a Posting Maven

Re: Removing punctuation from a string

 
0
  #6
Dec 7th, 2007
Could you please your code for the benefit of the entire daniweb community! Glad your program is working!
I shot the sheriff....but I didn't shoot the deputy
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 26
Reputation: LanierWexford is an unknown quantity at this point 
Solved Threads: 0
LanierWexford LanierWexford is offline Offline
Light Poster

Re: Removing punctuation from a string

 
0
  #7
Dec 7th, 2007
Here is the code as requested. I rewrote it using the STL queue and stack because I felt my class was too bulky.

Lanier


  1. #include <iostream>
  2. #include <string>
  3. #include <stack>
  4. #include <queue>
  5. #include <ctype.h>
  6.  
  7. using namespace std;
  8.  
  9. int main()
  10. {
  11.  
  12. stack<char> SPal;
  13. queue<char> QPal;
  14. string Stuff;
  15. string Again="Yes";
  16.  
  17. while(Again=="Yes")
  18. {
  19.  
  20. cout<<"Please enter a string to be checked"<<endl<<endl;
  21. getline(cin,Stuff);
  22.  
  23. while(!Stuff.empty())
  24. {
  25. if(isalpha(Stuff.at(0)))
  26. {
  27. SPal.push(tolower(Stuff.at(0)));
  28. QPal.push(tolower(Stuff.at(0)));
  29. Stuff.erase(0,1);
  30. }
  31.  
  32. else if(!isalpha(Stuff.at(0)))
  33. {
  34. Stuff.erase(0,1);
  35. }
  36. }
  37.  
  38. while(!SPal.empty()&&!QPal.empty())
  39. {
  40. if(SPal.top()==QPal.front())
  41. {
  42. SPal.pop();
  43. QPal.pop();
  44. }
  45. }
  46.  
  47.  
  48.  
  49. if(SPal.empty()&&QPal.empty())
  50. {
  51. cout<<endl<<"The string is a palindrome.";
  52. }
  53. else if(!SPal.empty()||!QPal.empty())
  54. {
  55. cout<<endl<<"The string is NOT a palindorme.";
  56. }
  57.  
  58. cout<<endl<<"Another ";
  59. cin>>Again;
  60. }
  61.  
  62. cout<<endl<<endl;
  63. system("pause");
  64. return 0;
  65. }
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC