Latest LQ Deal: Latest LQ Deals
Go Back > Forums > Non-*NIX Forums > Programming
User Name
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.


  Search this Thread
Old 11-11-2008, 10:43 AM   #1
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

Old 11-11-2008, 11:04 AM   #2
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: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065
Easy way? Try using strtol() and sprintf(), something like this
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):
#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
Senior Member
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

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)
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
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)
Well, probably doesn't matter in this case, but 'sprintf' is slow compared to memmove + strlen.
Old 11-11-2008, 06:31 PM   #5
Senior Member
Registered: Jun 2008
Distribution: debian, ubuntu, sidux
Posts: 1,126
Blog Entries: 2

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

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

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


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 > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:22 AM.

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