c++ - how to shift elements of array (or c-string) left by a given number indexes -
I am writing a function to move the letters of my C-string through the number of characters described. At present, this ceremony will leave the characters, but I am losing one. I know that this kind of indexing issue is with my loop, but I can not pin it down.
Edit: From left I mean:
The string of an initial C-A, B, C, D is shifted to the left index, then it will be the same array B , C, D, A is equal to, if two indexes are shifted to the left then this same c-string would be equal to c, d, a, b
So my code is:
#include & lt; Iostream & gt; using namespace std; Zero shift byte (four Marray [], int size, int shift by) {four taps; For (int i = size -1; i> gt; i--) {temp = myarray [size + shiftBy]; MyRare [size + shift] = myar [ii]; Mayor [I] = Temporary; }} Int main () {char myarray [20] = "test"; Int size = 4; Shift byte (amerare, size, 1); For (Int i = 0; I
This is my work which changes every element on the right, whatever I have to do, turns this loop upside down, and in this way the elements are left , Such as: & lt; - -
// function block wave shift right (four Merre [], int size, int shift) {if (shiftBy> size) {shiftBy = shiftBy - size; } If (size == 1) {// nothing} other {four fresh; // Loop to print array with index (above) - & gt; For 2 (int i = 0; i
With a little fanning, I was able to work it out . This is my functioning function :)
The issue was that I need to assign + ShiftBy to Element I, and only when I & lt; Shape - shiftBy
// function block zero change (variable myarray [], integer shape, int shiftBy) {if (shiftBy> size) {shiftBy = shiftBy - size; } If (size == 1) {// nothing} other {four fresh; // For loop to print array with index (left) & lt; - Print array for 2 (int i = 0; i
Comments
Post a Comment