如何在c++中将字符串拆分为字符数组
How to split a string to an array of characters in C++?
我正在尝试将字符串(例如,"how are you doing?")转换为不带任何逗号空格或标点符号的数组,例如{'h','o','w','a','r','e','y','o','u','d','o',' I ','n','g'}这是我在下面写的函数,但是它不起作用。
#include <iostream>
#include <cstring>
#include <string>
char split(string str){
char array[80];
for (int i=0; i<str.length();i++){
if (str[i]==","||str[i]=="."||str[i]==" ")
delete str[i];
else
array[i]=str.substr(i,1);
}
return (array[80]);
}
首先,关于代码的一些注意事项:
-
<cstring>
(即<string.h>
)是无用的,因为你不使用任何从<cstring>
头在你的代码。 -
由于
string
是一个输入的只读参数,所以可以通过const &
传递。 -
因为你想要一个字符数组,你可以返回一个
vector<char>
(甚至是string
…)。
要解决您的问题,您可以简单地遍历输入字符串的字符,如果它们是字母和数字字符(即没有空格,没有逗号等),您可以将它们添加到字符的输出结果向量中。
您可能需要考虑以下示例注释代码:
#include <ctype.h> // For isalnum()
#include <iostream> // For console output
#include <string> // For std::string
#include <vector> // For std::vector
using namespace std;
vector<char> split(const string& str)
{
vector<char> result;
// For each character in the string
for (char ch : str)
{
// Copy only alphabetical characters and numeric digits
if (isalnum(ch))
{
result.push_back(ch);
}
}
return result;
}
int main()
{
vector<char> result = split("How are you doing?");
cout << "{ ";
for (char ch : result)
{
cout << "'" << ch << "' ";
}
cout << "}" << endl;
}
输出:{ 'H' 'o' 'w' 'a' 'r' 'e' 'y' 'o' 'u' 'd' 'o' 'i' 'n' 'g' }
如果你喜欢更"功能"的风格,你可以使用std::copy_if()
算法,代码如下:
#include <algorithm> // For std::copy_if()
#include <iterator> // For std::back_inserter
....
const string str = "How are you doing?";
vector<char> result;
copy_if(
str.begin(), str.end(), // copy source
back_inserter(result), // copy destination
[](char ch) { return isalnum(ch); } // when to copy the character
);
你在重新发明轮子
std::string
有方法来帮助你
size_t found = str.find_last_of(".");
str.replace(found,1," ");
您也可以使用char*
使用简单的迭代来遍历字符串并挑选您需要的内容。
我不确定从split
返回array[80]
的原因是什么,但这与您的问题无关。
#include <iostream>
#include <string>
#include <ctype.h>
char split(std::string str)
{
char array[80];
char const* cp = str.c_str();
int i = 0;
for (; *cp; ++cp )
{
if ( isalnum(*cp) )
{
array[i++] = *cp;
}
}
array[i] = ' ';
cp = array;
for (; *cp; ++cp )
{
std::cout << "'" << *cp << "' ";
}
std::cout << std::endl;
return (array[80]);
}
int main()
{
split("What are you having for lunch today?");
}
输出:<>之前' W ' ' h ' a ' t ' ' ' ' r ' ' e ' y ' o ' ' u ' ' h ' ' ' ' v ' '我' ' n ' ' g ' f ' o ' ' r ' ' l ' ' u ' ' n ' ' c ' ' h ' ' t ' ' o ' ' d ' ' ' ' y ' 另一个简单的解决方案:
我改变返回类型为字符串,如果你坚持你可以采取c_str()
的返回值为const char*
string已经像vector of characters.
string split(string str)
{
size_t i=0;
while (string::npos != (i=str.find_first_of(",. ",i))){
str.erase(i,1);
}
return str;
}
你可以这样使用:
string s = split("bla bla bla");
如果你想要指针:
cout<<s.c_str()<<endl;
相关文章:
- 如何根据字符拆分字符串
- 使用连续分隔符和空最后一个字符进行拆分
- 拆分字符数组并存储为向量
- 通过拆分字符数组来创建字符串
- 拆分字符串输入并查找字符是否有效
- 通过正则表达式拆分字符串会切断结果的第一个字母
- C++ 如何将字符串拆分为单个字符
- 如何将字符串拆分为一组 3 个字符,它们之间有空格
- C++ 中的解析器/拆分常量字符*
- 将字符拆分为字符数组
- 在独立模式 c++ 中分隔符处拆分字符*
- 如何使用 c++ win32 API 拆分字符
- boost::algorithm - 拆分字符串会返回一个额外的标记
- CUDA 拆分字符数组
- 在空格之间拆分字符数组
- 拆分字符数组
- 如何在不使用任何基本函数的情况下拆分字符数组
- 如何在c ++中用多个分隔符拆分字符指针并返回字符指针数组?
- 如何创建一个字符串数组来拆分字符,其中单词由" "分隔?C++
- c++拆分字符串会产生奇怪的输出