Quick Question

Something is going wrong with my function to remove punctuation from a string.  Here is the code:

string removePunct(string Str)
{
  int l = Str.length();
 
  //isalnum(x) returns true if x is a character or number
  if (!isalnum(Str[0]))
  {
    int f = 1;
  }
  if (!isalnum(Str[l]))
  {
    int b = l-1;
  }

  //return substring without punctuation (if any)  

  return Str.substr(f, b);
}

It seems to not like the substr( ) function.  Any ideas?

Thanks
Ant_DoggAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

nagravesCommented:
couple things i'm wondering:

if (!isalnum(Str[l]))

if l is the length of the string, Str[l] would be out of bounds wouldn't it? What it looks like to me is, being that you're only doing two comparisons, you're checking if the first char and last char of a string are neither letter nor number.  if this is the case, try Str[l-1]  for that second call.

As far as substr goes, you are using it correctly.
SomeString.substr(i, n) extracts a substring of length n  starting at position i.
0
nagravesCommented:
If you want to remove ALL punctuation from the string, you're going to have to either do it with a loop, or with recursion. Either or, the way you're doing it now will only check the first element of the string, and the memory location immediately after that of the last element of the string.
0
efnCommented:
As nagraves pointed out, your function doesn't have an algorithm that will do what you want.

The problem with the substr call is probably that b and f are not defined at that point.  They are local to the blocks in which they are declared, and undefined outside those blocks.  You could declare them outside those blocks, in the same scope as the substr call, to fix this.
0
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

dennis_georgeCommented:
Hi,

I think missed many things here... Most important the logic......

string removePunct(string Str)
{
  int l = Str.length();
 
  //isalnum(x) returns true if x is a character or number
  if (!isalnum(Str[0]))
  {
    int f = 1;  // scope of 'f' is inside this curly bracket
  }
  if (!isalnum(Str[l]))  // out of bound array access
  {
    int b = l-1; //scope of 'b' is inside this curly bracket
  }

  //return substring without punctuation (if any)  

  return Str.substr(f, b);
}

I think you have to run a loop.... and check for non alpha chars.... I modified your code

The vector will contain all the substring...

int removePunct(string Str, vector<string> &refResult)
{
         int iSize = str.length() ;
        int iStart = 0;
        int i = 0;

        for(i = 0; i < iSize; i++) {
                if(!isalpha(str[i]) && i != iStart) {
                        string tmp = str.substr(iStart, i - iStart) ;
                        refStr.push_back(tmp) ;
                        iStart = i + 1 ;
                }
        }

        string tmp = str.substr(iStart, i - iStart) ;
        refStr.push_back(tmp) ;

        return 0 ;
}

Dennis
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ikeworkCommented:
hey dennis, you're right, but first parameter (Str) is better passed by reference,
than by value like this:

int removePunct( const string &Str, vector<string> &refResult) ...

otherwise each time the function is called, the entire string (Str) will be copied,
BIG overhead

ike
0
dennis_georgeCommented:
you are right ikework.... It should be passed as reference for efficiency.....

Dennis
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.