在 c++ 中的每个标记后添加空格
Add space after each token in c++
#include<stdio.h>
#include<iostream>
#include<string.h>
int main()
{
char str[] = "01 21 03 06 0f 1a 1c 33 3a 3b";
char *pch;
char *m[100];
pch = strtok (str,"' '' '");
size_t i = 0;
while (pch !=NULL)
{
m[i]=pch;
i++;
pch = strtok (NULL,"' '' '");
}
for (int j=0;j!=i;j++)
{
printf("%s",m[j]);
}
return 0;
我想在每个令牌后添加空格。我想将空间作为数组 m 的一部分包含在内。不是 printf(" ") 在我出来之前printf("%s",m[j]);
放成"012103060f1a1c333a3b"如何在每 2 个字符后添加空格?
用 C++ 重写以使其更简单:
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
int main()
{
std::string str("01 21 03 06 0f 1a 1c 33 3a 3b");
std::vector<std::string> m;
std::istringstream inp(str);
std::string token;
while (inp >> token)
{
std::ostringstream outp;
outp << token << " ";
m.push_back(outp.str());
}
for (const auto& tok: m) { std::cout << tok; }
}
使用 stringstream
时,您只需读取由任何空格分隔的标记,并将它们以所需的格式流式传输到输出流。
请注意,使用输出流比此处真正需要的更通用。 它可以替换为m.push_back(token + " ");
相关文章:
- C++为一串单词添加空格
- 我还可以添加什么来按空格或空格正确拆分C++字符串?
- 在特定位置为字符串添加空格
- 在单词之间添加空格
- 如何在括号与代码之间添加空格以进行C++?
- 在字符串中的字符前添加空格C++插入函数
- 如何在两个单词之间添加空格
- 如何使用 << 添加一个空格来填充带有 ofstream 的减号?
- 我正在尝试在字符数组的中间添加一个空格.不确定如何正确移动它
- 添加尾随空格和使用 eof 时的额外输出
- 如何添加一个空格字符来触发 var args c++ execlp()
- 在 c++ 中的每个标记后添加空格
- C++-向字符串添加空格
- 在字符数组的开头添加空格
- gnu在目录名vpath中添加空格
- 给定一个单词,通过在它们之间添加空格来形成一个有意义的单词
- 如何防止clang-format在注释字符后添加空格
- 编译器要求我在默认函数参数的等号前添加空格
- c++ getline在字符串开头添加空格
- 如何通过添加空格或其他字符来读取用户输入