Who123
Who123

Reputation: 29

How to get strings until find space

Hello how to get all strings until find space and push_back the words until space in the second turn of For loop to start getting all string after space and again until find space that's my code

for example this sentece 5bbbb3 1f a0aaa f1fg3

i want to get bbbb and push_back into in a vector of chars then to push_back aaaa and so

vector of chars vec = vec.[0] == 'bbbb' vec.[1] == 'aaaa' vec.[2] == 'f' vec.[3] == 'ffg'

Thank you in advanced

these are my 2 codes both does not work

#include <iostream>
#include <string>
#include <vector>

using namespace std;

int main(){

    string sentece;
    getline(cin, sentece);
    vector<char> words;

    for (int i = 0; i < sentece.size(); ++i)
    {
        while (sentece.substr(i, ' '))
        {
            if(isalpha(sentece.at(i)))
            {
                words.push_back(sentece.at(i));

            }
        }
    }
    cout << words[0] << '\n';
    cout << words[1] << '\n';
    cout << words[2] << '\n';

    for(const auto& a : words)
    {
        cout << a;
    }

        return 0;
}

//==================================================================


#include <iostream>
#include <string>
#include <vector>

using namespace std;

int main(){

    string sentece;
    getline(cin, sentece);
    vector<char> words;

    for (int i = 0; i < sentece.size(); ++i)
    {
        while (sentece.at(i) != ' ')
        {
            if(isalpha(sentece.at(i)))
            {
                words.push_back(sentece.at(i));

            }
            if(sentece.at(i) == ' ')
            {
                break;
            }
        }
    }

     cout << words[0] << '\n';
    cout << words[1] << '\n';
    cout << words[2] << '\n';

    for(const auto& a : words)
    {
        cout << a;
    }


        return 0;
}

Upvotes: 0

Views: 1132

Answers (2)

Ankit Mishra
Ankit Mishra

Reputation: 590

You can use character array with scanf() `

#include<bits/stdc++.h>
    using namespace std;
    int main()
   {
       char s[1000];
       scanf("%[^' ']%s", s);
       cout<<s;
   }

This will stop taking input untill you hit enter but this will store string upto only till first occurence of space.

Upvotes: 0

Sam
Sam

Reputation: 104

I believe this code should give you the answer you want:

#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main()
{
    string test = "5bbbb3 1f a0aaa f1fg3";
    vector<string> words;

    for (int i = 0; i < test.size(); i++)
    {
        string tmp = "";
        while (test[i] != ' ' && i < test.size())
        {
            if (isalpha(test.at(i))){
                tmp += test[i];
            }
            else if (test.at(i) == ' ')
            {
                i++;
                break;
            }
            i++;
        }
        words.push_back(tmp);
    }

    cout << words[0] << '\n';
    cout << words[1] << '\n';
    cout << words[2] << '\n';
    cout << words[3] << '\n';
}

All you have to do is then replace the test sentence with your user input. You were forgetting to increment i in the while loop so it was examining the same character every time and getting stuck infinitely.

I tried to use as much of your original code as possible so don't assume that this is the most efficient or elegant solution to the problem

Hope this helps :)

Upvotes: 1

Related Questions