如何在C中反转数组中的字符串。

huangapple go评论100阅读模式
英文:

how to reverse string in array in C

问题

我已翻译以下代码段:

  1. char *problem58()
  2. {
  3. char temp, *result = (char *)malloc(52 * sizeof(char));
  4. int left = 0, right = 52 - 1;
  5. for (char i = 'A'; i <= 'Z'; i++)
  6. {
  7. result[left] = i;
  8. printf("%c", i);
  9. left++;
  10. }
  11. // 这是我用来反转字符串的方法
  12. for (int i = left; i < right; i++)
  13. {
  14. temp = result[left];
  15. result[left] = result[right];
  16. result[right] = temp;
  17. printf("%c", result[right]);
  18. right--;
  19. }
  20. return result;
  21. }

运行此行代码在终端中输出:ABCDEFGHIJKLMNOPQRSTUVWXYZSW\:C=cepSmoC

英文:

I have exercise to use for loop to initialization 1 string array from A to Z and then Z to A.

I was successful to print from A to Z but print Z to A not working. So i need to help in this code.

  1. char *problem58()
  2. {
  3. char temp, *result = (char *)malloc(52 * sizeof(char));
  4. int left = 0, right = 52 - 1;
  5. for (char i = &#39;A&#39;; i &lt;= &#39;Z&#39;; i++)
  6. {
  7. result[left] = i;
  8. printf(&quot;%c&quot;, i);
  9. left++;
  10. }
  11. // this is my idea to reverse this string
  12. for (int i = left; i &lt; right; i++)
  13. {
  14. temp = result[left];
  15. result[left] = result[right];
  16. result[right] = temp;
  17. printf(&quot;%c&quot;, result[right]);
  18. right--;
  19. }
  20. return result;
  21. }

line code run in terminal: ABCDEFGHIJKLMNOPQRSTUVWXYZSW\:C=cepSmoC

答案1

得分: 0

这是我的答案

  1. #include &lt;stdio.h&gt;
  2. #include &lt;string.h&gt;
  3. #include &lt;stdlib.h&gt;
  4. char* problem58()
  5. {
  6. int n = 0;
  7. int left = 0, right = 52 - 1;
  8. // A---&gt;Z and Z---&gt;A total number is 52, because string end with &#39;
    #include &lt;stdio.h&gt;
  9. #include &lt;string.h&gt;
  10. #include &lt;stdlib.h&gt;
  11. char* problem58()
  12. {
  13.     int n = 0;
  14.     int left = 0, right = 52 - 1;
  15.     // A---&gt;Z and Z---&gt;A total number is 52, because string end with &#39;
    #include &lt;stdio.h&gt;
  16. #include &lt;string.h&gt;
  17. #include &lt;stdlib.h&gt;
  18. char* problem58()
  19. {
  20. int n = 0;
  21. int left = 0, right = 52 - 1;
  22. // A---&gt;Z and Z---&gt;A total number is 52, because string end with &#39;\0&#39;(0), so add 1 is 53.
  23. char* result = (char*)malloc(53 * sizeof(char));
  24. // Check if it is possible to dynamically allocate memory for 53 variables of type &quot;char&quot;
  25. // You can also write like this: if(result == NULL)
  26. if(NULL == result){
  27. printf(&quot;No space\n&quot;);
  28. exit(1);
  29. }
  30. // Initial result with 0,because string end with (&#39;\0&#39;)0.
  31. // So result[0] will be 0, result[1] will be 0 ...... result[52] will be 0
  32. for(int m = 0; m &lt; 53;m++){
  33. result[m] = 0;
  34. }
  35. for (char i = &#39;A&#39;; i &lt;= &#39;Z&#39;; i++)
  36. {
  37. result[left] = i;
  38. //printf(&quot;%c&quot;, i);
  39. left++;
  40. }
  41. // Now left is 26, reset it to 0, because result[0] is &#39;A&#39;
  42. left = 0;
  43. // n is execute times, need to let result[51] = result[0] = &#39;A&#39;, and result[50] = result[1] = &#39;B&#39; ......
  44. //so right-- and left++
  45. for (n = 0; n &lt; 26; n++)
  46. {
  47. result[right] = result[left];
  48. right--;
  49. left++;
  50. }
  51. return result;
  52. }
  53. int main()
  54. {
  55. char* str = problem58();
  56. printf(&quot;str&#39;s length is %ld\n&quot;,strlen(str));
  57. printf(&quot;%s\n&quot;,str);
  58. free(str);
  59. return 0;
  60. }
  61. &#39;(0), so add 1 is 53.
  62.     char* result = (char*)malloc(53 * sizeof(char));
  63.     // Check if it is possible to dynamically allocate memory for 53 variables of type &quot;char&quot;
  64.     // You can also write like this: if(result == NULL)
  65.     if(NULL == result){
  66.         printf(&quot;No space\n&quot;);
  67.         exit(1);
  68.     }
  69.     // Initial result with 0,because string end with (&#39;
    #include &lt;stdio.h&gt;
  70. #include &lt;string.h&gt;
  71. #include &lt;stdlib.h&gt;
  72. char* problem58()
  73. {
  74. int n = 0;
  75. int left = 0, right = 52 - 1;
  76. // A---&gt;Z and Z---&gt;A total number is 52, because string end with &#39;\0&#39;(0), so add 1 is 53.
  77. char* result = (char*)malloc(53 * sizeof(char));
  78. // Check if it is possible to dynamically allocate memory for 53 variables of type &quot;char&quot;
  79. // You can also write like this: if(result == NULL)
  80. if(NULL == result){
  81. printf(&quot;No space\n&quot;);
  82. exit(1);
  83. }
  84. // Initial result with 0,because string end with (&#39;\0&#39;)0.
  85. // So result[0] will be 0, result[1] will be 0 ...... result[52] will be 0
  86. for(int m = 0; m &lt; 53;m++){
  87. result[m] = 0;
  88. }
  89. for (char i = &#39;A&#39;; i &lt;= &#39;Z&#39;; i++)
  90. {
  91. result[left] = i;
  92. //printf(&quot;%c&quot;, i);
  93. left++;
  94. }
  95. // Now left is 26, reset it to 0, because result[0] is &#39;A&#39;
  96. left = 0;
  97. // n is execute times, need to let result[51] = result[0] = &#39;A&#39;, and result[50] = result[1] = &#39;B&#39; ......
  98. //so right-- and left++
  99. for (n = 0; n &lt; 26; n++)
  100. {
  101. result[right] = result[left];
  102. right--;
  103. left++;
  104. }
  105. return result;
  106. }
  107. int main()
  108. {
  109. char* str = problem58();
  110. printf(&quot;str&#39;s length is %ld\n&quot;,strlen(str));
  111. printf(&quot;%s\n&quot;,str);
  112. free(str);
  113. return 0;
  114. }
  115. &#39;)0.
  116.     // So result[0] will be 0, result[1] will be 0 ...... result[52] will be 0
  117.     for(int m = 0; m &lt; 53;m++){
  118.         result[m] = 0;
  119.     }
  120.     for (char i = &#39;A&#39;; i &lt;= &#39;Z&#39;; i++)
  121.     {
  122.         result[left] = i;
  123.         //printf(&quot;%c&quot;, i);
  124.         left++;
  125.     }
  126.     // Now left is 26, reset it to 0, because result[0] is &#39;A&#39;
  127.     left = 0;
  128.     // n is execute times, need to let result[51] = result[0] = &#39;A&#39;, and result[50] = result[1] = &#39;B&#39; ......
  129.     //so right-- and left++
  130.     for (n = 0; n &lt; 26; n++)
  131.     {
  132.         result[right] = result[left];
  133.         right--;
  134.         left++;
  135.     }
  136.     return result;
  137. }
  138. int main()
  139. {
  140.     char* str = problem58();
  141.     printf(&quot;str&#39;s length is %ld\n&quot;,strlen(str));
  142.     printf(&quot;%s\n&quot;,str);
  143.     free(str);
  144.     return 0;
  145. }
  146. &#39;(0), so add 1 is 53.
  147. char* result = (char*)malloc(53 * sizeof(char));
  148. // Check if it is possible to dynamically allocate memory for 53 variables of type &quot;char&quot;
  149. // You can also write like this: if(result == NULL)
  150. if(NULL == result){
  151. printf(&quot;No space\n&quot;);
  152. exit(1);
  153. }
  154. // Initial result with 0,because string end with (&#39;
    #include &lt;stdio.h&gt;
  155. #include &lt;string.h&gt;
  156. #include &lt;stdlib.h&gt;
  157. char* problem58()
  158. {
  159.     int n = 0;
  160.     int left = 0, right = 52 - 1;
  161.     // A---&gt;Z and Z---&gt;A total number is 52, because string end with &#39;
    #include &lt;stdio.h&gt;
  162. #include &lt;string.h&gt;
  163. #include &lt;stdlib.h&gt;
  164. char* problem58()
  165. {
  166. int n = 0;
  167. int left = 0, right = 52 - 1;
  168. // A---&gt;Z and Z---&gt;A total number is 52, because string end with &#39;\0&#39;(0), so add 1 is 53.
  169. char* result = (char*)malloc(53 * sizeof(char));
  170. // Check if it is possible to dynamically allocate memory for 53 variables of type &quot;char&quot;
  171. // You can also write like this: if(result == NULL)
  172. if(NULL == result){
  173. printf(&quot;No space\n&quot;);
  174. exit(1);
  175. }
  176. // Initial result with 0,because string end with (&#39;\0&#39;)0.
  177. // So result[0] will be 0, result[1] will be 0 ...... result[52] will be 0
  178. for(int m = 0; m &lt; 53;m++){
  179. result[m] = 0;
  180. }
  181. for (char i = &#39;A&#39;; i &lt;= &#39;Z&#39;; i++)
  182. {
  183. result[left] = i;
  184. //printf(&quot;%c&quot;, i);
  185. left++;
  186. }
  187. // Now left is 26, reset it to 0, because result[0] is &#39;A&#39;
  188. left = 0;
  189. // n is execute times, need to let result[51] = result[0] = &#39;A&#39;, and result[50] = result[1] = &#39;B&#39; ......
  190. //so right-- and left++
  191. for (n = 0; n &lt; 26; n++)
  192. {
  193. result[right] = result[left];
  194. right--;
  195. left++;
  196. }
  197. return result;
  198. }
  199. int main()
  200. {
  201. char* str = problem58();
  202. printf(&quot;str&#39;s length is %ld\n&quot;,strlen(str));
  203. printf(&quot;%s\n&quot;,str);
  204. free(str);
  205. return 0;
  206. }
  207. &#39;(0), so add 1 is 53.
  208.     char* result = (char*)malloc(53 * sizeof(char));
  209.     // Check if it is possible to dynamically allocate memory for 53 variables of type &quot;char&quot;
  210.     // You can also write like this: if(result == NULL)
  211.     if(NULL == result){
  212.         printf(&quot;No space\n&quot;);
  213.         exit(1);
  214.     }
  215.     // Initial result with 0,because string end with (&#39;
    #include &lt;stdio.h&gt;
  216. #include &lt;string.h&gt;
  217. #include &lt;stdlib.h&gt;
  218. char* problem58()
  219. {
  220. int n = 0;
  221. int left = 0, right = 52 - 1;
  222. // A---&gt;Z and Z---&gt;A total number is 52, because string end with &#39;\0&#39;(0), so add 1 is 53.
  223. char* result = (char*)malloc(53 * sizeof(char));
  224. // Check if it is possible to dynamically allocate memory for 53 variables of type &quot;char&quot;
  225. // You can also write like this: if(result == NULL)
  226. if(NULL == result){
  227. printf(&quot;No space\n&quot;);
  228. exit(1);
  229. }
  230. // Initial result with 0,because string end with (&#39;\0&#39;)0.
  231. // So result[0] will be 0, result[1] will be 0 ...... result[52] will be 0
  232. for(int m = 0; m &lt; 53;m++){
  233. result[m] = 0;
  234. }
  235. for (char i = &#39;A&#39;; i &lt;= &#39;Z&#39;; i++)
  236. {
  237. result[left] = i;
  238. //printf(&quot;%c&quot;, i);
  239. left++;
  240. }
  241. // Now left is 26, reset it to 0, because result[0] is &#39;A&#39;
  242. left = 0;
  243. // n is execute times, need to let result[51] = result[0] = &#39;A&#39;, and result[50] = result[1] = &#39;B&#39; ......
  244. //so right-- and left++
  245. for (n = 0; n &lt; 26; n++)
  246. {
  247. result[right] = result[left];
  248. right--;
  249. left++;
  250. }
  251. return result;
  252. }
  253. int main()
  254. {
  255. char* str = problem58();
  256. printf(&quot;str&#39;s length is %ld\n&quot;,strlen(str));
  257. printf(&quot;%s\n&quot;,str);
  258. free(str);
  259. return 0;
  260. }
  261. &#39;)0.
  262.     // So result[0] will be 0, result[1] will be 0 ...... result[52] will be 0
  263.     for(int m = 0; m &lt; 53;m++){
  264.         result[m] = 0;
  265.     }
  266.     for (char i = &#39;A&#39;; i &lt;= &#39;Z&#39;; i++)
  267.     {
  268.         result[left] = i;
  269.         //printf(&quot;%c&quot;, i);
  270.         left++;
  271.     }
  272.     // Now left is 26, reset it to 0, because result[0] is &#39;A&#39;
  273.     left = 0;
  274.     // n is execute times, need to let result[51] = result[0] = &#39;A&#39;, and result[50] = result[1] = &#39;B&#39; ......
  275.     //so right-- and left++
  276.     for (n = 0; n &lt; 26; n++)
  277.     {
  278.         result[right] = result[left];
  279.         right--;
  280.         left++;
  281.     }
  282.     return result;
  283. }
  284. int main()
  285. {
  286.     char* str = problem58();
  287.     printf(&quot;str&#39;s length is %ld\n&quot;,strlen(str));
  288.     printf(&quot;%s\n&quot;,str);
  289.     free(str);
  290.     return 0;
  291. }
  292. &#39;)0.
  293. // So result[0] will be 0, result[1] will be 0 ...... result[52] will be 0
  294. for(int m = 0; m &lt; 53;m++){
  295. result[m] = 0;
  296. }
  297. for (char i = &#39;A&#39;; i &lt;= &#39;Z&#39;; i++)
  298. {
  299. result[left] = i;
  300. //printf(&quot;%c&quot;, i);
  301. left++;
  302. }
  303. // Now left is 26, reset it to 0, because result[0] is &#39;A&#39;
  304. left = 0;
  305. // n is execute times, need to let result[51] = result[0] = &#39;A&#39;, and result[50] = result[1] = &#39;B&#39; ......
  306. //so right-- and left++
  307. for (n = 0; n &lt; 26; n++)
  308. {
  309. result[right] = result[left];
  310. right--;
  311. left++;
  312. }
  313. return result;
  314. }
  315. int main()
  316. {
  317. char* str = problem58();
  318. printf(&quot;str&#39;s length is %ld\n&quot;,strlen(str));
  319. printf(&quot;%s\n&quot;,str);
  320. free(str);
  321. return 0;
  322. }

运行它会输出

  1. str&#39;s length is 52
  2. ABCDEFGHIJKLMNOPQRSTUVWXYZZYXWVUTSRQPONMLKJIHGFEDCBA
英文:

Here is my answer

  1. #include &lt;stdio.h&gt;
  2. #include &lt;string.h&gt;
  3. #include &lt;stdlib.h&gt;
  4. char* problem58()
  5. {
  6. int n = 0;
  7. int left = 0, right = 52 - 1;
  8. // A---&gt;Z and Z---&gt;A total number is 52, because string end with &#39;
    #include &lt;stdio.h&gt;
  9. #include &lt;string.h&gt;
  10. #include &lt;stdlib.h&gt;
  11. char* problem58()
  12. {
  13.     int n = 0;
  14.     int left = 0, right = 52 - 1;
  15.     // A---&gt;Z and Z---&gt;A total number is 52, because string end with &#39;
    #include &lt;stdio.h&gt;
  16. #include &lt;string.h&gt;
  17. #include &lt;stdlib.h&gt;
  18. char* problem58()
  19. {
  20. int n = 0;
  21. int left = 0, right = 52 - 1;
  22. // A---&gt;Z and Z---&gt;A total number is 52, because string end with &#39;\0&#39;(0), so add 1 is 53.
  23. char* result = (char*)malloc(53 * sizeof(char));
  24. // Check if it is possible to dynamically allocate memory for 53 variables of type &quot;char&quot;
  25. // You can also write like this: if(result == NULL)
  26. if(NULL == result){
  27. printf(&quot;No space\n&quot;);
  28. exit(1);
  29. }
  30. // Initial result with 0,because string end with (&#39;\0&#39;)0.
  31. // So result[0] will be 0, result[1] will be 0 ...... result[52] will be 0
  32. for(int m = 0; m &lt; 53;m++){
  33. result[m] = 0;
  34. }
  35. for (char i = &#39;A&#39;; i &lt;= &#39;Z&#39;; i++)
  36. {
  37. result[left] = i;
  38. //printf(&quot;%c&quot;, i);
  39. left++;
  40. }
  41. // Now left is 26, reset it to 0, because result[0] is &#39;A&#39;
  42. left = 0;
  43. // n is execute times, need to let result[51] = result[0] = &#39;A&#39;, and result[50] = result[1] = &#39;B&#39; ......
  44. //so right-- and left++
  45. for (n = 0; n &lt; 26; n++)
  46. {
  47. result[right] = result[left];
  48. right--;
  49. left++;
  50. }
  51. return result;
  52. }
  53. int main()
  54. {
  55. char* str = problem58();
  56. printf(&quot;str&#39;s length is %ld\n&quot;,strlen(str));
  57. printf(&quot;%s\n&quot;,str);
  58. free(str);
  59. return 0;
  60. }
  61. &#39;(0), so add 1 is 53.
  62.     char* result = (char*)malloc(53 * sizeof(char));
  63.     // Check if it is possible to dynamically allocate memory for 53 variables of type &quot;char&quot;
  64.     // You can also write like this: if(result == NULL)
  65.     if(NULL == result){
  66.         printf(&quot;No space\n&quot;);
  67.         exit(1);
  68.     }
  69.     // Initial result with 0,because string end with (&#39;
    #include &lt;stdio.h&gt;
  70. #include &lt;string.h&gt;
  71. #include &lt;stdlib.h&gt;
  72. char* problem58()
  73. {
  74. int n = 0;
  75. int left = 0, right = 52 - 1;
  76. // A---&gt;Z and Z---&gt;A total number is 52, because string end with &#39;\0&#39;(0), so add 1 is 53.
  77. char* result = (char*)malloc(53 * sizeof(char));
  78. // Check if it is possible to dynamically allocate memory for 53 variables of type &quot;char&quot;
  79. // You can also write like this: if(result == NULL)
  80. if(NULL == result){
  81. printf(&quot;No space\n&quot;);
  82. exit(1);
  83. }
  84. // Initial result with 0,because string end with (&#39;\0&#39;)0.
  85. // So result[0] will be 0, result[1] will be 0 ...... result[52] will be 0
  86. for(int m = 0; m &lt; 53;m++){
  87. result[m] = 0;
  88. }
  89. for (char i = &#39;A&#39;; i &lt;= &#39;Z&#39;; i++)
  90. {
  91. result[left] = i;
  92. //printf(&quot;%c&quot;, i);
  93. left++;
  94. }
  95. // Now left is 26, reset it to 0, because result[0] is &#39;A&#39;
  96. left = 0;
  97. // n is execute times, need to let result[51] = result[0] = &#39;A&#39;, and result[50] = result[1] = &#39;B&#39; ......
  98. //so right-- and left++
  99. for (n = 0; n &lt; 26; n++)
  100. {
  101. result[right] = result[left];
  102. right--;
  103. left++;
  104. }
  105. return result;
  106. }
  107. int main()
  108. {
  109. char* str = problem58();
  110. printf(&quot;str&#39;s length is %ld\n&quot;,strlen(str));
  111. printf(&quot;%s\n&quot;,str);
  112. free(str);
  113. return 0;
  114. }
  115. &#39;)0.
  116.     // So result[0] will be 0, result[1] will be 0 ...... result[52] will be 0
  117.     for(int m = 0; m &lt; 53;m++){
  118.         result[m] = 0;
  119.     }
  120.     for (char i = &#39;A&#39;; i &lt;= &#39;Z&#39;; i++)
  121.     {
  122.         result[left] = i;
  123.         //printf(&quot;%c&quot;, i);
  124.         left++;
  125.     }
  126.     // Now left is 26, reset it to 0, because result[0] is &#39;A&#39;
  127.     left = 0;
  128.     // n is execute times, need to let result[51] = result[0] = &#39;A&#39;, and result[50] = result[1] = &#39;B&#39; ......
  129.     //so right-- and left++
  130.     for (n = 0; n &lt; 26; n++)
  131.     {
  132.         result[right] = result[left];
  133.         right--;
  134.         left++;
  135.     }
  136.     return result;
  137. }
  138. int main()
  139. {
  140.     char* str = problem58();
  141.     printf(&quot;str&#39;s length is %ld\n&quot;,strlen(str));
  142.     printf(&quot;%s\n&quot;,str);
  143.     free(str);
  144.     return 0;
  145. }
  146. &#39;(0), so add 1 is 53.
  147. char* result = (char*)malloc(53 * sizeof(char));
  148. // Check if it is possible to dynamically allocate memory for 53 variables of type &quot;char&quot;
  149. // You can also write like this: if(result == NULL)
  150. if(NULL == result){
  151. printf(&quot;No space\n&quot;);
  152. exit(1);
  153. }
  154. // Initial result with 0,because string end with (&#39;
    #include &lt;stdio.h&gt;
  155. #include &lt;string.h&gt;
  156. #include &lt;stdlib.h&gt;
  157. char* problem58()
  158. {
  159.     int n = 0;
  160.     int left = 0, right = 52 - 1;
  161.     // A---&gt;Z and Z---&gt;A total number is 52, because string end with &#39;
    #include &lt;stdio.h&gt;
  162. #include &lt;string.h&gt;
  163. #include &lt;stdlib.h&gt;
  164. char* problem58()
  165. {
  166. int n = 0;
  167. int left = 0, right = 52 - 1;
  168. // A---&gt;Z and Z---&gt;A total number is 52, because string end with &#39;\0&#39;(0), so add 1 is 53.
  169. char* result = (char*)malloc(53 * sizeof(char));
  170. // Check if it is possible to dynamically allocate memory for 53 variables of type &quot;char&quot;
  171. // You can also write like this: if(result == NULL)
  172. if(NULL == result){
  173. printf(&quot;No space\n&quot;);
  174. exit(1);
  175. }
  176. // Initial result with 0,because string end with (&#39;\0&#39;)0.
  177. // So result[0] will be 0, result[1] will be 0 ...... result[52] will be 0
  178. for(int m = 0; m &lt; 53;m++){
  179. result[m] = 0;
  180. }
  181. for (char i = &#39;A&#39;; i &lt;= &#39;Z&#39;; i++)
  182. {
  183. result[left] = i;
  184. //printf(&quot;%c&quot;, i);
  185. left++;
  186. }
  187. // Now left is 26, reset it to 0, because result[0] is &#39;A&#39;
  188. left = 0;
  189. // n is execute times, need to let result[51] = result[0] = &#39;A&#39;, and result[50] = result[1] = &#39;B&#39; ......
  190. //so right-- and left++
  191. for (n = 0; n &lt; 26; n++)
  192. {
  193. result[right] = result[left];
  194. right--;
  195. left++;
  196. }
  197. return result;
  198. }
  199. int main()
  200. {
  201. char* str = problem58();
  202. printf(&quot;str&#39;s length is %ld\n&quot;,strlen(str));
  203. printf(&quot;%s\n&quot;,str);
  204. free(str);
  205. return 0;
  206. }
  207. &#39;(0), so add 1 is 53.
  208.     char* result = (char*)malloc(53 * sizeof(char));
  209.     // Check if it is possible to dynamically allocate memory for 53 variables of type &quot;char&quot;
  210.     // You can also write like this: if(result == NULL)
  211.     if(NULL == result){
  212.         printf(&quot;No space\n&quot;);
  213.         exit(1);
  214.     }
  215.     // Initial result with 0,because string end with (&#39;
    #include &lt;stdio.h&gt;
  216. #include &lt;string.h&gt;
  217. #include &lt;stdlib.h&gt;
  218. char* problem58()
  219. {
  220. int n = 0;
  221. int left = 0, right = 52 - 1;
  222. // A---&gt;Z and Z---&gt;A total number is 52, because string end with &#39;\0&#39;(0), so add 1 is 53.
  223. char* result = (char*)malloc(53 * sizeof(char));
  224. // Check if it is possible to dynamically allocate memory for 53 variables of type &quot;char&quot;
  225. // You can also write like this: if(result == NULL)
  226. if(NULL == result){
  227. printf(&quot;No space\n&quot;);
  228. exit(1);
  229. }
  230. // Initial result with 0,because string end with (&#39;\0&#39;)0.
  231. // So result[0] will be 0, result[1] will be 0 ...... result[52] will be 0
  232. for(int m = 0; m &lt; 53;m++){
  233. result[m] = 0;
  234. }
  235. for (char i = &#39;A&#39;; i &lt;= &#39;Z&#39;; i++)
  236. {
  237. result[left] = i;
  238. //printf(&quot;%c&quot;, i);
  239. left++;
  240. }
  241. // Now left is 26, reset it to 0, because result[0] is &#39;A&#39;
  242. left = 0;
  243. // n is execute times, need to let result[51] = result[0] = &#39;A&#39;, and result[50] = result[1] = &#39;B&#39; ......
  244. //so right-- and left++
  245. for (n = 0; n &lt; 26; n++)
  246. {
  247. result[right] = result[left];
  248. right--;
  249. left++;
  250. }
  251. return result;
  252. }
  253. int main()
  254. {
  255. char* str = problem58();
  256. printf(&quot;str&#39;s length is %ld\n&quot;,strlen(str));
  257. printf(&quot;%s\n&quot;,str);
  258. free(str);
  259. return 0;
  260. }
  261. &#39;)0.
  262.     // So result[0] will be 0, result[1] will be 0 ...... result[52] will be 0
  263.     for(int m = 0; m &lt; 53;m++){
  264.         result[m] = 0;
  265.     }
  266.     for (char i = &#39;A&#39;; i &lt;= &#39;Z&#39;; i++)
  267.     {
  268.         result[left] = i;
  269.         //printf(&quot;%c&quot;, i);
  270.         left++;
  271.     }
  272.     // Now left is 26, reset it to 0, because result[0] is &#39;A&#39;
  273.     left = 0;
  274.     // n is execute times, need to let result[51] = result[0] = &#39;A&#39;, and result[50] = result[1] = &#39;B&#39; ......
  275.     //so right-- and left++
  276.     for (n = 0; n &lt; 26; n++)
  277.     {
  278.         result[right] = result[left];
  279.         right--;
  280.         left++;
  281.     }
  282.     return result;
  283. }
  284. int main()
  285. {
  286.     char* str = problem58();
  287.     printf(&quot;str&#39;s length is %ld\n&quot;,strlen(str));
  288.     printf(&quot;%s\n&quot;,str);
  289.     free(str);
  290.     return 0;
  291. }
  292. &#39;)0.
  293. // So result[0] will be 0, result[1] will be 0 ...... result[52] will be 0
  294. for(int m = 0; m &lt; 53;m++){
  295. result[m] = 0;
  296. }
  297. for (char i = &#39;A&#39;; i &lt;= &#39;Z&#39;; i++)
  298. {
  299. result[left] = i;
  300. //printf(&quot;%c&quot;, i);
  301. left++;
  302. }
  303. // Now left is 26, reset it to 0, because result[0] is &#39;A&#39;
  304. left = 0;
  305. // n is execute times, need to let result[51] = result[0] = &#39;A&#39;, and result[50] = result[1] = &#39;B&#39; ......
  306. //so right-- and left++
  307. for (n = 0; n &lt; 26; n++)
  308. {
  309. result[right] = result[left];
  310. right--;
  311. left++;
  312. }
  313. return result;
  314. }
  315. int main()
  316. {
  317. char* str = problem58();
  318. printf(&quot;str&#39;s length is %ld\n&quot;,strlen(str));
  319. printf(&quot;%s\n&quot;,str);
  320. free(str);
  321. return 0;
  322. }

Run it will output

  1. str&#39;s length is 52
  2. ABCDEFGHIJKLMNOPQRSTUVWXYZZYXWVUTSRQPONMLKJIHGFEDCBA

答案2

得分: -1

这个for循环

  1. for (int i = left; i &lt; right; i++)
  2. {
  3. temp = result[left];
  4. result[left] = result[right];
  5. //..

由于动态分配的数组未初始化,因此调用未定义行为,结果是表达式 result[left]result[right] 具有不确定的值。数组中没有要交换的内容。数组必须填充为字符。

而且,无论如何,该数组都不包含字符串,因为它没有以终止零字符 '\0' 结尾。

如果我正确理解了你的任务,你需要类似以下的代码。请注意,该函数可以以不同的方式实现。

例如,

  1. char * problem58( void )
  2. {
  3. char *s = malloc( 2 * ('Z' - 'A' + 1) + 1 );
  4. if ( s != NULL )
  5. {
  6. char *p = s;
  7. char c = 'A';
  8. do
  9. {
  10. *p++ = c;
  11. } while ( c++ != 'Z' );
  12. while ( c != 'A' )
  13. {
  14. *p++ = --c;
  15. }
  16. *p = '
    char * problem58( void )
  17. {
  18.     char *s = malloc( 2 * ('Z' - 'A' + 1) + 1 );
  19.     if ( s != NULL )
  20.     {
  21.         char *p = s;
  22.         char c = 'A';
  23.         do
  24.         {
  25.             *p++ = c;
  26.         } while ( c++ != 'Z' );
  27.         while ( c != 'A' )
  28.         {
  29.             *p++ = --c;
  30.         }
  31.         *p = '\0';
  32.     }
  33.     return s;
  34. }
  35. ';
  36. }
  37. return s;
  38. }

或者

  1. char * problem58( void )
  2. {
  3. size_t n = 2 * ('Z' - 'A' + 1);
  4. char *s = malloc( n + 1 );
  5. if (s != NULL)
  6. {
  7. char *first = s, *last = s + n;
  8. *last = '
    char * problem58( void )
  9. {
  10.     size_t n = 2 * ('Z' - 'A' + 1);
  11.     char *s = malloc( n + 1 );
  12.     if (s != NULL)
  13.     {
  14.         char *first = s, *last = s + n;
  15.         *last = '\0';
  16.         char c = 'A';
  17.         do
  18.         {
  19.             *first++ = *--last = c;
  20.         } while (c++ != 'Z');
  21.     }
  22.     return s;
  23. }
  24. ';
  25. char c = 'A';
  26. do
  27. {
  28. *first++ = *--last = c;
  29. } while (c++ != 'Z');
  30. }
  31. return s;
  32. }

然后在主函数中调用该函数,如下所示:

  1. char *s = problem58();
  2. if ( s != NULL ) puts( s );
  3. free( s );

调用该函数的预期输出是:

  1. ABCDEFGHIJKLMNOPQRSTUVWXYZZYXWVUTSRQPONMLKJIHGFEDCBA
英文:

This for loop

  1. for (int i = left; i &lt; right; i++)
  2. {
  3. temp = result[left];
  4. result[left] = result[right];
  5. //..

invokes undefined behavior because the dynamically allocated array was not initialized and as a result the expressions result[left] and result[right] have indeterminate values. There is nothing to swap in the array. The array has to be filled with characters.

Also the array in any case does not contain a string because it is not appended with the terminating zero character &#39;\0&#39;;.

If I have understood the assignment correctly then you need something like the following. Pay attention to that the function can be implemented in different ways.

For example

  1. char * problem58( void )
  2. {
  3. char *s = malloc( 2 * ( &#39;Z&#39; - &#39;A&#39; + 1 ) + 1 );
  4. if ( s != NULL )
  5. {
  6. char *p = s;
  7. char c = &#39;A&#39;;
  8. do
  9. {
  10. *p++ = c;
  11. } while ( c++ != &#39;Z&#39; );
  12. while ( c != &#39;A&#39; )
  13. {
  14. *p++ = --c;
  15. }
  16. *p = &#39;
    char * problem58( void )
  17. {
  18. char *s = malloc( 2 * ( &#39;Z&#39; - &#39;A&#39; + 1 ) + 1 );
  19. if ( s != NULL )
  20. {
  21. char *p = s;
  22. char c = &#39;A&#39;;
  23. do
  24. {
  25. *p++ = c;
  26. } while ( c++ != &#39;Z&#39; );
  27. while ( c != &#39;A&#39; )
  28. {
  29. *p++ = --c;
  30. }
  31. *p = &#39;\0&#39;;
  32. }
  33. return s;
  34. }
  35. &#39;;
  36. }
  37. return s;
  38. }

or

  1. char * problem58( void )
  2. {
  3. size_t n = 2 * ( &#39;Z&#39; - &#39;A&#39; + 1 );
  4. char *s = malloc( n + 1 );
  5. if (s != NULL)
  6. {
  7. char *first = s, *last = s + n;
  8. *last = &#39;
    char * problem58( void )
  9. {
  10. size_t n = 2 * ( &#39;Z&#39; - &#39;A&#39; + 1 );
  11. char *s = malloc( n + 1 );
  12. if (s != NULL)
  13. {
  14. char *first = s, *last = s + n;
  15. *last = &#39;\0&#39;;
  16. char c = &#39;A&#39;;
  17. do
  18. {
  19. *first++ = *--last = c;
  20. } while (c++ != &#39;Z&#39;);
  21. }
  22. return s;
  23. }
  24. &#39;;
  25. char c = &#39;A&#39;;
  26. do
  27. {
  28. *first++ = *--last = c;
  29. } while (c++ != &#39;Z&#39;);
  30. }
  31. return s;
  32. }

And the function is called in main like

  1. char *s = problem58();
  2. if ( s != NULL ) puts( s );
  3. free( s );

The expected output of a call of the function is

  1. ABCDEFGHIJKLMNOPQRSTUVWXYZZYXWVUTSRQPONMLKJIHGFEDCBA

huangapple
  • 本文由 发表于 2023年6月25日 18:38:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/76549982.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定