fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. char* read_string(size_t *size){
  5. char *str;
  6. int c;
  7. size_t length = 0;
  8. str = realloc(NULL, sizeof(char)*(*size));
  9. if(!str)return str;
  10. while((c=getchar()) != EOF && c != '\n'){
  11. str[length++] = c;
  12. if(length == *size) {
  13. str = realloc(str, sizeof(char)*(*size += 10));
  14. if(!str)
  15. return str;
  16. }
  17. }
  18. str[length++] = '\0';
  19. size = length;
  20. return str;
  21. }
  22.  
  23. int main() {
  24. int n;
  25. scanf("%d", &n);
  26. char tape[10000];
  27. for (int k = 0; k < n; k++)
  28. tape[k] = 0;
  29. int pointer = 0;
  30. size_t length = 100;
  31. char *program = read_string(&length);
  32. printf("%s\n", program);
  33. for (unsigned int i = 0; i < length; i++) {
  34. if (program[i] == '>')
  35. pointer++;
  36.  
  37. if (program[i] == '<')
  38. pointer--;
  39.  
  40. if (program[i] == '+')
  41. tape[pointer]++;
  42.  
  43. if (program[i] == '-')
  44. tape[pointer]--;
  45.  
  46. if (program[i] == '.')
  47. printf("%c", tape[pointer]);
  48.  
  49. if (program[i] == ',')
  50. scanf("%c", &tape[pointer]);
  51.  
  52. if (program[i] == '[') {
  53. if (tape[pointer] == 0) {
  54. int counter = 1;
  55. while (counter > 0) {
  56. i++;
  57. if (program[i] == '[')
  58. counter++;
  59. if (program[i] == ']')
  60. counter--;
  61. }
  62. }
  63. }
  64. else if (program[i] == ']') {
  65. if (tape[pointer] != 0) {
  66. int counter = 1;
  67. while (counter > 0) {
  68. i--;
  69. if (program[i] == '[')
  70. counter--;
  71. if (program[i] == ']')
  72. counter++;
  73. }
  74. }
  75. }
  76. }
  77.  
  78. return 0;
  79. }
Success #stdin #stdout 0s 2248KB
stdin
100
++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++..+++.>++.<<+++++++++++++++.>.+++.------.--------.>+.>.
stdout
++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++..+++.>++.<<+++++++++++++++.>.+++.------.--------.>+.>.
Hello World!