CHAPTER.10\MONTH.C ·
C ·
2.5 KB ·
1989-12-22 ·
from PCPlus_Help-Screen-Collection_1990
#include <stdio.h>
#include <stdlib.h>
char line[255];
char months[] = "JanFebMarAprMayJunJulAugSepOctNovDec";
char codes[] = "m01m02m03m04m05m06m07m08m09m10m11m12";
int find(), compare();
void fix_line(), swap();
main()
{
while(gets(line)) /* Read line from STDIN */
{ /* If not EOF then... */
fix_line(); /* swap months & codes, */
puts(line); /* write line to STDOUT */
} /* ...else exit. */
}
fix_line()
{
int l_ptr;
for(l_ptr=0; line[l_ptr]!='\0'; l_ptr++)
{ /* For each chr in line[],
check for month... */
if(!find(l_ptr,months,codes)) /* ...and if this fails */
find(l_ptr,codes,months); /* check for code. */
}
}
find(l_ptr,search,replace)
int l_ptr;
char *search, *replace;
{
int m_ptr, match;
for(m_ptr=0; m_ptr<36; m_ptr+=3) /* Check at line[l_ptr] */
{ /* for each month/code. */
if(!(match=compare(m_ptr,l_ptr,search)))
continue; /* If not found then try
next month/code.
Otherwise,... */
swap(m_ptr,l_ptr,replace); /* ...overwrite month/code
with code/month, */
break; /* and stop searching */
}
return match; /* TRUE if and only if */
} /* month/code found. */
compare(m_ptr,l_ptr,search)
int m_ptr, l_ptr;
char *search;
{
int ch, match;
for(ch=0; ch<3; ch++) /* Do 3 chrs at line[l_ptr]
form month/code? */
if(!(match=line[l_ptr+ch]==search[m_ptr+ch]))
break; /* Stop comparison at 1st
non-matching chr. */
return match; /* TRUE if and only if */
} /* month/code found. */
swap(m_ptr,l_ptr,replace)
int m_ptr, l_ptr;
char *replace;
{
int ch;
for(ch=0; ch<3; ch++)
line[l_ptr+ch]=replace[m_ptr+ch];
}