Converting a sentence string to a string array of words in Java

后端 未结 16 2441
余生分开走
余生分开走 2020-12-01 00:04

I need my Java program to take a string like:

\"This is a sample sentence.\"

and turn it into a string array like:

{\"this\         


        
16条回答
  •  借酒劲吻你
    2020-12-01 00:34

    Here is a solution in plain and simple C++ code with no fancy function, use DMA to allocate a dynamic string array, and put data in array till you find a open space. please refer code below with comments. I hope it helps.

    #include
    using namespace std;
    
    int main()
    {
    
    string data="hello there how are you"; // a_size=5, char count =23
    //getline(cin,data); 
    int count=0; // initialize a count to count total number of spaces in string.
    int len=data.length();
    for (int i = 0; i < (int)data.length(); ++i)
    {
        if(data[i]==' ')
        {
            ++count;
        }
    }
    //declare a string array +1 greater than the size 
    // num of space in string.
    string* str = new string[count+1];
    
    int i, start=0;
    for (int index=0; index

提交回复
热议问题