COMP2113
COMP2113_ENGG1340 Programming technologies and Computer programming II [Section 2BC] [2023]
Loading...
Searching...
No Matches
2.c
Go to the documentation of this file.
1#include <stdio.h>
2#include <string.h>
3
4int main(void) {
5 char inputs[501];
6 // not &inputs because it's already a pointer (to array)
7 scanf("%s", inputs);
8 char outputs[1001] = "\0";
9 // Perform run-length encoding and store the results to 'outputs'
10 unsigned int currentCharLength = 0;
11 char currentChar = inputs[0];
12 for (unsigned int i = 0; i <= strlen(inputs); i++) {
13 if (inputs[i] == currentChar) {
14 currentCharLength++;
15 }
16 else {
17 outputs[strlen(outputs)] = currentChar;
18 // Convert unsigned int with multiple digits to char array.
19 sprintf(outputs + strlen(outputs), "%u", currentCharLength);
20 outputs[strlen(outputs)] = '\0';
21 currentChar = inputs[i];
22 currentCharLength = 1;
23 }
24 }
25 // Print the results and display compression rate.
26 printf("%s\n", outputs);
27 // Automatic type convertion to double on the first variable,
28 // result in final result as double.
29 printf("%.3f", (double)strlen(outputs) / strlen(inputs));
30}
int main(void)
Definition 2.c:4