C++ 如何将字符串拆分为单个字符
c++ how to split a string into single characters
我已经有了这个
std::string str_1, str_2, str_3, str_4, str_5, str_6, str_7, str_8;
std::string str;
std::getline(std::cin,str);
std::istringstream iss(str);
iss >> str_1 >> str_2 >> str_3 >> str_4 >> str_5 >> str_6 >> str_7 >> str_8;
输入:H E L L O (在一个字符串中(
输出:str_1 =H str_2 =E str_3=L str_4=L str_5=O
这就是我试图实现的目标
输入:你好(在单个字符串中使用 CIN (
输出: h e l l o (在 5 个单独的字符串中(
我需要输入在一个字符串中,因为用户输入的字符数各不相同
cin >> str_1 >> str_2 >> str_3 >> str_4 >> str_5 >> str_6 >> str_7 >> str_8 >> endl;
不起作用,因为您必须声明所有变量
为什么不直接迭代一个字符串并像这样打印它呢?
string str1;
cin >> str1;
for(int i=0; i<str1.length(); i++)
cout << str[i] << " ";
cout << endl;
string a = "hello";
cout << a[1];
尝试遍历 a[对于不同的值]
#include <iostream>
#include <string.h>
using namespace std;
typedef struct _ARRAY_ITEMS
{
char item[10];
int iPosicion;
} ARRAY_ITEMS;
int tokens(char * string, char token, ARRAY_ITEMS * outItems){
char cItem[50];
char TempItem[50];
int x = 0,i;
ARRAY_ITEMS items;
memset(cItem, 0x00, sizeof(cItem));
memset(TempItem, 0x00, sizeof(TempItem));
sprintf( cItem,"%s", string);
for(i = 0; i <= strlen(cItem); i++){
if (cItem[i] != token){
sprintf( TempItem,"%s%c",TempItem, cItem[i]);
}else{
memset( &items, 0x00, sizeof( ARRAY_ITEMS ) );
sprintf( items.item,"%s",TempItem);
items.iPosicion = x;
memcpy( &outItems[x], &items, sizeof( ARRAY_ITEMS ) );
memset(TempItem, 0x00, sizeof(TempItem));
x++;
}
}
memset( &items, 0x00, sizeof( ARRAY_ITEMS ) );
sprintf( items.item,"%s",TempItem);
items.iPosicion = x;
memcpy( &outItems[x], &items, sizeof( ARRAY_ITEMS ) );
return x;
}
int main() {
ARRAY_ITEMS items[10];
int iLenPos = 0;
// iLenPos = tokens("01x02x03",'x', items); //split for ","
// iLenPos = tokens("01,02,03",',', items); //split for ","
iLenPos = tokens("01.02.03",'.', items); //split for "."
for ( int i = 0; i <= iLenPos; ++i)
{
printf("POSICION %d numero %sn",items[i].iPosicion, items[i].item);
}
//print POSICION 0 numero 01
//print POSICION 1 numero 02
//print POSICION 2 numero 03
return 0;
}
相关文章:
- 如何读取单个字符并在输入两个字符序列时输出? 使用 while 循环和C++
- 为什么char不是单个字符
- C++ 字符串单个字符
- 将单个字符转换为 std::string 前缀 \x01
- 尝试一次将单个字符读取到大小不确定的数组中
- C++ 如何将字符串拆分为单个字符
- 如何在C++中将单个字符哈希为相对较小的素数?
- 如何将像'A'这样的单个字符替换为类似"10"?
- 我通过迭代加法将二进制数转换为十进制并检查单个字符(请参阅代码)的方法有什么问题?
- std::bad_alloc 将文本文件中的单个字符存储到矢量C++中时
- 如何从用户那里获取单个字符,直到他输入"."或输入
- 字符串中单个字符作为数组的数据类型是什么
- 字符串中的重音,如何在单个字符中存储重音字符
- 从字符串中提取单个字符并将其转换为 int
- 如何将单个字符转换为为基本密码哈希器设置整数,C++
- 下推解析器是否扫描令牌或单个字符
- 如何定义一个从任何编码空间解析单个字符的精神 x3 解析器?
- C :占用更多内存,将单个字符作为char或字符串存储
- 如何从字符串中读取单个字符
- WM_GETTEXT只返回单个字符