繁体   English   中英

如何在文本的左侧和右侧用空格填充字符数组

[英]How to pad char array with empty spaces on left and right hand side of the text

我对 C++ 相当陌生,所以对于某些人来说,我的问题的答案似乎很明显。 我想要实现的是创建一个方法,该方法将返回给定的 char 数组,并在其前后填充空格以满足一定的长度。 所以最后的效果就像给定的 char 数组会在另一个更大的 char 数组的中间。

假设我们有一个带有 HelloWorld 的 char 数组。 我希望该方法返回一个新的字符数组,该数组具有预先指定的长度,并且给定的字符数组“定位”在返回字符数组的中间。

char ch[] = "HelloWorld";
char ret[20];                           // lets say we want to have the resulting char array the length of 20 chars
char ret[20] = "     HelloWorld     ";  // this is the result to be expected as return of the method

如果给定的字符数组为奇数,则希望它位于中间左侧一个空格的偏移量中。 我还想避免任何 memory 使用字符串或任何其他不在标准库中的方法 - 尽可能保持简单。 解决这个问题的最佳方法是什么? 谢谢!

主要有两种方法:要么使用 char 文字(又名 char 数组),就像你在 C 语言中所做的那样,要么使用内置的std::string类型(或类似类型),如果你'在 C++ 中重新编程,尽管有例外。 我为每个人提供一个例子。

首先,使用 arrays,您需要包含cstring header 以使用内置的字符串文字操作函数。 请记住,作为长度的一部分,char 数组始终以 null 终止符'\0' (ASCII 码为 0)终止,因此对于DIM维度的字符串,您将能够将字符存储在DIM - 1位置。 这是带有注释的代码。

constexpr int DIM = 20;

char ch[] = "HelloWorld";
char ret[DIM] = "";

auto len_ch = std::strlen(ch);      // length of ch without '\0' char
auto n_blanks = DIM - len_ch - 1;   // number of blank chars needed
auto half_n_blanks = n_blanks / 2;  // half of that

// fill in from begin and end of ret with blanks
for (auto i = 0u; i < half_n_blanks; i++)
    ret[i] = ret[DIM - i - 2] = ' ';

// copy ch content into ret starting from half_n_blanks position
memcpy_s(
    ret + half_n_blanks,    // start inserting from here
    DIM - half_n_blanks,    // length from our position to the end of ret
    ch,                     // string we need to copy
    len_ch);                // length of ch

// if odd, after ch copied chars
// there will be a space left to insert a blank in
if (n_blanks % 2 == 1)
    *(ret + half_n_blanks + len_ch) = ' ';

我选择首先在字符串的开头和结尾插入空格,然后复制ch的内容。

第二种方法要容易得多(编码和理解)。 std::string (在 header string中定义)可以包含的最大字符大小是std::npos ,这是std::size_t类型(通常是unsigned inttypedef )可以拥有的最大字符数。 基本上,您不必担心std::string最大长度。

std::string ch = "HelloWorld", ret;
auto ret_max_length = 20;

auto n_blanks = ret_max_length - ch.size();

// insert blanks at the beginning
ret.append(n_blanks / 2, ' ');

// append ch
ret += ch;

// insert blanks after ch
// if odd, simply add 1 to the number of blanks
ret.append(n_blanks / 2 + n_blanks % 2, ' ');

如您所见,我在这里采用的方法有所不同。

请注意,由于'\0' ,这两种方法的结果不同。 如果您想获得相同的行为,您可以将 1 添加到DIM或从ret_max_length中减去 1。

假设我们知道数组 ret 的大小 s 并且知道任何 char 数组的最后一个字符是'\0' ,我们找到输入 char 数组 ch 的长度 l。

int l = 0;
int i;
for(i=0; ch[i]!='\0'; i++){

 l++;

}

然后我们计算两边需要多少空间。 如果 total_space 是偶数,则两边的空格相等。 否则,我们可以选择哪一侧有额外的空间,在这种情况下,左侧。

int total_spaces = size-l-1; // subtract by 1 to adjust for '\0' character
int spaces_right = 0, spaces_left = 0;
if((total_spaces%2) == 0){

 spaces_left = total_spaces/2;
 spaces_right = total_spaces/2;

}
else{

 spaces_left = total_spaces/2;
 spaces_right = (total_spaces/2)+1;

}

然后首先添加 left_spaces,然后是输入数组 ch,然后是要 ret 的 right_spaces。

i=0;
while(spaces_left > 0){

 ret[i] = ' ';
 spaces_left--;
 i++;

} // add spaces
ret[i] = '\0';

strcat(ret, ch); // concatenate ch to ret

while(spaces_right){

 ret[i] = ' ';
 spaces_right--;
 i++;

}
ret[i] = '\0';

确保包含<cstring>以使用strcat()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM