building a single file program using three functions: main - the main program pr
ID: 3813314 • Letter: B
Question
building a single file program using three functions:
main - the main program
print_data - an output function
convert_data - a function that modifies data
Here is the process you should follow:
--Data Setup: Create a data array holding 5 strings in the initialized data section. Each string has exactly 4 characters - three ‘0’ (zero) characters followed by a single lower case letter (“000p”, “000c”, etc). Note that there is no zero byte at the end of any of these strings.
--Main function: Create a main function that calls a simple print_data procedure, passing the address of your data area in a register. You will add more to this in a later step.
-- print_data: Create the print_data procedure next. This procedure sets up a loop that runs five times. Inside the loop, you should set up another loop that runs four times. Each time through his inner loop, print one of the characters from the data. (You can use the C putchar function to do the output.) After each string has been output, end the string with a newline. This procedure should print out the set of strings as created in the above section, one string per line.
-- convert_data: Set up a convert_data procedure that will receive the address of the data array on the stack and convert the lower case letter to an upper case letter without modifying any of the ‘0’ characters in each string. You can use any scheme you can think of to do this. The modifications should happen in place - that is the original data elements are changed after your procedure is called. This function does not print anything, and returns nothing.
--Main function: Back in the main function, add code to call the convert_data procedure from step 4. Then call the print_data procedure you constructed in step 3 to print out the array again as you did earlier. You should see the same output with the letter capitalized.
None of these steps take much code. The hardest part is figuring out how to print the data in the array (in the loop) and finding the character you want to modify in the convert_data function (another loop?). There are many approaches to doing these tasks available.
Explanation / Answer
/**
C program that declare and intializes a c string array
with string values . Then prints the data in the c string array
to console before and after converting the data in c string
except '0'
*/
//include header files
#include<stdio.h>
#include<string>
#include<conio.h>
#define SIZE 5
#define COUNT 5
//function prototypes
void print_data(char str[SIZE][COUNT]);
void convert_data(char str[SIZE][COUNT]);
int main()
{
//intialize cstriing array ,str with values
char str[COUNT][SIZE]={"000p", "000c","000a", "000b","000t"};
printf("Before conversion ");
//calling print_data
print_data(str);
//calling convert_data
convert_data(str);
printf("After conversion ");
//calling print_data
print_data(str);
getch();
return 0;
}
/**
The method convert_data that take str array and
converts the chracter otherthan '0' to upper case
*/
void convert_data(char str[SIZE][COUNT])
{
int r=0;
int c=0;
for(r=0;r<COUNT;r++)
{
for(c=0;c<strlen(str[r]);c++)
if(str[r][c]!='0')
str[r][c]=toupper(str[r][c]);
}
}
/**
The method print_data that take str array and
prints the characters in the cstring to console
output using putchar
*/
void print_data(char str[SIZE][COUNT])
{
int r=0;
int c=0;
for(r=0;r<COUNT;r++)
{
for(c=0;c<strlen(str[r]);c++)
putchar(str[r][c]);
printf(" ");
}
}
Sample Ouptut:
Before conversion
000p
000c
000a
000b
000t
After conversion
000P
000C
000A
000B
000T
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.