开发者

How to format integer correctly in c?

开发者 https://www.devze.com 2022-12-21 06:54 出处:网络
Say I have a text file that has multiple names and their corresponding birthdays such as: john doe 2 34

Say I have a text file that has multiple names and their corresponding birthdays such as:

john doe 2 34

lauren doe 3 4

albert r. grifton 03 12

The converter program will make usernames for the students such as:

jd0234

ld0304

arg0312

The problem I am experiencing is adding the zeros for the if/else conditions for the odd amounts of birthdays.

As my prog开发者_如何学JAVAram currently stands, it prints out:

jd234

ld34

arg0312

I know there are 3 cases.

  1. If there are 3 digits, add a zero.
  2. If there are 2 digits, add two zeros.
  3. If there are 4 digits, everything is fine.

I know how to format the string accordingly, with printf("%02d, num). Although I don't think it is needed for my purpose, as this a program that works with another that uses pipes.

//      converter.c
//      

#include <string.h>
#include <stdio.h>
#include <ctype.h>

int main(void)
{
    char line[512];
while(!feof(stdin))
{
    if((fgets(line, sizeof(line), stdin) != 0))
    {
        char  name[16];
        char *dst = name;
        char *end = name + sizeof(name) - 1;
        char *src = line;
        while (*src != '\0')
        {
           char  c;
           while ((c = *src++) != '\0' && isspace(c)){}

           if (isalpha(c))
           {
               if (dst < end)
                   *dst++ = tolower(c);
               while ((c = *src++) != '\0' && !isspace(c)){}
           }
           else if (isdigit(c))
           {
                              //birthdays are evaluated here
               while (dst < end && isdigit(c))
               {
                    *dst++ = c;
                    c = *src++;
               }
           }
        }

        *dst = '\0';
        puts(name);
        fflush(stdout);
    }
}
return 0;
}


Once you've tokenized the string, the last two tokens should be parsed with atoi then you'll have two integers. Those can be printed with sprintf(%02d, myInt).


When you get to a digit, you know that the following character should be either another digit, a space, or the end of the string. Check for one of those conditions to determine whether you need to stick in a zero. You may also check that there are not more than 2 consecutive digits.


sprintf() and snprintf().


try this:

#include <string.h>
#include <stdio.h>
#include <ctype.h>

int main(void)
{
    char line[512];
    while(!feof(stdin))
    {
        if((fgets(line, sizeof(line), stdin) != 0))
        {
            char  buffer[512];
            char *p1 = line;
            char *p2 = NULL;
            int   valid = 0;

            while(*p1)
            {
                /* skip space */
                for(; *p1 && isspace(*p1); *p1++);

                /* extract to next space */
                for(p2 = buffer; *p1 && !isspace(*p1); *p2++ = *p1++);

                /* terminate p2 */
                *p2 = 0;

                /* check for alpha or digit */
                if(isalpha(buffer[0])) {
                    printf("%c", buffer[0]);
                    valid = 1;
                } else if(isdigit(buffer[0])) {
                    printf("%02d", atoi(buffer));
                    valid = 1;
                }
            }

            if(valid) {
                printf("\n");
            }
        }
    }
    return 0;
}


I have to question the variables dst, line and name being used...are they initialized? Looking at it, I'm under the impression as they are not initialized, there may be garbage in the buffers in which the pointers are pointing at and I have a feeling that the pointers are going to trip up and overstep the boundaries...check and observe very closely prior to running it...

Edit: After a comment from the OP...here's the hints...

line[0] = '\0'; 
// before the 'if((fgets(line, sizeof(line), stdin) != 0))'
name[0] = '\0';
// before the 'while (*src != '\0')'

Hope this helps, Best regards, Tom.


You can use sprintf for this....i wil give u an example... Suppose you have an integer value i...it can be anything between 1-30.... Now for values from 1 to 9 you want to print it as 01,02,....09....rite???

Follow this:

int main() { int i = 1; char str[10]; sprintf(str,"%02d",i); }

0

精彩评论

暂无评论...
验证码 换一张
取 消