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

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

how to reverse string in array in C

问题

我已翻译以下代码段:

char *problem58()
{
    char temp, *result = (char *)malloc(52 * sizeof(char));
    int left = 0, right = 52 - 1;

    for (char i = 'A'; i <= 'Z'; i++)
    {
        result[left] = i;
        printf("%c", i);
        left++;
    }
    // 这是我用来反转字符串的方法
    for (int i = left; i < right; i++)
    {
        temp = result[left];
        result[left] = result[right];
        result[right] = temp;
        printf("%c", result[right]);
        right--;
    }
    return result;
}

运行此行代码在终端中输出: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.

char *problem58()
{
    char temp, *result = (char *)malloc(52 * sizeof(char));
    int left = 0, right = 52 - 1;

    for (char i = &#39;A&#39;; i &lt;= &#39;Z&#39;; i++)
    {
        result[left] = i;
        printf(&quot;%c&quot;, i);
        left++;
    }
    // this is my idea to reverse this string
    for (int i = left; i &lt; right; i++)
    {
        temp = result[left];
        result[left] = result[right];
        result[right] = temp;
        printf(&quot;%c&quot;, result[right]);
        right--;
    }
    return result;
}

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

答案1

得分: 0

这是我的答案

#include &lt;stdio.h&gt;
#include &lt;string.h&gt;
#include &lt;stdlib.h&gt;

char* problem58()
{
    int n = 0;
    int left = 0, right = 52 - 1;

    // A---&gt;Z and Z---&gt;A total number is 52, because string end with &#39;
#include &lt;stdio.h&gt;
#include &lt;string.h&gt;
#include &lt;stdlib.h&gt;

char* problem58()
{
    int n = 0;
    int left = 0, right = 52 - 1;

    // A---&gt;Z and Z---&gt;A total number is 52, because string end with &#39;
#include &lt;stdio.h&gt;
#include &lt;string.h&gt;
#include &lt;stdlib.h&gt;
char* problem58()
{
int n = 0;
int left = 0, right = 52 - 1;
// A---&gt;Z and Z---&gt;A total number is 52, because string end with &#39;\0&#39;(0), so add 1 is 53.
char* result = (char*)malloc(53 * sizeof(char));
// Check if it is possible to dynamically allocate memory for 53 variables of type &quot;char&quot;
// You can also write like this: if(result == NULL)
if(NULL == result){
printf(&quot;No space\n&quot;);
exit(1);
}
// Initial result with 0,because string end with (&#39;\0&#39;)0.
// So result[0] will be 0, result[1] will be 0 ...... result[52] will be 0
for(int m = 0; m &lt; 53;m++){
result[m] = 0;
}
for (char i = &#39;A&#39;; i &lt;= &#39;Z&#39;; i++)
{
result[left] = i;
//printf(&quot;%c&quot;, i);
left++;
}
// Now left is 26, reset it to 0, because result[0] is &#39;A&#39;
left = 0;
// n is execute times, need to let result[51] = result[0] = &#39;A&#39;, and result[50] = result[1] = &#39;B&#39; ......
//so right-- and left++
for (n = 0; n &lt; 26; n++)
{
result[right] = result[left];
right--;
left++;
}
return result;
}
int main()
{
char* str = problem58();
printf(&quot;str&#39;s length is %ld\n&quot;,strlen(str));
printf(&quot;%s\n&quot;,str);
free(str);
return 0;
}
&#39;(0), so add 1 is 53. char* result = (char*)malloc(53 * sizeof(char)); // Check if it is possible to dynamically allocate memory for 53 variables of type &quot;char&quot; // You can also write like this: if(result == NULL) if(NULL == result){ printf(&quot;No space\n&quot;); exit(1); } // Initial result with 0,because string end with (&#39;
#include &lt;stdio.h&gt;
#include &lt;string.h&gt;
#include &lt;stdlib.h&gt;
char* problem58()
{
int n = 0;
int left = 0, right = 52 - 1;
// A---&gt;Z and Z---&gt;A total number is 52, because string end with &#39;\0&#39;(0), so add 1 is 53.
char* result = (char*)malloc(53 * sizeof(char));
// Check if it is possible to dynamically allocate memory for 53 variables of type &quot;char&quot;
// You can also write like this: if(result == NULL)
if(NULL == result){
printf(&quot;No space\n&quot;);
exit(1);
}
// Initial result with 0,because string end with (&#39;\0&#39;)0.
// So result[0] will be 0, result[1] will be 0 ...... result[52] will be 0
for(int m = 0; m &lt; 53;m++){
result[m] = 0;
}
for (char i = &#39;A&#39;; i &lt;= &#39;Z&#39;; i++)
{
result[left] = i;
//printf(&quot;%c&quot;, i);
left++;
}
// Now left is 26, reset it to 0, because result[0] is &#39;A&#39;
left = 0;
// n is execute times, need to let result[51] = result[0] = &#39;A&#39;, and result[50] = result[1] = &#39;B&#39; ......
//so right-- and left++
for (n = 0; n &lt; 26; n++)
{
result[right] = result[left];
right--;
left++;
}
return result;
}
int main()
{
char* str = problem58();
printf(&quot;str&#39;s length is %ld\n&quot;,strlen(str));
printf(&quot;%s\n&quot;,str);
free(str);
return 0;
}
&#39;)0. // So result[0] will be 0, result[1] will be 0 ...... result[52] will be 0 for(int m = 0; m &lt; 53;m++){ result[m] = 0; } for (char i = &#39;A&#39;; i &lt;= &#39;Z&#39;; i++) { result[left] = i; //printf(&quot;%c&quot;, i); left++; } // Now left is 26, reset it to 0, because result[0] is &#39;A&#39; left = 0; // n is execute times, need to let result[51] = result[0] = &#39;A&#39;, and result[50] = result[1] = &#39;B&#39; ...... //so right-- and left++ for (n = 0; n &lt; 26; n++) { result[right] = result[left]; right--; left++; } return result; } int main() { char* str = problem58(); printf(&quot;str&#39;s length is %ld\n&quot;,strlen(str)); printf(&quot;%s\n&quot;,str); free(str); return 0; }
&#39;(0), so add 1 is 53. char* result = (char*)malloc(53 * sizeof(char)); // Check if it is possible to dynamically allocate memory for 53 variables of type &quot;char&quot; // You can also write like this: if(result == NULL) if(NULL == result){ printf(&quot;No space\n&quot;); exit(1); } // Initial result with 0,because string end with (&#39;
#include &lt;stdio.h&gt;
#include &lt;string.h&gt;
#include &lt;stdlib.h&gt;

char* problem58()
{
    int n = 0;
    int left = 0, right = 52 - 1;

    // A---&gt;Z and Z---&gt;A total number is 52, because string end with &#39;
#include &lt;stdio.h&gt;
#include &lt;string.h&gt;
#include &lt;stdlib.h&gt;
char* problem58()
{
int n = 0;
int left = 0, right = 52 - 1;
// A---&gt;Z and Z---&gt;A total number is 52, because string end with &#39;\0&#39;(0), so add 1 is 53.
char* result = (char*)malloc(53 * sizeof(char));
// Check if it is possible to dynamically allocate memory for 53 variables of type &quot;char&quot;
// You can also write like this: if(result == NULL)
if(NULL == result){
printf(&quot;No space\n&quot;);
exit(1);
}
// Initial result with 0,because string end with (&#39;\0&#39;)0.
// So result[0] will be 0, result[1] will be 0 ...... result[52] will be 0
for(int m = 0; m &lt; 53;m++){
result[m] = 0;
}
for (char i = &#39;A&#39;; i &lt;= &#39;Z&#39;; i++)
{
result[left] = i;
//printf(&quot;%c&quot;, i);
left++;
}
// Now left is 26, reset it to 0, because result[0] is &#39;A&#39;
left = 0;
// n is execute times, need to let result[51] = result[0] = &#39;A&#39;, and result[50] = result[1] = &#39;B&#39; ......
//so right-- and left++
for (n = 0; n &lt; 26; n++)
{
result[right] = result[left];
right--;
left++;
}
return result;
}
int main()
{
char* str = problem58();
printf(&quot;str&#39;s length is %ld\n&quot;,strlen(str));
printf(&quot;%s\n&quot;,str);
free(str);
return 0;
}
&#39;(0), so add 1 is 53. char* result = (char*)malloc(53 * sizeof(char)); // Check if it is possible to dynamically allocate memory for 53 variables of type &quot;char&quot; // You can also write like this: if(result == NULL) if(NULL == result){ printf(&quot;No space\n&quot;); exit(1); } // Initial result with 0,because string end with (&#39;
#include &lt;stdio.h&gt;
#include &lt;string.h&gt;
#include &lt;stdlib.h&gt;
char* problem58()
{
int n = 0;
int left = 0, right = 52 - 1;
// A---&gt;Z and Z---&gt;A total number is 52, because string end with &#39;\0&#39;(0), so add 1 is 53.
char* result = (char*)malloc(53 * sizeof(char));
// Check if it is possible to dynamically allocate memory for 53 variables of type &quot;char&quot;
// You can also write like this: if(result == NULL)
if(NULL == result){
printf(&quot;No space\n&quot;);
exit(1);
}
// Initial result with 0,because string end with (&#39;\0&#39;)0.
// So result[0] will be 0, result[1] will be 0 ...... result[52] will be 0
for(int m = 0; m &lt; 53;m++){
result[m] = 0;
}
for (char i = &#39;A&#39;; i &lt;= &#39;Z&#39;; i++)
{
result[left] = i;
//printf(&quot;%c&quot;, i);
left++;
}
// Now left is 26, reset it to 0, because result[0] is &#39;A&#39;
left = 0;
// n is execute times, need to let result[51] = result[0] = &#39;A&#39;, and result[50] = result[1] = &#39;B&#39; ......
//so right-- and left++
for (n = 0; n &lt; 26; n++)
{
result[right] = result[left];
right--;
left++;
}
return result;
}
int main()
{
char* str = problem58();
printf(&quot;str&#39;s length is %ld\n&quot;,strlen(str));
printf(&quot;%s\n&quot;,str);
free(str);
return 0;
}
&#39;)0. // So result[0] will be 0, result[1] will be 0 ...... result[52] will be 0 for(int m = 0; m &lt; 53;m++){ result[m] = 0; } for (char i = &#39;A&#39;; i &lt;= &#39;Z&#39;; i++) { result[left] = i; //printf(&quot;%c&quot;, i); left++; } // Now left is 26, reset it to 0, because result[0] is &#39;A&#39; left = 0; // n is execute times, need to let result[51] = result[0] = &#39;A&#39;, and result[50] = result[1] = &#39;B&#39; ...... //so right-- and left++ for (n = 0; n &lt; 26; n++) { result[right] = result[left]; right--; left++; } return result; } int main() { char* str = problem58(); printf(&quot;str&#39;s length is %ld\n&quot;,strlen(str)); printf(&quot;%s\n&quot;,str); free(str); return 0; }
&#39;)0. // So result[0] will be 0, result[1] will be 0 ...... result[52] will be 0 for(int m = 0; m &lt; 53;m++){ result[m] = 0; } for (char i = &#39;A&#39;; i &lt;= &#39;Z&#39;; i++) { result[left] = i; //printf(&quot;%c&quot;, i); left++; } // Now left is 26, reset it to 0, because result[0] is &#39;A&#39; left = 0; // n is execute times, need to let result[51] = result[0] = &#39;A&#39;, and result[50] = result[1] = &#39;B&#39; ...... //so right-- and left++ for (n = 0; n &lt; 26; n++) { result[right] = result[left]; right--; left++; } return result; } int main() { char* str = problem58(); printf(&quot;str&#39;s length is %ld\n&quot;,strlen(str)); printf(&quot;%s\n&quot;,str); free(str); return 0; }

运行它会输出

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

Here is my answer

#include &lt;stdio.h&gt;
#include &lt;string.h&gt;
#include &lt;stdlib.h&gt;

char* problem58()
{
    int n = 0;
    int left = 0, right = 52 - 1;

    // A---&gt;Z and Z---&gt;A total number is 52, because string end with &#39;
#include &lt;stdio.h&gt;
#include &lt;string.h&gt;
#include &lt;stdlib.h&gt;

char* problem58()
{
    int n = 0;
    int left = 0, right = 52 - 1;

    // A---&gt;Z and Z---&gt;A total number is 52, because string end with &#39;
#include &lt;stdio.h&gt;
#include &lt;string.h&gt;
#include &lt;stdlib.h&gt;
char* problem58()
{
int n = 0;
int left = 0, right = 52 - 1;
// A---&gt;Z and Z---&gt;A total number is 52, because string end with &#39;\0&#39;(0), so add 1 is 53.
char* result = (char*)malloc(53 * sizeof(char));
// Check if it is possible to dynamically allocate memory for 53 variables of type &quot;char&quot;
// You can also write like this: if(result == NULL)
if(NULL == result){
printf(&quot;No space\n&quot;);
exit(1);
}
// Initial result with 0,because string end with (&#39;\0&#39;)0.
// So result[0] will be 0, result[1] will be 0 ...... result[52] will be 0
for(int m = 0; m &lt; 53;m++){
result[m] = 0;
}
for (char i = &#39;A&#39;; i &lt;= &#39;Z&#39;; i++)
{
result[left] = i;
//printf(&quot;%c&quot;, i);
left++;
}
// Now left is 26, reset it to 0, because result[0] is &#39;A&#39;
left = 0;
// n is execute times, need to let result[51] = result[0] = &#39;A&#39;, and result[50] = result[1] = &#39;B&#39; ......
//so right-- and left++
for (n = 0; n &lt; 26; n++)
{
result[right] = result[left];
right--;
left++;
}
return result;
}
int main()
{
char* str = problem58();
printf(&quot;str&#39;s length is %ld\n&quot;,strlen(str));
printf(&quot;%s\n&quot;,str);
free(str);
return 0;
}
&#39;(0), so add 1 is 53. char* result = (char*)malloc(53 * sizeof(char)); // Check if it is possible to dynamically allocate memory for 53 variables of type &quot;char&quot; // You can also write like this: if(result == NULL) if(NULL == result){ printf(&quot;No space\n&quot;); exit(1); } // Initial result with 0,because string end with (&#39;
#include &lt;stdio.h&gt;
#include &lt;string.h&gt;
#include &lt;stdlib.h&gt;
char* problem58()
{
int n = 0;
int left = 0, right = 52 - 1;
// A---&gt;Z and Z---&gt;A total number is 52, because string end with &#39;\0&#39;(0), so add 1 is 53.
char* result = (char*)malloc(53 * sizeof(char));
// Check if it is possible to dynamically allocate memory for 53 variables of type &quot;char&quot;
// You can also write like this: if(result == NULL)
if(NULL == result){
printf(&quot;No space\n&quot;);
exit(1);
}
// Initial result with 0,because string end with (&#39;\0&#39;)0.
// So result[0] will be 0, result[1] will be 0 ...... result[52] will be 0
for(int m = 0; m &lt; 53;m++){
result[m] = 0;
}
for (char i = &#39;A&#39;; i &lt;= &#39;Z&#39;; i++)
{
result[left] = i;
//printf(&quot;%c&quot;, i);
left++;
}
// Now left is 26, reset it to 0, because result[0] is &#39;A&#39;
left = 0;
// n is execute times, need to let result[51] = result[0] = &#39;A&#39;, and result[50] = result[1] = &#39;B&#39; ......
//so right-- and left++
for (n = 0; n &lt; 26; n++)
{
result[right] = result[left];
right--;
left++;
}
return result;
}
int main()
{
char* str = problem58();
printf(&quot;str&#39;s length is %ld\n&quot;,strlen(str));
printf(&quot;%s\n&quot;,str);
free(str);
return 0;
}
&#39;)0. // So result[0] will be 0, result[1] will be 0 ...... result[52] will be 0 for(int m = 0; m &lt; 53;m++){ result[m] = 0; } for (char i = &#39;A&#39;; i &lt;= &#39;Z&#39;; i++) { result[left] = i; //printf(&quot;%c&quot;, i); left++; } // Now left is 26, reset it to 0, because result[0] is &#39;A&#39; left = 0; // n is execute times, need to let result[51] = result[0] = &#39;A&#39;, and result[50] = result[1] = &#39;B&#39; ...... //so right-- and left++ for (n = 0; n &lt; 26; n++) { result[right] = result[left]; right--; left++; } return result; } int main() { char* str = problem58(); printf(&quot;str&#39;s length is %ld\n&quot;,strlen(str)); printf(&quot;%s\n&quot;,str); free(str); return 0; }
&#39;(0), so add 1 is 53. char* result = (char*)malloc(53 * sizeof(char)); // Check if it is possible to dynamically allocate memory for 53 variables of type &quot;char&quot; // You can also write like this: if(result == NULL) if(NULL == result){ printf(&quot;No space\n&quot;); exit(1); } // Initial result with 0,because string end with (&#39;
#include &lt;stdio.h&gt;
#include &lt;string.h&gt;
#include &lt;stdlib.h&gt;

char* problem58()
{
    int n = 0;
    int left = 0, right = 52 - 1;

    // A---&gt;Z and Z---&gt;A total number is 52, because string end with &#39;
#include &lt;stdio.h&gt;
#include &lt;string.h&gt;
#include &lt;stdlib.h&gt;
char* problem58()
{
int n = 0;
int left = 0, right = 52 - 1;
// A---&gt;Z and Z---&gt;A total number is 52, because string end with &#39;\0&#39;(0), so add 1 is 53.
char* result = (char*)malloc(53 * sizeof(char));
// Check if it is possible to dynamically allocate memory for 53 variables of type &quot;char&quot;
// You can also write like this: if(result == NULL)
if(NULL == result){
printf(&quot;No space\n&quot;);
exit(1);
}
// Initial result with 0,because string end with (&#39;\0&#39;)0.
// So result[0] will be 0, result[1] will be 0 ...... result[52] will be 0
for(int m = 0; m &lt; 53;m++){
result[m] = 0;
}
for (char i = &#39;A&#39;; i &lt;= &#39;Z&#39;; i++)
{
result[left] = i;
//printf(&quot;%c&quot;, i);
left++;
}
// Now left is 26, reset it to 0, because result[0] is &#39;A&#39;
left = 0;
// n is execute times, need to let result[51] = result[0] = &#39;A&#39;, and result[50] = result[1] = &#39;B&#39; ......
//so right-- and left++
for (n = 0; n &lt; 26; n++)
{
result[right] = result[left];
right--;
left++;
}
return result;
}
int main()
{
char* str = problem58();
printf(&quot;str&#39;s length is %ld\n&quot;,strlen(str));
printf(&quot;%s\n&quot;,str);
free(str);
return 0;
}
&#39;(0), so add 1 is 53. char* result = (char*)malloc(53 * sizeof(char)); // Check if it is possible to dynamically allocate memory for 53 variables of type &quot;char&quot; // You can also write like this: if(result == NULL) if(NULL == result){ printf(&quot;No space\n&quot;); exit(1); } // Initial result with 0,because string end with (&#39;
#include &lt;stdio.h&gt;
#include &lt;string.h&gt;
#include &lt;stdlib.h&gt;
char* problem58()
{
int n = 0;
int left = 0, right = 52 - 1;
// A---&gt;Z and Z---&gt;A total number is 52, because string end with &#39;\0&#39;(0), so add 1 is 53.
char* result = (char*)malloc(53 * sizeof(char));
// Check if it is possible to dynamically allocate memory for 53 variables of type &quot;char&quot;
// You can also write like this: if(result == NULL)
if(NULL == result){
printf(&quot;No space\n&quot;);
exit(1);
}
// Initial result with 0,because string end with (&#39;\0&#39;)0.
// So result[0] will be 0, result[1] will be 0 ...... result[52] will be 0
for(int m = 0; m &lt; 53;m++){
result[m] = 0;
}
for (char i = &#39;A&#39;; i &lt;= &#39;Z&#39;; i++)
{
result[left] = i;
//printf(&quot;%c&quot;, i);
left++;
}
// Now left is 26, reset it to 0, because result[0] is &#39;A&#39;
left = 0;
// n is execute times, need to let result[51] = result[0] = &#39;A&#39;, and result[50] = result[1] = &#39;B&#39; ......
//so right-- and left++
for (n = 0; n &lt; 26; n++)
{
result[right] = result[left];
right--;
left++;
}
return result;
}
int main()
{
char* str = problem58();
printf(&quot;str&#39;s length is %ld\n&quot;,strlen(str));
printf(&quot;%s\n&quot;,str);
free(str);
return 0;
}
&#39;)0. // So result[0] will be 0, result[1] will be 0 ...... result[52] will be 0 for(int m = 0; m &lt; 53;m++){ result[m] = 0; } for (char i = &#39;A&#39;; i &lt;= &#39;Z&#39;; i++) { result[left] = i; //printf(&quot;%c&quot;, i); left++; } // Now left is 26, reset it to 0, because result[0] is &#39;A&#39; left = 0; // n is execute times, need to let result[51] = result[0] = &#39;A&#39;, and result[50] = result[1] = &#39;B&#39; ...... //so right-- and left++ for (n = 0; n &lt; 26; n++) { result[right] = result[left]; right--; left++; } return result; } int main() { char* str = problem58(); printf(&quot;str&#39;s length is %ld\n&quot;,strlen(str)); printf(&quot;%s\n&quot;,str); free(str); return 0; }
&#39;)0. // So result[0] will be 0, result[1] will be 0 ...... result[52] will be 0 for(int m = 0; m &lt; 53;m++){ result[m] = 0; } for (char i = &#39;A&#39;; i &lt;= &#39;Z&#39;; i++) { result[left] = i; //printf(&quot;%c&quot;, i); left++; } // Now left is 26, reset it to 0, because result[0] is &#39;A&#39; left = 0; // n is execute times, need to let result[51] = result[0] = &#39;A&#39;, and result[50] = result[1] = &#39;B&#39; ...... //so right-- and left++ for (n = 0; n &lt; 26; n++) { result[right] = result[left]; right--; left++; } return result; } int main() { char* str = problem58(); printf(&quot;str&#39;s length is %ld\n&quot;,strlen(str)); printf(&quot;%s\n&quot;,str); free(str); return 0; }

Run it will output

str&#39;s length is 52
ABCDEFGHIJKLMNOPQRSTUVWXYZZYXWVUTSRQPONMLKJIHGFEDCBA

答案2

得分: -1

这个for循环

for (int i = left; i &lt; right; i++)
{
    temp = result[left];
    result[left] = result[right];
    //..

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

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

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

例如,

char * problem58( void )
{
    char *s = malloc( 2 * ('Z' - 'A' + 1) + 1 );

    if ( s != NULL )
    {
        char *p = s;
        char c = 'A';

        do
        {
            *p++ = c;
        } while ( c++ != 'Z' );

        while ( c != 'A' )
        {
            *p++ = --c;
        }

        *p = '
char * problem58( void )
{
    char *s = malloc( 2 * ('Z' - 'A' + 1) + 1 );

    if ( s != NULL )
    {
        char *p = s;
        char c = 'A';

        do
        {
            *p++ = c;
        } while ( c++ != 'Z' );

        while ( c != 'A' )
        {
            *p++ = --c;
        }

        *p = '\0';
    }

    return s;
}
'
;
} return s; }

或者

char * problem58( void )
{
    size_t n = 2 * ('Z' - 'A' + 1);

    char *s = malloc( n + 1 );

    if (s != NULL)
    {
        char *first = s, *last = s + n;

        *last = '
char * problem58( void )
{
    size_t n = 2 * ('Z' - 'A' + 1);

    char *s = malloc( n + 1 );

    if (s != NULL)
    {
        char *first = s, *last = s + n;

        *last = '\0';

        char c = 'A';

        do
        {
            *first++ = *--last = c;
        } while (c++ != 'Z');
    }

    return s;
}
'
;
char c = 'A'; do { *first++ = *--last = c; } while (c++ != 'Z'); } return s; }

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

char *s = problem58();

if ( s != NULL ) puts( s );

free( s );

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

ABCDEFGHIJKLMNOPQRSTUVWXYZZYXWVUTSRQPONMLKJIHGFEDCBA
英文:

This for loop

for (int i = left; i &lt; right; i++)
{
    temp = result[left];
    result[left] = result[right];
    //..

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

char * problem58( void )
{
    char *s = malloc( 2 * ( &#39;Z&#39; - &#39;A&#39; + 1 ) + 1 );

    if ( s != NULL )
    {
        char *p = s;
        char c = &#39;A&#39;;

        do
        {
            *p++ = c;
        } while ( c++ != &#39;Z&#39; );

        while ( c != &#39;A&#39; )
        {
            *p++ = --c;
        }

        *p = &#39;
char * problem58( void )
{
char *s = malloc( 2 * ( &#39;Z&#39; - &#39;A&#39; + 1 ) + 1 );
if ( s != NULL )
{
char *p = s;
char c = &#39;A&#39;;
do
{
*p++ = c;
} while ( c++ != &#39;Z&#39; );
while ( c != &#39;A&#39; )
{
*p++ = --c;
}
*p = &#39;\0&#39;;
}
return s;
}
&#39;; } return s; }

or

char * problem58( void )
{
	size_t n = 2 * ( &#39;Z&#39; - &#39;A&#39; + 1 );

	char *s = malloc( n + 1 );

	if (s != NULL)
	{
		char *first = s, *last = s + n;

		*last = &#39;
char * problem58( void )
{
size_t n = 2 * ( &#39;Z&#39; - &#39;A&#39; + 1 );
char *s = malloc( n + 1 );
if (s != NULL)
{
char *first = s, *last = s + n;
*last = &#39;\0&#39;;
char c = &#39;A&#39;;
do
{
*first++ = *--last = c;
} while (c++ != &#39;Z&#39;);
}
return s;
}
&#39;; char c = &#39;A&#39;; do { *first++ = *--last = c; } while (c++ != &#39;Z&#39;); } return s; }

And the function is called in main like

char *s = problem58();

if ( s != NULL ) puts( s );

free( s );

The expected output of a call of the function is

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:

确定