如何将数组(或 c 字符串)的元素向左移动给定的数字索引

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26745018/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 11:41:07  来源:igfitidea点击:

how to shift elements of array (or c-string) left by a given number indexes

c++visual-studio-2012

提问by

I am writing a function to shift the characters of my c-string left by a given number of characters. Currently the function will shift the characters left but I am losing one. I know it is some sort of indexing issue with my for loop, but I can't pin it down.

我正在编写一个函数来将我的 c 字符串的字符向左移动给定数量的字符。目前该功能将向左移动字符,但我丢失了一个。我知道这是我的 for 循环的某种索引问题,但我无法确定。

EDIT: By shift left I mean:

编辑:左移我的意思是:

Given a starting c-string of a, b, c, d if shifted left one index this same array would equal b, c, d, a if shifted left two indexes this same c-string would equal c, d, a, b

给定 a, b, c, d 的起始 c-string 如果左移一个索引,这个相同的数组将等于 b, c, d, a 如果左移两个索引,这个相同的 c-string 将等于 c, d, a, b

Here is my code so far:

到目前为止,这是我的代码:

#include <iostream>
using namespace std;

void shiftleft (char myarray[], int size, int shiftBy)
{

char temp;

for (int i=size-1; i > 0; i--)
{
    temp = myarray[size+shiftBy];
    myarray[size+shiftBy] = myarray[i];
    myarray[i] = temp;
}


}

int main() {
    char myarray[20] = "test";
    int size = 4;
    shiftleft (myarray, size, 1);

    for(int i = 0; i < size+1; i++){
    cout << myarray[i];
    }


    return 0;
}

Here is my working function that shifts each element to the right, all I need to do is reverse this loop, and move the elements left, as in this way: <----

这是我将每个元素向右移动的工作函数,我需要做的就是反转这个循环,并将元素向左移动,如下所示:<----

//function bloack
void shiftright (char myarray[], int size, int shiftBy)
{
    if(shiftBy > size){
        shiftBy = shiftBy - size;
    }
    if(size == 1){
        //do nothing
    }
    else{
        char temp;
        //for loop to print the array with indexes moved up (to the right) --> by 2
        for (int i=0; i < size; i++)
        {
            temp = myarray[size-shiftBy];
            myarray[size-shiftBy] = myarray[i];
            myarray[i] = temp;
        }

    }
}

采纳答案by

With a little fanangling, I was able to get it to work. Here is my functioning function :)

通过一点点fanangling,我能够让它工作。这是我的功能:)

The issue was that I needed to assign element i to i+shiftBy, and only repeat the loop while i < size-shiftBy.

问题是我需要将元素 i 分配给 i+shiftBy,并且只在 i < size-shiftBy 时重复循环。

//function bloack
void shiftLeft (char myarray[], int size, int shiftBy)
{
    if(shiftBy > size){
        shiftBy = shiftBy - size;
    }

    if(size == 1){
        //do nothing
    }
    else{
        char temp;
        //for loop to print the array with indexes moved up (to the left) <-- by 2
        for (int i=0; i < size-shiftBy; i++)
        {//EXAMPLE shift by 3  for a c-string of 5
            temp = myarray[i];//temp = myarray[0]
            myarray[i] = myarray[i + shiftBy];//myarray[0] == myarray[2]
            myarray[i + shiftBy] = temp;//myarray[2] = temp(value previously at index i)
        }

    }
}

回答by morteza khadem

if you want shift string to left and don't rotate, you can use this code:

如果您想将字符串向左移动并且不旋转,您可以使用以下代码:

void shiftLeft (char *string,int shiftLength)
{

int i,size=strlen(string);
if(shiftLength >= size){
    memset(string,'
char* name = strdup("Hello");
char* str_ptr = (char*) malloc(sizeof(char));  
strcpy(str_ptr, name);
int j = 3 // len of shift
for(int i = 0; i < strlen(str_ptr); i++){
  printf("%c", str_ptr[(i+j)%strlen(str_ptr)]);
}
',size); return; } for (i=0; i < size-shiftLength; i++){ string[i] = string[i + shiftLength]; string[i + shiftLength] = '##代码##'; } }

回答by athyagat

// Suppose if j is your len of shift

// 假设 j 是你的移位长度

##代码##