我对如何将生成的数据保存在新字符串的 for 循环中有一些疑问

I have some question about how to save generated data in a for looping in a new string

我正在尝试用 C 编写代码,生成由 2 个字母组成的随机字符(aa、ab、ac、ad、ae ... za、zb、zc ... zz)。问题是如何将这些字符存储到数组中而不是将它们打印在屏幕上?

string key[] = {
    "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M",  "N", "O", "P", "Q", "R", "S", "T", "U", "V", "X", "Y", "Z", "a","b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p","q", "r", "s", "t", "u", "v", "x", "y", "z"};
for(int i = 0; i < 50; i++)
{
        for(int j = 0; j < 50; j++)
        {
            printf("%s%s\n", key[i], key[j]);
        }
    }

我可以再给一个密码。

int i;
int j;
for(i=97;i<=122;i++){
    for(j=97;j<=122;j++){
        printf("%c %c,",i,j);
    }
}

它以下列格式打印:

a a,a b 依此类推直到 z z.

我是这样解决问题的:

char char_2[1000];
for(int i = 0; i < 50; i++)
    {
        for(int j = 0; j < 50; j++)
        {
            sprintf(char_2, "%s%s", key[i], key[j]);
        }
    }

您似乎希望将两个字符的所有组合作为数组中的字符串。顺便说一句:这不是随机的。

解决方案可能如下所示:

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

    string key[] = {
            "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U",
            "V", "X", "Y", "Z", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q",
            "r", "s", "t", "u", "v", "x", "y", "z"};

    //num is the number of elements in the array
    int num = sizeof(key) / sizeof(key[0]);
    //dynamically allocate memory for it
    char *combi = malloc(num * num * (sizeof(char) * 3));

    for (int i = 0; i < num; i++) {
        for (int j = 0; j < num; j++) {
            char *buf = combi + (i * num + j) * (sizeof(char) * 3);
            sprintf(buf, "%s%s", key[i], key[j]);
        }
    }

    //verify the result
    int counter = 0;
    for (int i = 0; i < num; i++) {
        for (int j = 0; j < num; j++) {
            char *combination = combi + (i * num + j) * (sizeof(char) * 3);
            printf("[%d]: %s\n", counter++, combination);
        }
    }

    //free the dynamically allocated memory
    free(combi);

    return 0;
}

输出

[0]: AA
[1]: AB
[2]: AC
...
[2498]: zy
[2499]: zz

说明

`combi' 指向一个大小为 7500 字节的动态分配块。然后使用此内存块将两个字符的所有组合存储为字符串。

C 中的字符串表示它以 NULL 结尾。

每个条目因此有 3 个字节:两个字节用于 2 个字符,一个字节用于终止 NULL。

内存布局如下所示:

假设您要访问第二个字符串,在我们的例子中是字符串 "AB":

您将使用指向分配内存的字符指针 combi 并添加 3。

char *secondString = combi + 3;

如果你现在想用printf输出这个字符串,写

printf("%s\n", secondString);

并得到输出 "AB"。顺便说一句:您还可以调用除 printf 之外的任何需要字符串作为参数的函数。

在使用嵌套循环时,我们必须将元素的数量乘以外部索引变量的值并加上内部索引变量的值以获得相应的数组索引。由于每个元素有三个字节(两个字符和一个终止 NULL),这个数组索引必须乘以 3 才能得到对应的字符串指针。

您需要一个存储数据的地方 (stored[]),它需要包含字符串 (char*'s),并为字符串空终止符留出足够的空间。 因此,"XX" 有 sizeof("XX") = 3.

您省略了 'W'(和 'w')。

#include <stdio.h>
#include <stdlib.h>

string key[] = {
  "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M",
  "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z",
  "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m",
  "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"
};
//how many elements in key[] array
const int keysize = sizeof(key)/sizeof(key[0]);
//this should be largest string produced...
const int elementsize = sizeof("XX");
//fixed static allocation
char stored[keysize*keysize][elementsize];
//dynamic allocation
char* dynamic = malloc(keysize*keysize*elementsize);

int
main(int argc, char* argv)
{
  printf("keysize:%d\n",keysize);
  printf("elementsize:%d\n",elementsize);

  int ndx0, ndx1, offset;
  //combine every element of key with every element, O(N*N)
  for(ndx0=0; ndx0<keysize; ndx0++) {
    for(ndx1=0; ndx1<keysize; ndx1++) {
      //printf("[%d,%d]offset:%d\n",ndx0,ndx1,offset);
      offset = ndx0*keysize+ndx1;
      sprintf(stored[offset],"%s%s",*key[ndx0],*key[ndx1]);
      //place all the strings in one long char buffer, dynamic[]
      sprintf(dynamic[offset*elementsize],"%s%s",*key[ndx0],*key[ndx1]);
    }
  }

  //confirm
  for(ndx0=0; ndx0<keysize; ndx0++) {
    for(ndx1=0; ndx1<keysize; ndx1++) {
      offset = ndx0*keysize+ndx1;
      //printf("[%d,%d]%s,",ndx0,ndx1,stored[offset]);
      printf("%s\n", stored[offset]);
      if( (26-1)==(offset%26)) printf("\n");
    }
  }

  //confirm, dynamic allocation
  for(ndx0=0; ndx0<keysize; ndx0++) {
    for(ndx1=0; ndx1<keysize; ndx1++) {
      int offset = ndx0*keysize+ndx1;
      printf("%s,", dynamic+offset*elementsize);
      if( (26-1)==(offset%26)) printf("\n");
    }
  }
}