英文:
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 = 'A'; i <= 'Z'; i++)
{
result[left] = i;
printf("%c", i);
left++;
}
// this is my idea to reverse this string
for (int i = left; i < right; i++)
{
temp = result[left];
result[left] = result[right];
result[right] = temp;
printf("%c", result[right]);
right--;
}
return result;
}
line code run in terminal: ABCDEFGHIJKLMNOPQRSTUVWXYZSW\:C=cepSmoC
答案1
得分: 0
这是我的答案
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
char* problem58()
{
int n = 0;
int left = 0, right = 52 - 1;
// A--->Z and Z--->A total number is 52, because string end with '#include <stdio.h>
#include <string.h>
#include <stdlib.h>
char* problem58()
{
int n = 0;
int left = 0, right = 52 - 1;
// A--->Z and Z--->A total number is 52, because string end with '#include <stdio.h>
#include <string.h>
#include <stdlib.h>
char* problem58()
{
int n = 0;
int left = 0, right = 52 - 1;
// A--->Z and Z--->A total number is 52, because string end with '\0'(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 "char"
// You can also write like this: if(result == NULL)
if(NULL == result){
printf("No space\n");
exit(1);
}
// Initial result with 0,because string end with ('\0')0.
// So result[0] will be 0, result[1] will be 0 ...... result[52] will be 0
for(int m = 0; m < 53;m++){
result[m] = 0;
}
for (char i = 'A'; i <= 'Z'; i++)
{
result[left] = i;
//printf("%c", i);
left++;
}
// Now left is 26, reset it to 0, because result[0] is 'A'
left = 0;
// n is execute times, need to let result[51] = result[0] = 'A', and result[50] = result[1] = 'B' ......
//so right-- and left++
for (n = 0; n < 26; n++)
{
result[right] = result[left];
right--;
left++;
}
return result;
}
int main()
{
char* str = problem58();
printf("str's length is %ld\n",strlen(str));
printf("%s\n",str);
free(str);
return 0;
}
'(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 "char"
// You can also write like this: if(result == NULL)
if(NULL == result){
printf("No space\n");
exit(1);
}
// Initial result with 0,because string end with ('#include <stdio.h>
#include <string.h>
#include <stdlib.h>
char* problem58()
{
int n = 0;
int left = 0, right = 52 - 1;
// A--->Z and Z--->A total number is 52, because string end with '\0'(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 "char"
// You can also write like this: if(result == NULL)
if(NULL == result){
printf("No space\n");
exit(1);
}
// Initial result with 0,because string end with ('\0')0.
// So result[0] will be 0, result[1] will be 0 ...... result[52] will be 0
for(int m = 0; m < 53;m++){
result[m] = 0;
}
for (char i = 'A'; i <= 'Z'; i++)
{
result[left] = i;
//printf("%c", i);
left++;
}
// Now left is 26, reset it to 0, because result[0] is 'A'
left = 0;
// n is execute times, need to let result[51] = result[0] = 'A', and result[50] = result[1] = 'B' ......
//so right-- and left++
for (n = 0; n < 26; n++)
{
result[right] = result[left];
right--;
left++;
}
return result;
}
int main()
{
char* str = problem58();
printf("str's length is %ld\n",strlen(str));
printf("%s\n",str);
free(str);
return 0;
}
')0.
// So result[0] will be 0, result[1] will be 0 ...... result[52] will be 0
for(int m = 0; m < 53;m++){
result[m] = 0;
}
for (char i = 'A'; i <= 'Z'; i++)
{
result[left] = i;
//printf("%c", i);
left++;
}
// Now left is 26, reset it to 0, because result[0] is 'A'
left = 0;
// n is execute times, need to let result[51] = result[0] = 'A', and result[50] = result[1] = 'B' ......
//so right-- and left++
for (n = 0; n < 26; n++)
{
result[right] = result[left];
right--;
left++;
}
return result;
}
int main()
{
char* str = problem58();
printf("str's length is %ld\n",strlen(str));
printf("%s\n",str);
free(str);
return 0;
}
'(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 "char"
// You can also write like this: if(result == NULL)
if(NULL == result){
printf("No space\n");
exit(1);
}
// Initial result with 0,because string end with ('#include <stdio.h>
#include <string.h>
#include <stdlib.h>
char* problem58()
{
int n = 0;
int left = 0, right = 52 - 1;
// A--->Z and Z--->A total number is 52, because string end with '#include <stdio.h>
#include <string.h>
#include <stdlib.h>
char* problem58()
{
int n = 0;
int left = 0, right = 52 - 1;
// A--->Z and Z--->A total number is 52, because string end with '\0'(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 "char"
// You can also write like this: if(result == NULL)
if(NULL == result){
printf("No space\n");
exit(1);
}
// Initial result with 0,because string end with ('\0')0.
// So result[0] will be 0, result[1] will be 0 ...... result[52] will be 0
for(int m = 0; m < 53;m++){
result[m] = 0;
}
for (char i = 'A'; i <= 'Z'; i++)
{
result[left] = i;
//printf("%c", i);
left++;
}
// Now left is 26, reset it to 0, because result[0] is 'A'
left = 0;
// n is execute times, need to let result[51] = result[0] = 'A', and result[50] = result[1] = 'B' ......
//so right-- and left++
for (n = 0; n < 26; n++)
{
result[right] = result[left];
right--;
left++;
}
return result;
}
int main()
{
char* str = problem58();
printf("str's length is %ld\n",strlen(str));
printf("%s\n",str);
free(str);
return 0;
}
'(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 "char"
// You can also write like this: if(result == NULL)
if(NULL == result){
printf("No space\n");
exit(1);
}
// Initial result with 0,because string end with ('#include <stdio.h>
#include <string.h>
#include <stdlib.h>
char* problem58()
{
int n = 0;
int left = 0, right = 52 - 1;
// A--->Z and Z--->A total number is 52, because string end with '\0'(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 "char"
// You can also write like this: if(result == NULL)
if(NULL == result){
printf("No space\n");
exit(1);
}
// Initial result with 0,because string end with ('\0')0.
// So result[0] will be 0, result[1] will be 0 ...... result[52] will be 0
for(int m = 0; m < 53;m++){
result[m] = 0;
}
for (char i = 'A'; i <= 'Z'; i++)
{
result[left] = i;
//printf("%c", i);
left++;
}
// Now left is 26, reset it to 0, because result[0] is 'A'
left = 0;
// n is execute times, need to let result[51] = result[0] = 'A', and result[50] = result[1] = 'B' ......
//so right-- and left++
for (n = 0; n < 26; n++)
{
result[right] = result[left];
right--;
left++;
}
return result;
}
int main()
{
char* str = problem58();
printf("str's length is %ld\n",strlen(str));
printf("%s\n",str);
free(str);
return 0;
}
')0.
// So result[0] will be 0, result[1] will be 0 ...... result[52] will be 0
for(int m = 0; m < 53;m++){
result[m] = 0;
}
for (char i = 'A'; i <= 'Z'; i++)
{
result[left] = i;
//printf("%c", i);
left++;
}
// Now left is 26, reset it to 0, because result[0] is 'A'
left = 0;
// n is execute times, need to let result[51] = result[0] = 'A', and result[50] = result[1] = 'B' ......
//so right-- and left++
for (n = 0; n < 26; n++)
{
result[right] = result[left];
right--;
left++;
}
return result;
}
int main()
{
char* str = problem58();
printf("str's length is %ld\n",strlen(str));
printf("%s\n",str);
free(str);
return 0;
}
')0.
// So result[0] will be 0, result[1] will be 0 ...... result[52] will be 0
for(int m = 0; m < 53;m++){
result[m] = 0;
}
for (char i = 'A'; i <= 'Z'; i++)
{
result[left] = i;
//printf("%c", i);
left++;
}
// Now left is 26, reset it to 0, because result[0] is 'A'
left = 0;
// n is execute times, need to let result[51] = result[0] = 'A', and result[50] = result[1] = 'B' ......
//so right-- and left++
for (n = 0; n < 26; n++)
{
result[right] = result[left];
right--;
left++;
}
return result;
}
int main()
{
char* str = problem58();
printf("str's length is %ld\n",strlen(str));
printf("%s\n",str);
free(str);
return 0;
}
运行它会输出
str's length is 52
ABCDEFGHIJKLMNOPQRSTUVWXYZZYXWVUTSRQPONMLKJIHGFEDCBA
英文:
Here is my answer
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
char* problem58()
{
int n = 0;
int left = 0, right = 52 - 1;
// A--->Z and Z--->A total number is 52, because string end with '#include <stdio.h>
#include <string.h>
#include <stdlib.h>
char* problem58()
{
int n = 0;
int left = 0, right = 52 - 1;
// A--->Z and Z--->A total number is 52, because string end with '#include <stdio.h>
#include <string.h>
#include <stdlib.h>
char* problem58()
{
int n = 0;
int left = 0, right = 52 - 1;
// A--->Z and Z--->A total number is 52, because string end with '\0'(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 "char"
// You can also write like this: if(result == NULL)
if(NULL == result){
printf("No space\n");
exit(1);
}
// Initial result with 0,because string end with ('\0')0.
// So result[0] will be 0, result[1] will be 0 ...... result[52] will be 0
for(int m = 0; m < 53;m++){
result[m] = 0;
}
for (char i = 'A'; i <= 'Z'; i++)
{
result[left] = i;
//printf("%c", i);
left++;
}
// Now left is 26, reset it to 0, because result[0] is 'A'
left = 0;
// n is execute times, need to let result[51] = result[0] = 'A', and result[50] = result[1] = 'B' ......
//so right-- and left++
for (n = 0; n < 26; n++)
{
result[right] = result[left];
right--;
left++;
}
return result;
}
int main()
{
char* str = problem58();
printf("str's length is %ld\n",strlen(str));
printf("%s\n",str);
free(str);
return 0;
}
'(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 "char"
// You can also write like this: if(result == NULL)
if(NULL == result){
printf("No space\n");
exit(1);
}
// Initial result with 0,because string end with ('#include <stdio.h>
#include <string.h>
#include <stdlib.h>
char* problem58()
{
int n = 0;
int left = 0, right = 52 - 1;
// A--->Z and Z--->A total number is 52, because string end with '\0'(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 "char"
// You can also write like this: if(result == NULL)
if(NULL == result){
printf("No space\n");
exit(1);
}
// Initial result with 0,because string end with ('\0')0.
// So result[0] will be 0, result[1] will be 0 ...... result[52] will be 0
for(int m = 0; m < 53;m++){
result[m] = 0;
}
for (char i = 'A'; i <= 'Z'; i++)
{
result[left] = i;
//printf("%c", i);
left++;
}
// Now left is 26, reset it to 0, because result[0] is 'A'
left = 0;
// n is execute times, need to let result[51] = result[0] = 'A', and result[50] = result[1] = 'B' ......
//so right-- and left++
for (n = 0; n < 26; n++)
{
result[right] = result[left];
right--;
left++;
}
return result;
}
int main()
{
char* str = problem58();
printf("str's length is %ld\n",strlen(str));
printf("%s\n",str);
free(str);
return 0;
}
')0.
// So result[0] will be 0, result[1] will be 0 ...... result[52] will be 0
for(int m = 0; m < 53;m++){
result[m] = 0;
}
for (char i = 'A'; i <= 'Z'; i++)
{
result[left] = i;
//printf("%c", i);
left++;
}
// Now left is 26, reset it to 0, because result[0] is 'A'
left = 0;
// n is execute times, need to let result[51] = result[0] = 'A', and result[50] = result[1] = 'B' ......
//so right-- and left++
for (n = 0; n < 26; n++)
{
result[right] = result[left];
right--;
left++;
}
return result;
}
int main()
{
char* str = problem58();
printf("str's length is %ld\n",strlen(str));
printf("%s\n",str);
free(str);
return 0;
}
'(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 "char"
// You can also write like this: if(result == NULL)
if(NULL == result){
printf("No space\n");
exit(1);
}
// Initial result with 0,because string end with ('#include <stdio.h>
#include <string.h>
#include <stdlib.h>
char* problem58()
{
int n = 0;
int left = 0, right = 52 - 1;
// A--->Z and Z--->A total number is 52, because string end with '#include <stdio.h>
#include <string.h>
#include <stdlib.h>
char* problem58()
{
int n = 0;
int left = 0, right = 52 - 1;
// A--->Z and Z--->A total number is 52, because string end with '\0'(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 "char"
// You can also write like this: if(result == NULL)
if(NULL == result){
printf("No space\n");
exit(1);
}
// Initial result with 0,because string end with ('\0')0.
// So result[0] will be 0, result[1] will be 0 ...... result[52] will be 0
for(int m = 0; m < 53;m++){
result[m] = 0;
}
for (char i = 'A'; i <= 'Z'; i++)
{
result[left] = i;
//printf("%c", i);
left++;
}
// Now left is 26, reset it to 0, because result[0] is 'A'
left = 0;
// n is execute times, need to let result[51] = result[0] = 'A', and result[50] = result[1] = 'B' ......
//so right-- and left++
for (n = 0; n < 26; n++)
{
result[right] = result[left];
right--;
left++;
}
return result;
}
int main()
{
char* str = problem58();
printf("str's length is %ld\n",strlen(str));
printf("%s\n",str);
free(str);
return 0;
}
'(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 "char"
// You can also write like this: if(result == NULL)
if(NULL == result){
printf("No space\n");
exit(1);
}
// Initial result with 0,because string end with ('#include <stdio.h>
#include <string.h>
#include <stdlib.h>
char* problem58()
{
int n = 0;
int left = 0, right = 52 - 1;
// A--->Z and Z--->A total number is 52, because string end with '\0'(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 "char"
// You can also write like this: if(result == NULL)
if(NULL == result){
printf("No space\n");
exit(1);
}
// Initial result with 0,because string end with ('\0')0.
// So result[0] will be 0, result[1] will be 0 ...... result[52] will be 0
for(int m = 0; m < 53;m++){
result[m] = 0;
}
for (char i = 'A'; i <= 'Z'; i++)
{
result[left] = i;
//printf("%c", i);
left++;
}
// Now left is 26, reset it to 0, because result[0] is 'A'
left = 0;
// n is execute times, need to let result[51] = result[0] = 'A', and result[50] = result[1] = 'B' ......
//so right-- and left++
for (n = 0; n < 26; n++)
{
result[right] = result[left];
right--;
left++;
}
return result;
}
int main()
{
char* str = problem58();
printf("str's length is %ld\n",strlen(str));
printf("%s\n",str);
free(str);
return 0;
}
')0.
// So result[0] will be 0, result[1] will be 0 ...... result[52] will be 0
for(int m = 0; m < 53;m++){
result[m] = 0;
}
for (char i = 'A'; i <= 'Z'; i++)
{
result[left] = i;
//printf("%c", i);
left++;
}
// Now left is 26, reset it to 0, because result[0] is 'A'
left = 0;
// n is execute times, need to let result[51] = result[0] = 'A', and result[50] = result[1] = 'B' ......
//so right-- and left++
for (n = 0; n < 26; n++)
{
result[right] = result[left];
right--;
left++;
}
return result;
}
int main()
{
char* str = problem58();
printf("str's length is %ld\n",strlen(str));
printf("%s\n",str);
free(str);
return 0;
}
')0.
// So result[0] will be 0, result[1] will be 0 ...... result[52] will be 0
for(int m = 0; m < 53;m++){
result[m] = 0;
}
for (char i = 'A'; i <= 'Z'; i++)
{
result[left] = i;
//printf("%c", i);
left++;
}
// Now left is 26, reset it to 0, because result[0] is 'A'
left = 0;
// n is execute times, need to let result[51] = result[0] = 'A', and result[50] = result[1] = 'B' ......
//so right-- and left++
for (n = 0; n < 26; n++)
{
result[right] = result[left];
right--;
left++;
}
return result;
}
int main()
{
char* str = problem58();
printf("str's length is %ld\n",strlen(str));
printf("%s\n",str);
free(str);
return 0;
}
Run it will output
str's length is 52
ABCDEFGHIJKLMNOPQRSTUVWXYZZYXWVUTSRQPONMLKJIHGFEDCBA
答案2
得分: -1
这个for循环
for (int i = left; i < 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 < 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 '\0';
.
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 * ( '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;
}
or
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;
}
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论