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

Recommended Answers

All 6 Replies

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!

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.

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.

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


lanier

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

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;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.