Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Quick Question

Posted on 2004-10-21
6
Medium Priority
?
269 Views
Last Modified: 2010-04-01
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
0
Comment
Question by:Ant_Dogg
6 Comments
 
LVL 4

Expert Comment

by:nagraves
ID: 12377096
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
 
LVL 4

Expert Comment

by:nagraves
ID: 12377103
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
 
LVL 15

Expert Comment

by:efn
ID: 12377440
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 5

Accepted Solution

by:
dennis_george earned 200 total points
ID: 12377449
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
 
LVL 20

Expert Comment

by:ikework
ID: 12380801
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
 
LVL 5

Expert Comment

by:dennis_george
ID: 12396751
you are right ikework.... It should be passed as reference for efficiency.....

Dennis
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses

916 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question