C 字符串的反向数组

发布于 2024-12-12 02:34:45 字数 925 浏览 1 评论 0原文

我有一些关于 C 中字符串数组的问题。

我有一个数组 char *string。我有一个 char *string,然后我将每 4 个字符拆分为一个名为 sep_str 的字符串数组。例如,如果 char *string = 'The sum';,则 char **sep_str 是:

0: |_| --> "The "
1: |_| --> "Sum"

我的第一个问题是,在 C 中的字符串数组中(所以数组字符数组),每个 sep_str[i] 的末尾是否有一个空终止字符,或者只是在 sep_str 的最后一个位置?以下是我将 string 复制到字符串数组中的方法:

    for (int i = 0; i < str_length; i++) {
        sep_str[i/4][i%4] = *ptr;
        ptr++;
    }

我的第二个问题是,如何反转 sep_str 中每个字符串的元素?这是我的做法,但我觉得它正在走出子字符串的数组。 (所以在 sep_str 的元素之外):

// Reverse each element in the array
    char temp;

    for (int i = 0; i < num_strs; i++) {

        for (int j = 0, k = 4; j < k; j++, k--) {

            temp = sep_str[i][j];
            sep_str[i][j] = sep_str[i][k];
            sep_str[i][k] = temp;
        }
    }

I have a few questions regarding array of strings in C.

I have an array char *string. I have a char *string and then I split every 4 characters in a array of strings called sep_str. So for example if char *string = 'The sum';, then char **sep_str is:

0: |_| --> "The "
1: |_| --> "Sum"

My first question is, in an array of strings in C (so array of array of chars), will there be a null terminating character at the end of each sep_str[i], or just at the last position of sep_str? Here is how I copy string into an array of strings:

    for (int i = 0; i < str_length; i++) {
        sep_str[i/4][i%4] = *ptr;
        ptr++;
    }

My second question is, how would I reverse the elements of each string in sep_str? Here's how I did it, but I feel like it is stepping out of the array of the substring. (so out of the element of the sep_str):

// Reverse each element in the array
    char temp;

    for (int i = 0; i < num_strs; i++) {

        for (int j = 0, k = 4; j < k; j++, k--) {

            temp = sep_str[i][j];
            sep_str[i][j] = sep_str[i][k];
            sep_str[i][k] = temp;
        }
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

比忠 2024-12-19 02:34:45

琴弦的副本对我来说听起来不错。由于每个字符串始终有 4 个字符,因此您可以避免空终止符 \0。或者,您需要将 sep_str 声明为 5x(lenght/4) 矩阵,以在每个字符串末尾存储 \0 字符。

要反转字符串,您需要从字符串的开头迭代到中间,将第 i 个字符替换为第 length-i-1 个字符。您需要更换内部才能将 k=3 替换为 k=2

您还需要处理最后一个字符串,因为长度可能不是四的倍数。

char temp;

for (int i = 0; i < (num_strs - 1); i++) {
    for (int j = 0, k = 3; j < k; j++, k--) {
        temp = sep_str[i][j];
        sep_str[i][j] = sep_str[i][k];
        sep_str[i][k] = temp;
    }
}

if (num_strs > 0) {
    for (int j = 0, k = strlen(sep_str[i]) - 1; j < k; j++, k--) {
        temp = sep_str[i][j];
        sep_str[i][j] = sep_str[i][k];
        sep_str[i][k] = temp;
    }
}

The copy of the strings sounds good to me. Since each string has always 4 chars, you can avoid the null terminator \0. Alternatively you need to declare sep_str as a 5x(lenght/4) matrix, to store the \0 char at the end of each string.

To reverse a string you need to iterate from the start to the middle of the string, replacing the i-th char with the length-i-1-th. You need to replace the inner for replacing k=3 to k=2.

You also need to take care of the last string, since the lenght might not be multiple of four.

char temp;

for (int i = 0; i < (num_strs - 1); i++) {
    for (int j = 0, k = 3; j < k; j++, k--) {
        temp = sep_str[i][j];
        sep_str[i][j] = sep_str[i][k];
        sep_str[i][k] = temp;
    }
}

if (num_strs > 0) {
    for (int j = 0, k = strlen(sep_str[i]) - 1; j < k; j++, k--) {
        temp = sep_str[i][j];
        sep_str[i][j] = sep_str[i][k];
        sep_str[i][k] = temp;
    }
}
耳钉梦 2024-12-19 02:34:45

在 C 字符串中,只有一个终止字符。但如果您需要对字符串进行标记,则每个字符串必须以 null 结尾。

但在此之前 -

char *string = "The sum"; // should be const char* string = "The sum";

上述情况中的字符串文字驻留在只读位置并且无法修改。如果需要修改的话

char string[] = "The sum";

In a C string, there will be only one termination character. But if you need to tokenize the strings, then each string must be null terminated.

But before that -

char *string = "The sum"; // should be const char* string = "The sum";

String literal in the above case resides in read only location and cannot be modified. If you need to modify, then

char string[] = "The sum";
失退 2024-12-19 02:34:45

如果您的字符串中没有终止字符,那么是的,您将超出数组的范围,因为您正在访问 sep_str[i][4],这不是有效的位置:

sep_str[0] = 'T'
sep_str[1] = 'h'
sep_str[2] = 'e'
sep_str[3] = ' '

但是,我怀疑您想要在字符串开头有空字符,因此 for 循环中需要 k=3,而不是 k=4。

If you don't have the terminating character in your strings then yes, you will be outside the bounds of the array since you are accessing sep_str[i][4], which is not a valid location:

sep_str[0] = 'T'
sep_str[1] = 'h'
sep_str[2] = 'e'
sep_str[3] = ' '

However, I doubt that you want to have the null character at the beginning of your string, so you need k=3 in your for loop, not k=4.

缘字诀 2024-12-19 02:34:45

我的第一个问题是,在 C 中的字符串数组(因此字符数组的数组)中,每个 sep_str[i] 的末尾或仅在 sep_str 的最后一个位置是否会有一个空终止字符?

仅在最后,但如果您想将每个单独的块视为自己的字符串,则需要自己添加 \0

我的第二个问题是,如何反转 sep_str 中每个字符串的元素?

你可以用指针来做到这一点...

char temp;

// Point to start of string, `str` will decay to first memory position.
char *start = str;

// Point to the end of the string. You will need to `#include <string.h>`
// for `strlen()`. Otherwise, write a `while` loop that goes until `\0` to find
// the last position.
char *end = &str[strlen(str) - 1];

// Do until we hit the middle of the string.
while (start < end) {

   // Need a temp char, no parallel assignment in C.
   temp = str[start];

   // Swap chars.
   str[start++] = str[end];
   str[end--] = str[temp];
}

假设 str 是你的字符串。

My first question is, in an array of strings in C (so array of array of chars), will there be a null terminating character at the end of each sep_str[i], or just at the last position of sep_str?

Only at the end, but if you want to treat each individual chunk as its own string, you'll need to add the \0 yourself.

My second question is, how would I reverse the elements of each string in sep_str?

You could do it with pointers...

char temp;

// Point to start of string, `str` will decay to first memory position.
char *start = str;

// Point to the end of the string. You will need to `#include <string.h>`
// for `strlen()`. Otherwise, write a `while` loop that goes until `\0` to find
// the last position.
char *end = &str[strlen(str) - 1];

// Do until we hit the middle of the string.
while (start < end) {

   // Need a temp char, no parallel assignment in C.
   temp = str[start];

   // Swap chars.
   str[start++] = str[end];
   str[end--] = str[temp];
}

Assuming str is your string.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文