944,054 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 7107
  • C++ RSS
Dec 4th, 2007
0

Removing punctuation from a string

Expand Post »
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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
LanierWexford is offline Offline
27 posts
since Sep 2007
Dec 4th, 2007
0

Re: Removing punctuation from a string

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!
Featured Poster
Reputation Points: 129
Solved Threads: 26
Nearly a Posting Maven
zandiago is offline Offline
2,463 posts
since Jun 2007
Dec 4th, 2007
0

Re: Removing punctuation from a string

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
Reputation Points: 10
Solved Threads: 0
Light Poster
LanierWexford is offline Offline
27 posts
since Sep 2007
Dec 4th, 2007
0

Re: Removing punctuation from a string

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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,954 posts
since Aug 2005
Dec 6th, 2007
0

Re: Removing punctuation from a string

Just wanted to say thanx for the help. Program is running great!


lanier
Reputation Points: 10
Solved Threads: 0
Light Poster
LanierWexford is offline Offline
27 posts
since Sep 2007
Dec 7th, 2007
0

Re: Removing punctuation from a string

Could you please your code for the benefit of the entire daniweb community! Glad your program is working!
Featured Poster
Reputation Points: 129
Solved Threads: 26
Nearly a Posting Maven
zandiago is offline Offline
2,463 posts
since Jun 2007
Dec 7th, 2007
0

Re: Removing punctuation from a string

Here is the code as requested. I rewrote it using the STL queue and stack because I felt my class was too bulky.

Lanier


C++ Syntax (Toggle Plain Text)
  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. }
Reputation Points: 10
Solved Threads: 0
Light Poster
LanierWexford is offline Offline
27 posts
since Sep 2007

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

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: Parsing question
Next Thread in C++ Forum Timeline: Which C++ to use?





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


Follow us on Twitter


© 2011 DaniWeb® LLC