1. Read in a line of input then print the line in reverse order. You should stor
ID: 672327 • Letter: 1
Question
1. Read in a line of input then print the line in reverse order. You should store the input in an array of char. Assume that the line is no longer than 255 characters. Note that you can use scanf() with the %c specifier to read one character at a time from input and that the new line character ' ' is generated when you press the Enter Key. You should use a while loop to gather the user input and use a for loop to output the reverse input.???
2. Write a fuction called unusedSpace() that recieves the length and size of the character array from #1 as integers and returns the unused space in the character array (size of the array minus the length of the string). This function will be called from main() and has a function prototype as follows:
int unusedSpace (int length, int size);?
3. Your output, as show in the sample execution, should be: the reversed user input, and the length of the character array, the size of the character array, and the unused space in the character array.?
4. You can use the following command to ensure that the memory allocated for your character array is empty:
f?or(x=0;x<255;x++){string[x] = '';}
Explanation / Answer
#include <stdio.h>
int unusedSpace (int length, int size) {
printf(" Char Array Length: %d", length);
printf(" Char Array Size: %d", size);
printf(" Char Array Unused Space: %d", (size-length));
}
int main() {
char input[255];
char ch;
int i=0, j;
for(j=0;j<255;j++){input[j] = '';}
printf("Enter a line of text upto 255 characters ");
while(1) {
scanf("%c", &ch);
if(ch == ' ') {
input[i] = '';
break;
}
input[i] = ch;
i++;
}
printf(" Reversed: ");
for(j=i; j>=0; j--)
printf("%c", input[j]);
unusedSpace(i, sizeof(input));
printf(" ");
printf("%s %d", input, i);
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.