文本对齐C语言
Text justification C language
我必须解决一个涉及左对齐字符串长度和前导零的问题。
我有以下 table :
- 开始
- 关闭
- 连接
- 删除
- 结束
- 初始化
- 打印
- 写入
这是一个简单的程序产生的。
我的问题是找出如何转换它:
它必须完全对齐。
我必须使用左对齐而不是“制表符”(\t)。
另外,我必须计算字符串长度。到目前为止,我的代码如下:
int i;
int long temp;
char a = 'a';
char command[][12]= {
"BEGIN", "CLOSE", "CONCATENATE", "DELETE",
"END" , "INITIALIZE", "PRINT", "WRITE"
};
int main()
{
printf("a/a \tCommand \tLength\n");
for (i=0 ; i<8 ;i++){
temp = strlen(command[i]);
printf("%c. %10s %ld \n",a, command[i], temp);
a++;
}
printf("\n");
return 0;
}
我想在 printf 中添加一个整数而不是数字 ( 10 ),但我不知道这是否是正确的解决方案。
例如这样的事情:
printf("%c. %10s %%dld \n",a, command[i], integer, temp);
感谢任何帮助。
谢谢。
来自 printf 的手册:
The field width
An optional decimal digit string (with nonzero first digit) specifying
a minimum field width. If the converted value has fewer characters
than the field width, it will be padded with spaces on the left (or
right, if the left-adjustment flag has been given). Instead of a
decimal digit string one may write "*" or "*m$" (for some decimal
integer m) to specify that the field width is given in the next
argument, or in the m-th argument, respectively, which must be of type
int.
(emphasis mine)
所以你可以这样写:
printf("%c. %10s %*ld \n",a, command[i], temp, integer);
作为如何输出你想要的东西的例子:
行:
printf("%c. %10s %ld \n",a, command[i], temp);
应该写成:
printf("%c. %-10.10s %5.ld \n",a, command[i], temp);
和这一行:
printf("%c. %10s %ld \n",a, command[i], temp);
应该写成:
printf("%c. %-10.10s %5.ld \n",a, command[i], temp);
请注意左对齐 %s 上的“-”,
注意 %s 上的 10.10 强制该字段始终为 10 个字符
注意“5”。在 %d 上强制该字段始终为 5 个字符
(右对齐)
除 space 之外,还有其他填充字符修改,以禁止前导 0 抑制等。
您可能想在这里查看更多详细信息:
http://www.tutorialspoint.com/c_standard_library/c_function_printf.htm
我必须解决一个涉及左对齐字符串长度和前导零的问题。
我有以下 table :
- 开始
- 关闭
- 连接
- 删除
- 结束
- 初始化
- 打印
- 写入
这是一个简单的程序产生的。 我的问题是找出如何转换它:
它必须完全对齐。
我必须使用左对齐而不是“制表符”(\t)。 另外,我必须计算字符串长度。到目前为止,我的代码如下:
int i;
int long temp;
char a = 'a';
char command[][12]= {
"BEGIN", "CLOSE", "CONCATENATE", "DELETE",
"END" , "INITIALIZE", "PRINT", "WRITE"
};
int main()
{
printf("a/a \tCommand \tLength\n");
for (i=0 ; i<8 ;i++){
temp = strlen(command[i]);
printf("%c. %10s %ld \n",a, command[i], temp);
a++;
}
printf("\n");
return 0;
}
我想在 printf 中添加一个整数而不是数字 ( 10 ),但我不知道这是否是正确的解决方案。
例如这样的事情:
printf("%c. %10s %%dld \n",a, command[i], integer, temp);
感谢任何帮助。 谢谢。
来自 printf 的手册:
The field width
An optional decimal digit string (with nonzero first digit) specifying a minimum field width. If the converted value has fewer characters than the field width, it will be padded with spaces on the left (or right, if the left-adjustment flag has been given). Instead of a decimal digit string one may write "*" or "*m$" (for some decimal integer m) to specify that the field width is given in the next argument, or in the m-th argument, respectively, which must be of type int. (emphasis mine)
所以你可以这样写:
printf("%c. %10s %*ld \n",a, command[i], temp, integer);
作为如何输出你想要的东西的例子:
行:
printf("%c. %10s %ld \n",a, command[i], temp);
应该写成:
printf("%c. %-10.10s %5.ld \n",a, command[i], temp);
和这一行:
printf("%c. %10s %ld \n",a, command[i], temp);
应该写成:
printf("%c. %-10.10s %5.ld \n",a, command[i], temp);
请注意左对齐 %s 上的“-”,
注意 %s 上的 10.10 强制该字段始终为 10 个字符
注意“5”。在 %d 上强制该字段始终为 5 个字符 (右对齐)
除 space 之外,还有其他填充字符修改,以禁止前导 0 抑制等。
您可能想在这里查看更多详细信息:
http://www.tutorialspoint.com/c_standard_library/c_function_printf.htm