如何在c++中将字符串拆分为字符数组

How to split a string to an array of characters in C++?

本文关键字:拆分 字符 数组 字符串 c++      更新时间:2023-10-16

我正在尝试将字符串(例如,"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]);
}

首先,关于代码的一些注意事项:

  1. <cstring>(即<string.h>)是无用的,因为你不使用任何从<cstring>头在你的代码。

  2. 由于string是一个输入的只读参数,所以可以通过const &传递。

  3. 因为你想要一个字符数组,你可以返回一个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;