the following is a simple program that fragments standardized name consisting of 3 letters and 2 numbers then prints the full word, for instance spa11 print spanish eleven.
Why is it not working?
char sname[4];
char snum[3];
// void strnumCpy(char *dest, char *source) { // NOT ACCURATE
void strNumCpy(char *dest, const char *source) { // You are passing a const char in the source parameter, not a regular char.
int i=0;
do { // Isn't it easier to use a for loop here ? :)
*source++;
i += 1;
} while (i < 3);
while (*source)
*dest++ = *source++ ;
*dest = 0; // This adds the null terminator
}
void look_up(char *fname)
{
strncpy(sname, fname, 3);
sname[3] = 0;
strNumCpy(snum, fname);
read_sname(sname);
read_snum(snum);
}
void read_sname(char *snm)
{
switch(snm) {
case "ara": printf("\n%s\n", 'arabic'); break;
case "spa": printf("\n%s\n", 'spanish'); break;
case "eng": printf("\n%s\n", 'english'); break;
case "bio": printf("\n%s\n", 'biology'); break;
case "ger": printf("\n%s\n", 'german'); break;
}
}
void read_snum(char *sno)
{
switch(sno) {
case "01": printf("\n%s\n", 'one'); break;
case "02": printf("\n%s\n", 'two'); break;
case "03": printf("\n%s\n", 'three'); break;
case "05": printf("\n%s\n", 'five'); break;
case "11": printf("\n%s\n", 'eleven'); break;
}
}
void main()
{
look_up("spa02");
look_up("eng11");
look_up("ger05");
}