Trying to reverse a C++ array and at the same time store it into a new array

前端 未结 2 1659
长发绾君心
长发绾君心 2021-01-29 05:18

I\'m trying to reverse an array and simultaneously store it in a new array with for loops. I have achieved it somewhat manually but not with loops. The first code b

2条回答
  •  轮回少年
    2021-01-29 05:25

    You have two nested loops, and they are not doing what you think they are doing. You only need one:

    int y, s;
    
    s = strlen(wrd) - 1;
    
    for (y = 0; y<=s; y++)
    {
        rev[y] = wrd[s - y];
    }
    

    and don't forget to terminate:

    rev[s + 1] = 0;
    

提交回复
热议问题