I have created two arrays that need to be concatenated after the second array (y in this case) has been rotated. However I want to rotate only the last 4 bytes of this array. This is my code:
我创建了两个数组,需要在第二个数组(在本例中为y)旋转后连接。但是我想只旋转这个数组的最后4个字节。这是我的代码:
char x[]={"hello"};
char y[]={"goodmorning"};
char combine[20];
strcpy(combine, x);
strcat(combine, y);
printf(combine);
Here before the concatenation into combine I want to do the rotation operation.
在串联到联合之前我想做旋转操作。
before rotation
combine= hellogoodmorning
after rotation
combine= gninhellogoodmor
I have tried to look for a logic to do this online but could not find anything specific. Can anybody please help.
我试图在网上寻找一个逻辑,但找不到具体的东西。任何人都可以帮忙。
4 个解决方案
#1
1
void strRev(char *s)
{
char temp, *end = s + strlen(s) - 1;
while( end > s)
{
temp = *s;
*s = *end;
*end = temp;
--end;
++s;
}
}
char x[]={"hello"};
char y[]={"goodmorning"};
char combine[20];
strcpy(combine, x);
strcat(combine, y);
strRev(combine);
strRev(combine+4);
printf(combine);
#2
1
I use
#define swap(a, b) { a ^= b; b ^= a; a ^= b; }
void reverse(char * s, int beg, int end) {
while (beg < end) {
swap(s[beg], s[end]);
++beg, --end;
}
}
void rotate(char * s, int k) {
if (!s || !*s) return;
int len = strlen(s);
k %= len;
reverse(s, 0, len - 1);
reverse(s, 0, k - 1);
reverse(s, k, len - 1);
}
and call rotate(combine, 4);
to rotate 4 bytes in combine.
并调用rotate(combine,4);在combine中旋转4个字节。
#3
1
Try something like this shown below. I use strncpy instead of strcpy()
and strcat()
. Try to debug for better understanding. Run Live.
尝试如下所示的内容。我使用strncpy而不是strcpy()和strcat()。尝试调试以获得更好的理解。跑动。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define ROTATION_LEN 4
int main() {
char x[] = { "hello" };
char y[] = { "goodmorning" };
char c;
char combine[20] = {0}; // init with null
char * com = combine;
int i;
int leny = strlen(y);
int lenx = strlen(x);
for (i = 0; i < ROTATION_LEN; ++i) {
c = y[leny -1 - i];
combine[i] = c;
}
com += ROTATION_LEN; // forward address of combine by ROTATION_LEN
strncpy(com, x, lenx);
com += lenx; // forward address of combine by x length
strncpy(com, y, leny - ROTATION_LEN);
printf(combine);
return 0;
}
Output:
gninhellogoodmor
#4
0
If you define a function like this:
如果你定义这样的函数:
void rotate_last_four(char *string)
{
char old_four[4];
char new_four[4];
strncpy(old_four, string + strlen(string) - 4, 4);
new_four[0] = old_four[3];
new_four[1] = old_four[2];
new_four[2] = old_four[1];
new_four[3] = old_four[0];
memmove(string + 4, string, strlen(string) - 4);
strncpy(string, new_four, 4);
}
Then you only need to add this line to your code before printing combine
:
然后,您只需要在打印组合之前将此行添加到代码中:
rotate_last_four(combine);
And the output is: gninhellogoodmor
输出是:gninhellogoodmor
#1
1
void strRev(char *s)
{
char temp, *end = s + strlen(s) - 1;
while( end > s)
{
temp = *s;
*s = *end;
*end = temp;
--end;
++s;
}
}
char x[]={"hello"};
char y[]={"goodmorning"};
char combine[20];
strcpy(combine, x);
strcat(combine, y);
strRev(combine);
strRev(combine+4);
printf(combine);
#2
1
I use
#define swap(a, b) { a ^= b; b ^= a; a ^= b; }
void reverse(char * s, int beg, int end) {
while (beg < end) {
swap(s[beg], s[end]);
++beg, --end;
}
}
void rotate(char * s, int k) {
if (!s || !*s) return;
int len = strlen(s);
k %= len;
reverse(s, 0, len - 1);
reverse(s, 0, k - 1);
reverse(s, k, len - 1);
}
and call rotate(combine, 4);
to rotate 4 bytes in combine.
并调用rotate(combine,4);在combine中旋转4个字节。
#3
1
Try something like this shown below. I use strncpy instead of strcpy()
and strcat()
. Try to debug for better understanding. Run Live.
尝试如下所示的内容。我使用strncpy而不是strcpy()和strcat()。尝试调试以获得更好的理解。跑动。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define ROTATION_LEN 4
int main() {
char x[] = { "hello" };
char y[] = { "goodmorning" };
char c;
char combine[20] = {0}; // init with null
char * com = combine;
int i;
int leny = strlen(y);
int lenx = strlen(x);
for (i = 0; i < ROTATION_LEN; ++i) {
c = y[leny -1 - i];
combine[i] = c;
}
com += ROTATION_LEN; // forward address of combine by ROTATION_LEN
strncpy(com, x, lenx);
com += lenx; // forward address of combine by x length
strncpy(com, y, leny - ROTATION_LEN);
printf(combine);
return 0;
}
Output:
gninhellogoodmor
#4
0
If you define a function like this:
如果你定义这样的函数:
void rotate_last_four(char *string)
{
char old_four[4];
char new_four[4];
strncpy(old_four, string + strlen(string) - 4, 4);
new_four[0] = old_four[3];
new_four[1] = old_four[2];
new_four[2] = old_four[1];
new_four[3] = old_four[0];
memmove(string + 4, string, strlen(string) - 4);
strncpy(string, new_four, 4);
}
Then you only need to add this line to your code before printing combine
:
然后,您只需要在打印组合之前将此行添加到代码中:
rotate_last_four(combine);
And the output is: gninhellogoodmor
输出是:gninhellogoodmor