LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 08-11-2005, 03:56 AM   #1
ewt3y
Member
 
Registered: May 2005
Location: hanoi vietnam
Distribution: mandriva
Posts: 106

Rep: Reputation: 15
please optimize this C function


i, I launched myself into writing a function used to remove symbols within a string. It is not very robust, can you improve ?

char *RmSymbols ( const char *str ) {
char *ptr = str;
while (*ptr != '\0') {
if (*ptr >= 65 && *ptr <=90) {
*RmSymbols++ = *ptr;
}
ptr++;

}
}
 
Old 08-11-2005, 04:33 AM   #2
theYinYeti
Senior Member
 
Registered: Jul 2004
Location: France
Distribution: Arch Linux
Posts: 1,897

Rep: Reputation: 61
Is that homework, or what? That's two strange posts I've seen from you...

Yves.
 
Old 08-11-2005, 10:36 AM   #3
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 58
Not very robust? Optomize? If it doesn't even work I'd say optomization should be the last thing you should be worrying about. You're trying to dereference and increment a function. You're going to need to create another buffer.
 
Old 08-11-2005, 12:24 PM   #4
lowpro2k3
Member
 
Registered: Oct 2003
Location: Canada
Distribution: Slackware
Posts: 340

Rep: Reputation: 30
Please use [ code ] tags. Quote me if you dont know how. You might also want to try using a COMPILER which will tell you this function doesn't work unless RmSymbols is some sort of global variable, and if thats the case why are you returning it. This isn't bash, you need to declare your variables before you use them.

Code:
char *RmSymbols ( const char *str ) {
  char *ptr = str;
  while (*ptr != '\0') {
    if (*ptr >= 65 && *ptr <=90)
      *RmSymbols++ = *ptr; /* what are you doing??? */
    ptr++;
  }
}
 
Old 08-11-2005, 02:27 PM   #5
vladmihaisima
Member
 
Registered: Oct 2002
Location: Delft, Netherlands
Distribution: Gentoo
Posts: 196

Rep: Reputation: 33
For sure seems like a homework :-p.

Anyway I vaguely rember that in some language (Pascal?) the return value of a function was a variable with the function name.

In C this is not the case. You must declare the variable (and allocate if it is a pointer) which you want to return and return it explicitly.

But anyway I think you should read first a C tutorial...
 
Old 08-11-2005, 02:34 PM   #6
orgcandman
Member
 
Registered: May 2002
Location: dracut MA
Distribution: Ubuntu; PNE-LE; LFS (no book)
Posts: 594

Rep: Reputation: 102Reputation: 102
Code:
char *RmSymbols( const char *str)
{
    static char cRmSymBuf[8192] = {0};
    char *pStrIt = str;
    int iPos = 0;
    
    while((*pStrIt != 0) && (iPos < 8192))
    {
        if(*pStrIt >= 'A' && *pStrIt <= 'Z')
        {
           cRmSymBuf[iPos++] = *pStrIt;
        }
        pStrIt++;
    }

    return cRmSymBuf;
}
would be a better example of something for people to optimize.

Personally, I'd make it look like this:

Code:
int fRemoveSyms(char *pSource, char *pDest)
{
    while((*pSource++ = *pDest) != 0)
    {
        if((*pDest <= 'Z') && (*pDest >= 'A'))
        {
            pDest++;
        }
    }
    return strlen(pDest);
}
ymmv (And note, I haven't tested either of these functions, but I'm pretty sure they work).
 
Old 08-11-2005, 03:10 PM   #7
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 58
Aren't you copying the wrong way here (dest to source, instead of source to dest)?
Code:
while((*pSource++ = *pDest) != 0)
Also, the call to strlen() can be eliminated (since it's slow) so I would write it like this:
Code:
int fRemoveSyms(char *pSource, char *pDest)
{
    char *d = pDest;

    while((*d = *pSource++))
    {
        if((*d <= 'Z') && (*d >= 'A'))
        {
            d++;
        }
    }
    return d - pDest;
}

Last edited by itsme86; 08-11-2005 at 03:13 PM.
 
Old 08-11-2005, 03:25 PM   #8
orgcandman
Member
 
Registered: May 2002
Location: dracut MA
Distribution: Ubuntu; PNE-LE; LFS (no book)
Posts: 594

Rep: Reputation: 102Reputation: 102
You got me. Sorry...I had been eating cake, so I wasn't looking for program corectness...more of a general idea. Plus, leaving bugs in would help him/her with debugging anyway.

Good catch w.r.t. strlen(), also. I should probably spend less time writing code while eating cake, I think
 
Old 08-12-2005, 01:32 AM   #9
JanusPaul
Member
 
Registered: Nov 2004
Location: Houston, Texas
Distribution: Ubuntu, Debian, Solaris, Free BSD
Posts: 82

Rep: Reputation: 15
char *RmSymbols ( const char *str ) {
char *ptr = str;
while (*ptr != '\0') {
if (*ptr >= 65 && *ptr <=90) {
*RmSymbols++ = *ptr;
}
ptr++;
}
}

I suggest ditching 'str' since you really aren't using it in the first place. 'str' isn't byref so altering it wouldn't be a bad thing persay here... You can reduce the size of the code with increasing/reducing number figures around >= and <= to do the exact same thing only with > and <! Using the function name *( RmSymbols++ ), as a variable itself, is very indicative this guy is a Visual Basic user. As this is one feature to the language I would imagine is Microsoft's attempt at making people more dependent on their products.

char *RmSymbols ( char *ptr )
{
while ( *ptr )
{
if ( *ptr > 64 && *ptr < 91 )
*( RmSymbols++ ) = *ptr;
ptr++;
}
}
 
Old 08-12-2005, 04:25 AM   #10
ewt3y
Member
 
Registered: May 2005
Location: hanoi vietnam
Distribution: mandriva
Posts: 106

Original Poster
Rep: Reputation: 15
My heartfelt thanks to you all !

Hitler's follower.
 
Old 08-12-2005, 05:48 AM   #11
theYinYeti
Senior Member
 
Registered: Jul 2004
Location: France
Distribution: Arch Linux
Posts: 1,897

Rep: Reputation: 61
Quote:
Originally posted by ewt3y
My heartfelt thanks to you all !

Hitler's follower.
I'll consider you don't grasp the meaning of those words!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
can I optimize this kushalkoolwal Programming 5 10-21-2005 09:05 PM
How to optimize the partitions of a HD dabenavidesd Linux - General 1 09-02-2005 11:40 PM
How can I optimize VNC Wynand1 Linux - Networking 4 05-17-2005 01:05 AM
optimize PHP string function Cedrik Programming 6 08-29-2004 09:07 AM
Optimize/Prefix rogk Linux - Software 1 07-25-2004 05:04 AM


All times are GMT -5. The time now is 03:57 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration