tags:

views:

117

answers:

6

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 program 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;
}

A: 

sprintf() and snprintf().

Ignacio Vazquez-Abrams
+1  A: 

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.

Dan
Is there a command to check the next char after the current position?
foobiefoob
Think about what `*src++` is doing. It dereferences the `src` pointer to get the character value at that pointer, and it increments the pointer to point to the next character. To see what the next character is, without advancing to that character, you could do `char nextCh = *(src + 1);`. Then switch on that value. If it's another digit you'll want to "consume" it by advancing the pointer using `++src;`.
Dan
A: 

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;
}
fen
+3  A: 

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).

David Kanarek
A: 

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.

tommieb75
Yeah! I was noticing that the current way it is now. Garbage was being printed with like weird boxes and sometimes I got random new lines being printed out.
foobiefoob
This is what I've got:for(k = 0; k < 512; k++)line[k] = '\0';if((fgets(line, sizeof(line), stdin) != 0)) { char name[16]; char *dst = name; char *end = name + sizeof(name) - 1; char *src = line; //name[0] = '\0'; int i; for(i = 0; i < 16; i++) name[i] = '\0'; while (*src != '\0'){..}
foobiefoob
Get rid of the for loops...the hints I presented will suffice! :). Or you can use memset(line, 0, sizeof(line)); and memset(name, 0, sizeof(name));
tommieb75
Thank you, it lessened the amount of garbage being printed out to the file. But there is a little bit still left. I suspect it's coming from my manager program that's using the pipes.
foobiefoob
A: 

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); }

Furquan