[英]Reverse string array
I'm trying to reverse an array of characters and place it in a new array. 我正在尝试反转字符数组并将其放置在新数组中。 It doesn't seem to be returning anything. 它似乎没有返回任何东西。 Here is the code below, I can't figure out waht is wrong. 这是下面的代码,我不知道这是错误的。 Any pointers? 有指针吗? (No pun intended) (无双关语)
void getReverse(char dest[], char src[])
{
int i;
int j=0;
int length = strlen(src);
printf("%d\n", length);
for(i=length-1; i>=0; i--, j++)
//for(j=0; j<length; j++)
{
dest[j]=src[i];
putchar(j);
j++;
}
dest[j] = '\0';
}
main()
{
char dest[MAX_SIZE];
char src[MAX_SIZE];
int i = 0;
int count;
int c = getchar();
count = 0;
while ((count < MAX_SIZE) || (c != EOF))
{
src[count] = c;
++count;
//putchar(c);
c = getchar();
}
getReverse(dest, src);
printf("%s", dest);
}
bugs: 错误:
as @Bill said, j++ execute in the loop of getReverse() twice. 正如@Bill所说,j ++在getReverse()循环中执行两次。
the putchar(j) in getReverse() will lead to a confused output, as j is very little they may be unprintable character. getReverse()中的putchar(j)会导致输出混乱,因为j很小,它们可能是无法打印的字符。 If you really want to monitor the value of j, use printf("%d\\n",j); 如果您确实想监视j的值,请使用printf(“%d \\ n”,j);
as @jarmod said ,you miss a '\\0' in src[]. 如@jarmod所说,您在src []中错过了'\\ 0'。 You should add "src[count] = '\\0';" 您应该添加“ src [count] ='\\ 0';” after the loop of while() and before getReverse() in main(). 在while()循环之后和main()中的getReverse()之前。
in addition,your getReverse() can't work if *src and *dest point to the same address.Althogh you have declared that the function "reverse an array of characters and place it in a new array", but consider this when you write a more common function. 此外,如果* src和* dest指向相同的地址,则getReverse()无法工作。尽管您已经声明函数“反转字符数组并将其放置在新数组中”,但是当您使用该函数时,请考虑一下写一个更通用的功能。
I just modify some code and run it. 我只是修改一些代码并运行它。 the code as follow 代码如下
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#define MAX_SIZE 30
void getReverse(char dest[], char src[])
{
int i;
int j=0;
int length = strlen(src);
printf("%d\n", length);
for(i=length-1; i>=0; i--, j++)
//for(j=0; j<length; j++)
{
dest[j]=src[i];
//putchar(j);
//j++;
}
dest[j] = '\0';
}
main()
{
char dest[MAX_SIZE];
char src[MAX_SIZE];
int i = 0;
int count;
int c = getchar();
count = 0;
memset(src, 0, MAX_SIZE);
while ((count < MAX_SIZE - 1) && c != EOF)
{
src[count] = c;
++count;
//putchar(c);
c = getchar();
}
printf("src=%s\n", src);
getReverse(dest, src);
printf("dest=%s\n", dest);
}
the result as follow 结果如下
abcdefghijk
src=abcdefghijk
11
dest=kjihgfedcba
Here the value of j incremented twice. 在此,j的值增加了两倍。
for(i=length-1; i>=0; i--, **j++**)
//for(j=0; j<length; j++)
{
dest[j]=src[i];
putchar(j);
**j++**;
}
dest[j] = '\0';
}
This may be the problem. 这可能是问题所在。
#include <stdio.h>
#include <string.h>
void getReverse(char dest[], char src[])
{
int i;
int j=0;
int length = strlen(src);
printf("%d\n", length);
for(i=length-1; i>=0; i--, j++)
//for(j=0; j<length; j++)
{
dest[j]=src[i];
//putchar(dest[j]);
//j++;//duplicate , `for` after expression
}
dest[j] = '\0';
}
#define MAX_SIZE 32
int main(void){
char dest[MAX_SIZE];
char src[MAX_SIZE];
//int i = 0;//unused
int count;
int c = getchar();
count = 0;
while ((count < MAX_SIZE) && (c != EOF))//|| -> &&
if(c == '\n'){
src[count] = '\0';
break;
}
src[count] = c;
++count;
//putchar(c);
c = getchar();
}
getReverse(dest, src);
printf("%s", dest);
return 0;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.