Solved

Quick Question

Posted on 2004-10-21
218 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
Question by:Ant_Dogg
    6 Comments
     
    LVL 4

    Expert Comment

    by:nagraves
    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
    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
    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
     
    LVL 5

    Accepted Solution

    by:
    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
    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
    you are right ikework.... It should be passed as reference for efficiency.....

    Dennis
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone. Privacy Policy Terms of Use

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
    Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
    The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
    The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

    875 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

    Need Help in Real-Time?

    Connect with top rated Experts

    12 Experts available now in Live!

    Get 1:1 Help Now