-4

I want to split the string into a 2d string and divide array's first index to store only the reference of 3 words and the second index to store the word I have read this post similar to It but it is not soo good for me StackOver Flow, to understand it properly I have given an example.
For example string a="Hello I am writing c++ code";

I want to convert it to

string b[100][3];
b[0][1]="hello";
b[0][2]="I";
b[0][3]="am";
b[1][1]="writing";
b[1][2]="c++";
b[1][3]="code";

2 Answers2

0

This works and I think will do what you want:

#include <array>
#include <iostream>
#include <string>
#include <utility>
#include <vector>

std::vector< std::array< std::string, 3 > > split( std::string const &s )
{
    std::vector< std::array< std::string, 3 > > rv;
    std::array< std::string, 3 > a;
    int i = 0;
    size_t first = 0, len = s.size();

    while (first < len)
    {
        size_t last = s.find( ' ', first );
        if (last == std::string::npos)
            last = len;
        a[i++] = s.substr( first, last - first );
        first = last + 1;
        if (i == 3)
        {
            rv.emplace_back( std::move( a ) );
            i = 0;
        }
    }
    if (i)
        rv.emplace_back( std::move( a ) );

    return rv;
}

int main()
{
    auto v = split( "Hello I am writing c++ code" );
    for (auto const &a : v)
        std::cout << "'" << a[0] << "'; '" << a[1] << "'; '" << a[2] << "'\n";
    return 0;
}
Khouri Giordano
  • 796
  • 3
  • 9
0

Vector of vectors is a nice and elegant solution to your problem. Here is a suggested solution to your problem.

#include <iostream>
#include <string>
#include <sstream>
#include <algorithm>
#include <iterator>
#include <vector>

using namespace std;



vector<vector<string>> strTo2DStr(const string& str, const int& r, const int& c)
{
    vector<vector<string>> mat;
    int rows(r), cols(c);
    vector<string> words;
    istringstream ss(str);
    copy(istream_iterator<string>(ss),istream_iterator<string>(),back_inserter(words));

    int counter(0);
    for ( int i(0); i < rows; ++i ){
        vector<string> temp;
        for ( int j(0); j < cols; ++j){
            if ( counter < words.size() )
                temp.push_back(words[counter++]);
            else
                temp.push_back("");
        }
        mat.push_back(temp);
    }

    return mat;
}

int main ()
{
    string str("Hello I am writing c++ code");
    int rows(3), cols(3);
    vector< vector<string> > mat = strTo2DStr(str,rows,cols);

    cout << str << endl << endl;
    for ( int i(0); i < rows; ++i )
        for ( int j(0); j < cols; ++j)
            cout << "mat[" << i << "]["<< j << "]= " << mat[i][j] << "  " << endl;


  return 0;
}

The solution is flexible in the sense that you can choose the number of rows and columns. The result is

Hello I am writing c++ code
mat[0][0]= Hello
mat[0][1]= I
mat[0][2]= am
mat[1][0]= writing
mat[1][1]= c++
mat[1][2]= code
mat[2][0]=
mat[2][1]=
mat[2][2]=
CroCo
  • 5,531
  • 9
  • 56
  • 88