C 字符串的反向数组
我有一些关于 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
琴弦的副本对我来说听起来不错。由于每个字符串始终有 4 个字符,因此您可以避免空终止符
\0
。或者,您需要将sep_str
声明为 5x(lenght/4) 矩阵,以在每个字符串末尾存储 \0 字符。要反转字符串,您需要从字符串的开头迭代到中间,将第
i
个字符替换为第length-i-1
个字符。您需要更换内部才能将k=3
替换为k=2
。您还需要处理最后一个字符串,因为长度可能不是四的倍数。
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 declaresep_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 thelength-i-1
-th. You need to replace the inner for replacingk=3
tok=2
.You also need to take care of the last string, since the lenght might not be multiple of four.
在 C 字符串中,只有一个终止字符。但如果您需要对字符串进行标记,则每个字符串必须以 null 结尾。
但在此之前 -
上述情况中的字符串文字驻留在只读位置并且无法修改。如果需要修改的话
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 -
String literal in the above case resides in read only location and cannot be modified. If you need to modify, then
如果您的字符串中没有终止字符,那么是的,您将超出数组的范围,因为您正在访问 sep_str[i][4],这不是有效的位置:
但是,我怀疑您想要在字符串开头有空字符,因此 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:
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.
仅在最后,但如果您想将每个单独的块视为自己的字符串,则需要自己添加
\0
。你可以用指针来做到这一点...
假设
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.You could do it with pointers...
Assuming
str
is your string.