In C Language: This question asks you to simply implement ListMnemonics(char *st
ID: 663949 • Letter: I
Question
In C Language:
This question asks you to simply implement ListMnemonics(char *str) function, and other subsidiary or utility functions (if any). In this assignment, however, we ask you to implement a mnemonics library which exports ListMnemonics(char *str); in mnemonics.h and implement it along with other subsidiary and utility functions (if any) in mnemonics.c.
Once your library is implemented, then you are asked to implement a client/driver program (e.g., driver.c) that gets different strings from the command line and calls the ListMnemonics() function for each string. Your driver program should check each string and make sure each string contains only digits between 2 and 9; otherwise, gives an error msg for that string.
If we run your program as follows
> program 723 41267 3a5b81
Your program should generate the following outputs
Mnemonics for 723 are:
PAD PBD PCD RAD RBD RCD .... (as given in the textbook)
Mnemonics for 412 are:
None, 412 contains a digit 1
Mnemonics for 3a5b81 are:
None, 3a5b81 contains at least one alphabetic character
Explanation / Answer
#include<stdio.h>
#include<stdlib.h>
#include <math.h>
int noOfDigits(int n)
{
int count = 0;
while(n > 0)
{
n = n / 10;
count++;
}
return count;
}
void ListMnemonics(char *str)
{
char mnemonicConst[10][4] = {{}, { 'A', 'B', 'C' },{'D','E','F'},{'G','H','I'},{'J','K','L'},{'M','N','O'},{'P','Q','R','S'},{'T','U','V'},{'W','X','Y','Z'}};
//Get list of numbers in string
int listOfNos[10],i,j;
int number = atoi(str);
int count = noOfDigits(number);
for(i = count-1 ; i >=0 ; i--)
{
int temp = 1;
for(j = 1 ; j <= i ; j++)
{
temp = temp*10;
}
listOfNos[count - i + 1] = number/temp;
printf("%d,%d ",count - i,listOfNos[i]);
number = number % temp;
}
for( i = 0 ; i <count ; i++)
printf("%d",listOfNos[i]);
int pointers[count];
for( i = 0 ; i <count ; i++)
pointers[i] = 0;
char mnemonics[100][count];
i = 0;
while(1)
{
//creating mnemonic
for(j = 0 ; j < count ; j++)
{
mnemonics[i][j] = mnemonicConst[listOfNos[j]+1][pointers[j]++];
printf("%c",mnemonics[i][j]);
}
break;
}
}
int main()
{
ListMnemonics("723");
return 0;
}