LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 11-11-2008, 10:43 AM   #1
amit_pansuria
Member
 
Registered: Sep 2006
Posts: 73

Rep: Reputation: 15
how to check and remove leading zeroes from the buffer using c program


Helo ,
I m writing small module of c.on RHEL 4

I have one buffer (for e.g. buffer = "002"
now I want to check whethere buffer contains leading zeroes and if it contains
leading zeroes then I want to remove all leading zeroes
( i.e. if buffer = "002" then I want to make buffer = "2")

how do I do using c code

Regards,
Amit
 
Old 11-11-2008, 11:04 AM   #2
tronayne
Senior Member
 
Registered: Oct 2003
Location: Northeastern Michigan, where Carhartt is a Designer Label
Distribution: Slackware 32- & 64-bit Stable
Posts: 3,541

Rep: Reputation: 1063Reputation: 1063Reputation: 1063Reputation: 1063Reputation: 1063Reputation: 1063Reputation: 1063Reputation: 1063
Easy way? Try using strtol() and sprintf(), something like this
Code:
int     value;

/*     get what's in buf as a number     */
value = (int) strtol (buf, (char **) NULL, 10);
/*     copy it back     */
(void) sprintf (buf, "%d", value);
You can also do that all in one whack; replace "value" in the sprintf with the strtol.

You can also use the atoi() function (the strtol() function is more complicated):
Code:
#include <stdio.h>
#include <stdlib.h>

main    ()
{
        char    buf [BUFSIZ];

        (void) sprintf (buf, "002");
        (void) fprintf (stdout, "%d\n", atoi (buf));
}

Last edited by tronayne; 11-11-2008 at 02:34 PM.
 
Old 11-11-2008, 05:32 PM   #3
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,539

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
Another option, is to use strtok().
PHP Code:
  // check if leading zero exists
  
if (buf[0] == '0')
  {
    
// remove leading zero(es)
    
sprintf(buf"%s"strtok(buf"0"));
  } 
 
Old 11-11-2008, 05:39 PM   #4
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by dwhitney67 View Post
Another option, is to use strtok().
PHP Code:
  // check if leading zero exists
  
if (buf[0] == '0')
  {
    
// remove leading zero(es)
    
sprintf(buf"%s"strtok(buf"0"));
  } 
Well, probably doesn't matter in this case, but 'sprintf' is slow compared to memmove + strlen.
 
Old 11-11-2008, 06:31 PM   #5
estabroo
Senior Member
 
Registered: Jun 2008
Distribution: debian, ubuntu, sidux
Posts: 1,126
Blog Entries: 2

Rep: Reputation: 124Reputation: 124
or something really simple

Code:
        char    buf [BUFSIZ];
        char*   p = buf;
        char*   e = buf+BUFSIZ;
...
        while ((p < e) && (*p == '0') {
          p++;
        }
...
}

Last edited by estabroo; 11-11-2008 at 06:31 PM. Reason: fix code tag
 
  


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
How to check ethernet buffer size in linux fedora OS? nishith Linux - Networking 7 08-28-2008 06:15 AM
PCI-BUG #81 [<some zeroes>] Found! What should I do? algogeek Linux - Hardware 3 04-29-2008 03:11 AM
netstat to check buffer overflow rajat83 Linux - Newbie 1 06-03-2007 11:19 AM
What is the difference between the free buffer and buffer in the buffer hash queue? Swagata Linux - Enterprise 0 05-26-2006 12:57 AM
pflogsumm generates zeroes for results rioguia Linux - Software 1 09-29-2004 04:29 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:53 AM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration