Removing punctuation and spaces from a String in C++

There may be times in which you need to do character processing that only deals with the Alphanumeric characters of a string, not the punctuations or blank spaces. Here is a program that will remove the punctuations and spaces from a string. Download the EXE and Source Code

The Code


//Joseph McCullough
//Program: alphanumonly.cpp
//Description: Takes a string and creates another string that contains only the
//letters and numbers of the first string.
//Visit www.mcculloughdesigns.com/blog for more C++ Goods!

#include <iostream>
#include <string>
using namespace std;

string alphaNumOnly(string);

const string SENTINEL = "0";//When entered as strToConvert,
              //terminates program.

int main()
{
  string strToConvert;  //The string that will be converted to.
  string strConverted;  //strToConverted with only letters and numbers.

  cout << "***************************************************" << "\n"
     << "AlphaNumOnly.exe"                     << "\n"
     << "Removes blank spaces and punctuations from a string" << "\n"
     << "Provided By McCullough Designs"      << "\n"
     << "***************************************************";

  //Read in strToConvert
  cout << "\n\nEnter a String or enter 0 to exit: ";
  getline(cin, strToConvert);

  while (strToConvert != SENTINEL)
  {

    strConverted = alphaNumOnly(strToConvert);
    cout << "\nNew String: " << strConverted;

    //Read in strToConvert
    cout << "\n\nEnter a String or enter 0 to exit: ";
    getline(cin, strToConvert);

  }
  return 0;
}

/************ function alphaNumOnly *********************************\
Description: Used to isolate the alphanumeric characters of a string

Precondition: strToConvert is defined

Postcondition: Returns a string
/********************************************************************/

string alphaNumOnly(string strToConvert)
{
  string strConverted; //strToConvert with only alpha-numeric characters.

  for (unsigned int i=0; i<strToConvert.length();i++)
  {
    if (isalnum(strToConvert[i])) //If current character is alpha-numeric
     strConverted += strToConvert[i]; //Add the character to strConverted

  }

  return strConverted;

}
Download the EXE and Source Code

The Explanation

The main() component of this function is just a sentinel controlled loop. The alphaNumOnly function is what's important. We create a local string variable
string strConverted
To store all the letters and numbers from the original string. This string is returned at the end of the function. Let's examine the for loop
for (unsigned int i=0; i<strToConvert.length();i++)
  {
    if (isalnum(strToConvert[i])) //If current character is alpha-numeric
     strConverted += strToConvert[i]; //Add the character to strConverted

  }
We begin by setting a counter i to 0. We use unsigned int because string lengths will always be positive. We then tell the loop to keep going until we reach the end of the string being processed. The body of the for loop is where the magic happens. The logic is as follows: Instead of removing the spaces and punctuation from the string being processed, it would be much easier to create a new string that contains only the alphanumeric characters. Each time an alphanumeric character is found (if(isalnum(string) evaluates to true), it is appended to the end of strToConvert (as indicated by the += operator. A+=b is short for A= A+b) So after you do this for the entire string, strToConvert will appear to be the original string but with only alpha numeric characters. Download the EXE and Source Code March 20, 2010
About the Author:

Joseph is the lead developer of Vert Studios Follow Joseph on Twitter: @Joe_Query
Subscribe to the blog: RSS
Visit Joseph's site: joequery.me