gpt4 book ai didi

c - 如何使用空格重新排列数组?

转载 作者:行者123 更新时间:2023-12-01 21:46:00 25 4
gpt4 key购买 nike

我正在努力重新排列我的数组。我使用了从单个循环到多个循环,试图在两对字符之间放置空格(白色字符),但我一直在重写原始输入。所以总是有偶数长度的输入,例如 ABCDEFGH。我的任务是通过在每 2 个字符(最后一个字符除外)后放置空格来扩展数组的大小。所以输出将是:

AB CD EF GH

所以输出的大小(如果我是正确的)将是 (2*input_len)-1谢谢。

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

// output = "ABCDEFGHIJKL
char c1;
char c2;
char c3;
int o_len = strlen(output);
for(int i = 2; i < o_len + olen/2; i = i + 3){
if(i == 2){
c1 = output[i];
c2 = output[i+1];
c3 = output[i+2];
output[i] = ' ';
output[i+1] = c1;
output[i+2] = c2;
}
else{
c1 = output[i];
c2 = output[i+1];
output[i] = ' ';
output[i+1] = c3;
output[i+2] = c1;
c3 = c2;
}
}

所以前3对打印正确,然后就乱七八糟了。

最佳答案

假设您需要存储空格分隔的结果,可能最简单的插入空格的方法就是使用一对指针(一个指向您的输入字符串,一个指向您的输出字符串)然后循环不断地写入一对输出字符串,将两个指针递增 2,检查输入字符串中是否有字符(如果是 break; 并以 nul 终止输出字符串), 否则将 space 写入输出字符串并重复。

你可以相当简单地使用memcpy(或者你可以只复制 2 个字符到当前指针和 pointer + 1,你的选择,但是因为你已经include string.h for strlen() -- 让你自己轻松)你可以做类似的事情:

#include <stdio.h>
#include <string.h>

#define ARRSZ 128 /* constant for no. of chars in output string */

int main (int argc, char **argv) {

char *instr = argc > 1 ? argv[1] : "ABCDEFGH", /* in string */
outstr[ARRSZ] = "", /* out string */
*ip = instr, *op = outstr; /* pointers to each */
size_t len = strlen (instr); /* len of instr */

if (len < 4) { /* validate at least 2-pairs worth of input provided */
fputs ("error: less than two-pairs to separate.\n", stderr);
return 1;
}
if (len & 1) { /* validate even number of characters */
fputs ("error: odd number of characters in instr.\n", stderr);
return 1;
}
if (ARRSZ < len + len / 2) { /* validate sufficient storage in outstr */
fputs ("error: insufficient storage in outstr.\n", stderr);
return 1;
}

for (;;) { /* loop continually */
memcpy (op, ip, 2); /* copy pair to op */
ip += 2; /* increment ip by 2 for next pair */
op += 2; /* increment op by 2 for next pair */
if (!*ip) /* check if last pair written */
break;
*op++ = ' '; /* write space between pairs in op */
}
*op = 0; /* nul-terminate outstr */

printf ("instr : %s\noutstr : %s\n", instr, outstr);
}

示例使用/输出

$ ./bin/strspaceseppairs
instr : ABCDEFGH
outstr : AB CD EF GH

$ ./bin/strspaceseppairs ABCDEFGHIJLMNOPQ
instr : ABCDEFGHIJLMNOPQ
outstr : AB CD EF GH IJ LM NO PQ

奇数个字符:

$ ./bin/strspaceseppairs ABCDEFGHIJLMNOP
error: odd number of characters in instr.

或短字符串:

$ ./bin/strspaceseppairs AB
error: less than two-pairs to separate.

检查一下,如果您还有其他问题,请告诉我。


编辑以简单地输出单对或空字符串

基于 @chqrlie 的评论它可能比对短字符串发出诊断更有意义,只是为了输出它不变。由你决定。在这种情况下,您可以修改第一个条件并将其移动到奇数字符检查之后,例如

    if (len & 1) {  /* validate even number of characters */
fputs ("error: odd number of characters in instr.\n", stderr);
return 1;
}
if (len < 4) { /* validate at least 2-pairs worth of input provided */
puts(instr); /* (otherwise output unchanged and exit) */
return 0;
}

您可以决定如何处理程序的任何方面并进行相应的更改。

关于c - 如何使用空格重新排列数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60586219/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com