fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. int main(void){
  6. char input[1024];
  7. char result[2*sizeof(input)];
  8. int length = 0;// The length of the input
  9. FILE *openFile = stdin;//fopen("input.txt", "r");
  10.  
  11. if(!openFile){
  12. perror("fopen:");
  13. exit(EXIT_FAILURE);
  14. }
  15.  
  16. while(fgets(input, 1024, openFile)){
  17. char *p = strchr(input, '\n');
  18. if(!p){
  19. fprintf(stderr, "Input too long!\n");
  20. exit(EXIT_FAILURE);
  21. }
  22. *p = 0;//chomp newline
  23. length = p - input;
  24. if(*input == '.' && !input[1]){
  25. puts("c u!");
  26. return 0;
  27. }
  28. //conversion
  29. int resultLength = 0;
  30. int i, j;
  31. for(i = 0; i < length; i = j){
  32. int count = 1;
  33. for(j = i + 1; input[i] == input[j]; ++j)
  34. ++count;
  35.  
  36. while(count > 9){
  37. result[resultLength++] = '9';
  38. result[resultLength++] = input[i];
  39. count -= 9;
  40. }
  41. result[resultLength++] = count + '0';
  42. result[resultLength++] = input[i];
  43. }
  44. result[resultLength] = 0;
  45.  
  46. //Print out the result
  47. printf("The next element in the sequence is: %s\n", result);
  48. }
  49. fclose(openFile);
  50. return 0;
  51. }
Success #stdin #stdout 0s 9416KB
stdin
21
114421
111111111111
.
stdout
The next element in the sequence is: 1211
The next element in the sequence is: 21241211
The next element in the sequence is: 9131
c u!