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.
the putchar(j) in getReverse() will lead to a confused output, as j is very little they may be unprintable character. If you really want to monitor the value of j, use printf("%d\\n",j);
as @jarmod said ,you miss a '\\0' in src[]. You should add "src[count] = '\\0';" after the loop of while() and before getReverse() in main().
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.
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.
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;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.