Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>There are probably other ways to do it, but this code works. You did a good job on the algorithm in <code>reverse()</code>. In the <code>reverse()</code> function, the only changes were formatting of the code and adding the more complete diagnostic printing. The <code>main()</code> function was rewritten to read a line using <code>fgets()</code> and then parse that line. If nothing else, doing that makes it easier to report erroneous input (though the code does not report properly on lines that do not contain a semicolon, for example, and the code assumes that the <code>k</code> items at a time is always a single digit, and it does not insist on single digits separated by commas, and ...).</p> <pre><code>#include &lt;stdio.h&gt; #include &lt;stdlib.h&gt; #include &lt;string.h&gt; static void reverse(char str[], int p) { int i, j, k, a, b; int len = strlen(str); printf("Input: %d: %s (%d)\n", len, str, p); a = 0; b = p - 1; while (1) { i = a; j = b; while (j &gt; i) { k = str[i]; str[i] = str[j]; str[j] = k; i++; j--; } a = a + p; b = b + p; if (b &gt;= len) break; } printf("Output: "); for (i = 0; i &lt; len - 1; i++) printf("%c, ", str[i]); if (i == len - 1) printf("%c", str[i]); printf("\n"); } int main(int argc, char* argv[]) { if (argc != 2) { printf("Wrong number of arguments\n"); exit(1); } FILE *fp = fopen(argv[1], "r"); if (fp == NULL) { printf("File can't be openend:\n"); exit(1); } char line[100]; while (fgets(line, sizeof(line), fp) != 0) { printf("Read: %s", line); fflush(0); char str[100]; int k = 0; int i = 0; char c; while ((c = line[i++]) != '\0') { if (c == ';') break; else if (c == ',') continue; else str[k++] = c; } str[k] = '\0'; printf("String: %s\n", str); if (c == ';') { c = line[i]; int p = c - '0'; reverse(str, p); } } fclose(fp); return 0; } </code></pre> <h3>Sample input data</h3> <pre><code>1,2,3,4,5;2 1,2,3,4,5,6,7;3 1,2,3,4,5,6,7,8,9;2 1,2,3,4,5,6,7,8,9;3 1,2,3,4,5,6,7,8,9;4 1,2,3,4,5,6,7,8,9;5 </code></pre> <h3>Sample output</h3> <pre><code>Read: 1,2,3,4,5;2 String: 12345 Input: 5: 12345 (2) Output: 2, 1, 4, 3, 5 Read: 1,2,3,4,5,6,7;3 String: 1234567 Input: 7: 1234567 (3) Output: 3, 2, 1, 6, 5, 4, 7 Read: 1,2,3,4,5,6,7,8,9;2 String: 123456789 Input: 9: 123456789 (2) Output: 2, 1, 4, 3, 6, 5, 8, 7, 9 Read: 1,2,3,4,5,6,7,8,9;3 String: 123456789 Input: 9: 123456789 (3) Output: 3, 2, 1, 6, 5, 4, 9, 8, 7 Read: 1,2,3,4,5,6,7,8,9;4 String: 123456789 Input: 9: 123456789 (4) Output: 4, 3, 2, 1, 8, 7, 6, 5, 9 Read: 1,2,3,4,5,6,7,8,9;5 String: 123456789 Input: 9: 123456789 (5) Output: 5, 4, 3, 2, 1, 6, 7, 8, 9 </code></pre> <h3>Debugging the original</h3> <p>If you have to debug the original code, then the diagnostic printing in <code>reverse()</code> that I added would help there. In the <code>main()</code> code, you'd need to think about echoing each character as it arrives, and perhaps printing the <code>str</code> array. You'd need to be careful there because you filled it with zeros. You might use <code>printf("String: %.*s\n", k, str);</code> to limit the output to the first <code>k</code> characters of <code>str</code>.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload