每隔n个字符打印一行换行符
Print a newline every n characters
我试图打印一个字符数组,但我想在固定数量的字符后打印一个换行符。到目前为止,我只能在读取第一个固定数量的字符后打印一行换行符。有人能给我指正确的方向吗?
for ( int i = 0; i < (m_nWidth*m_nHeight); i++) {
if (i != m_nWidth)
cout << pMyPointer[i];
else
{
printf("n");
}
}
if( i % n == 0 && i != 0 ) printf("n");
其中n
是换行之间的字符数。
编辑:完整上下文:
#include <iostream>
int main()
{
const int n = 5;
const std::string str("abcdefghijklmnopqrstuvwxyz");
for (int i=0 ; i<str.size() ; ++i)
{
if (i%n == 0 && i != 0)
std::cout << 'n';
std::cout << str[i];
}
std::cout << std::endl;
return 0;
}
您可以使用模块运算符:
if(i % n == 0)
printf("n");
相关文章:
- 如何防止clang格式在流运算符调用之间添加换行符<<
- C/C++ - 查询平台相关的换行符(用于内存映射文件)
- 在 Stream C++ 文本之前有一个额外的换行符
- 流:CSV 文件中的换行符
- 如何在三元条件运算符中添加换行符和连接? :在 C++ 中
- Clang-格式:在多行语句之后的换行符上打开大括号
- 在输出流中插入换行符
- 防止控制台在通道字符(在 c++ 中)中输入空格(即空格、制表符和换行符)
- QProcess::readAllStandardOutput() 和换行符
- 如果有换行符,clang 格式不会附加大括号
- 提取后返回换行符的C++istream.get()
- 如何在出现换行符之前将多个整数作为输入?
- 正则表达式 获取两个换行符之间的文本
- 包含换行符分隔的单词的文件和C++中这些单词的字符串向量的大小是否相同?
- 在字符串中输入换行符
- 从文本文件中读取数据并删除所有换行符空格,并在 C++ 控制台中显示
- 使用 fgets() 从键盘读取时从以前的输入读取换行符
- 每隔n个字符打印一行换行符
- 如何在写入文本文件时向一行添加换行符
- 在控制台中读取一行直到换行符