#include #include #include #define MAC 50 #define MAX 10 char * chdectoasc(int dec); int main(void) { int k, loc; char str[MAC]; // in your lab, the room names are in the 2-D string array char *tocat = NULL; strcpy(str, "A sales room"); // Use the values in your sales room name array //puts(str); //error check for debug //loc = strlen(str); //error check //printf("\nloc is %d before\n",loc); //error check for debug //Get the sales room number (in this sample, the user gives the number). In your lab, //the sales room number is one of the array indicies printf("Please enter a positive integer: "); scanf("%d", &k); //Call chdectoasc to make a char string that contains the number k tocat = chdectoasc(k); printf("\ntocat is %s\n",tocat); //error checking for debug //Concatenate a blank and the number string on the end of the sales room name strcat(str, " "); strcat(str, tocat); printf("\nstr is **%s**\n",str); // error checking for debug loc = strlen(str); // Find the length of the new string-with-number printf("\nloc is %d after and char at loc is %c\n",loc,str[loc-1]); //error check for debug //Use atoi if you need to get the number back out of the string //The number will always be at the end of the string so //str (start of string) + loc (count of chars in string) - 1 (index starts at 0) printf("\nNumber of the room is %d\n",atoi(str+loc-1)); //example use of atoi return 0; } /****** function chdectoasc : CHange DECimal TO ASCii char * chdectoasc(int dec) Input: int dec - Takes in a positive integer Output: char * - Returns the address of a string that contains the ASCII representation of that number ******/ char * chdectoasc(int dec) { // ASCII table values to print the 10 digits are 48 chars away from the digit itself char *temp; int tens = 0, i, dtemp, ltr; // printf("dec is %d\n",dec); //error checking dtemp = dec; //Count the number of digits in dtemp while (dtemp > 9) { dtemp = dtemp / 10.0; // printf("dtemp is %d\n",dtemp); //error checking tens++; } dtemp = dec; //Allocate a string big enough to hold all the digits and an end of string marker temp = (char *)malloc(sizeof(char)*(tens+2)); //Loop through the digits, take a remainder put its ASCII rep in string, divide it out of dtemp for (i=0; i<= tens; i++) { ltr = dtemp % 10; dtemp = dtemp / 10.0; temp[tens-i] = (char) (ltr +48); } //Put in end of string marker temp[tens+1] = '\0'; // printf(temp); //use for error checking return temp; }