-1

vector of the string takes input from a given string.

Input: Hello World

Expected Output: Hello World

Actual Output: Hello

#include <bits/stdc++.h>

using namespace std;

int main()
{
    string s1;
    getline(cin, s1);
    vector<string> vs1;
    string s2;
    for (int i = 0; i < s1.size(); i++) {
        if (s1[i] != ' ') {
            s2.push_back(s1[i]);
            continue;
        }
        if (s1[i] == ' ' || i == s1.size() - 1) {
            vs1.push_back(s2);
            s2.clear();
        }
    }
    for (string t : vs1) {
        cout << t << " ";
    }

    return 0;
}
east1000
  • 1,240
  • 1
  • 10
  • 30
  • 1
    The normal input operator `>>` separates on space. You can use it with e.g. an [`std::istringstream`](https://en.cppreference.com/w/cpp/io/basic_istringstream) to split a string into "words". Together with [`std::istream_iterator`](https://en.cppreference.com/w/cpp/iterator/istream_iterator) you can fill a vector in just a couple of lines: `std::getline(std::cin, s1); std::istringstream iss1(s1); std::vector vs1(std::istream_iterator(iss1), std::istream_iterator());` All done, now `vs1` contains all "words" in the input. – Some programmer dude Sep 09 '21 at 05:51
  • 4
    Side note : https://stackoverflow.com/Questions/31816095/Why-Should-I-Not-Include-Bits-Stdc-H. – Pepijn Kramer Sep 09 '21 at 05:52
  • 4
    As for your current code, time to learn how to use a *debugger* to step through your code statement by statement while monitoring variables and their values. – Some programmer dude Sep 09 '21 at 05:55
  • 1
    OT: don't include ``, it's a nasty thing taught in some poor learning materials. Read this: https://stackoverflow.com/Questions/31816095/Why-Should-I-Not-Include-Bits-Stdc-H. – Jabberwocky Sep 09 '21 at 07:02

3 Answers3

2

Your first if statement will execute for any character besides a space. This means that the second if statement is not reached when i==s1.size()-1 because s1[10] == 'd' for your test input.

Since your first if statement includes a continue, the second branch is never evaluated at all when the first is true. That continue statement should not be there.

bindsniper001
  • 164
  • 2
  • 13
  • 1
    By the way, I don't recommend you use this method. I second the comments made by @Someprogrammerdude. If you're new to the concept of iterators, his code may be confusing, but they're incredibly useful and definitely worth learning about. And you definitely should get used to using a live debugger. It will make tracking down these kinds of bugs *much* easier. – bindsniper001 Sep 09 '21 at 06:44
2

You can use std::stringstream so you don't need unnecessary if blocks.

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

int main()
{
    std::string s1;
    std::getline(std::cin, s1);
    std::string buffer;                  
    std::stringstream ss(s1);       // Insert the string into a stream

    std::vector<std::string> vs1; // Create vector to hold our words

    while (ss >> buffer)
        vs1.push_back(buffer);

    for (std::string t : vs1) {
        std::cout << t << " ";
    }
    return 0;
}

Output: Hello World

Note: using namespace std; is bad practice. Why is "using namespace std;" considered bad practice?

east1000
  • 1,240
  • 1
  • 10
  • 30
0

When you use vs1.push_back(s2) you are adding s2 to other element. You don't even have to use first if, if you want to add space to first element. Here you are:

#include <bits/stdc++.h>

using namespace std;

int main()
{
string s1;
getline(cin,s1);
vector < string > vs1;
string s2;
for(int i=0;i<s1.size();i++)
{
    s2.push_back(s1[i]);
    
     if(s1[i]==' ' || i==s1.size()-1)
     {
        vs1.push_back(s2);
        s2.clear();
      }
 }
cout<<vs1[0]<<vs1[1];

 return 0;

}