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

Removing punctuation from a string

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

LanierWexford
Light Poster
27 posts since Sep 2007
Reputation Points: 10
Solved Threads: 0
 

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!

zandiago
Nearly a Posting Maven
2,480 posts since Jun 2007
Reputation Points: 129
Solved Threads: 26
 

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.

LanierWexford
Light Poster
27 posts since Sep 2007
Reputation Points: 10
Solved Threads: 0
 

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.

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

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


lanier

LanierWexford
Light Poster
27 posts since Sep 2007
Reputation Points: 10
Solved Threads: 0
 

Could you please your code for the benefit of the entire daniweb community! Glad your program is working!

zandiago
Nearly a Posting Maven
2,480 posts since Jun 2007
Reputation Points: 129
Solved Threads: 26
 

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

Lanier

#include <iostream>
#include <string>
#include <stack>
#include <queue>
#include <ctype.h>

using namespace std;

int main()
{

stack<char> SPal;
queue<char> QPal;
string Stuff;
string Again="Yes";

while(Again=="Yes")
{

   cout<<"Please enter a string to be checked"<<endl<<endl;
   getline(cin,Stuff);

   while(!Stuff.empty())
   {
      if(isalpha(Stuff.at(0)))
	  {
	     SPal.push(tolower(Stuff.at(0)));
		 QPal.push(tolower(Stuff.at(0)));
		 Stuff.erase(0,1);
	  }
	  
	  else if(!isalpha(Stuff.at(0)))
	  {
	     Stuff.erase(0,1);
	  }
   }

   while(!SPal.empty()&&!QPal.empty())
   {
      if(SPal.top()==QPal.front())
      {
              SPal.pop();
	          QPal.pop();
	  }
   }
   

   
   if(SPal.empty()&&QPal.empty())
   {
      cout<<endl<<"The string is a palindrome.";
   }
   else if(!SPal.empty()||!QPal.empty())
   {
      cout<<endl<<"The string is NOT a palindorme.";
   }  
	
   cout<<endl<<"Another  ";
   cin>>Again;
}

cout<<endl<<endl;
system("pause");
return 0;
}
LanierWexford
Light Poster
27 posts since Sep 2007
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You