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.
- If there are 3 digits, add a zero.
- If there are 2 digits, add two zeros.
- 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;
}