Home > Back-end >  I'm stuck with problem related to string in C
I'm stuck with problem related to string in C

Time:11-28

I want to display a sentence in reverse order. For eg "I have money" to "money have I". I have used stack like this but I don't understand why I can't push elements into my stack (stack size always = 0 when I check at the end)

#include<bits/stdc  .h>
using namespace std;

int main() {
    string s;
    cin >> s;
    string temp;
    stack<string> stk;
    for (int i = 0; i < s.size(); i  ) {
        if (s[i] != ' ') {
           temp.push_back(s[i]);
           cout << temp << endl;
        } else {
            stk.push(temp);
            temp = "";
        }
    }
    for (int i = 0; i < stk.size(); i  ) {
        cout << stk.top() << endl;
        stk.pop();
    }
    cout << stk.size();
}

CodePudding user response:

Here is a complete answer. It is very short and concise.

#include <stack>
#include <string>
#include <iostream>

int main() 
{
    std::string temp;
    std::stack<std::string> stk;
    while (std::cin >> temp)
        stk.push(temp);

    while (!stk.empty())
    {
      std::cout << stk.top() << " ";
      stk.pop();
    }
}  

Note that there are no loops to check for spaces.

Second, the while loop is much more intuitive and easier to write than your for loop, which is buggy. In your for loop, you have a shrinking stack and an increasing i index going on at the same time, which is wrong.

The proper way to print out a stacks entries while emptying the stack is to use a while (not empty) paradigm, while inside the loop, popping the top element.

Last, and not least, the code doesn't have all of the flaws such as including the bits header, and using namespace std;

Live Example

CodePudding user response:

#include <bits/stdc  .h>
using namespace std;

vector <string> splitstringarray;
void splitstring(string s)
{
    stringstream ss(s);
    string word;
    while (ss >> word) {
        splitstringarray.push_back(word);
        cout << word << endl;
    }
}

int main()
{
    splitstring("you sample string")
    for (int i = splitstringarray.size(); i > 0; i--)
    {
        cout << splitstringarray[i];
    }
    return 0;
}

CodePudding user response:

As a variant you can consider this solution of your problem.

#include <iostream>
#include <vector>
#include <string>
using namespace std;
const int maximumSize=10;
vector<int> visited(maximumSize, 0);
vector<string> sentence={"I", "have", "money"};
vector<string> reverseSentence;
void showContentVector(vector<string> input)
{
    for(int i=0; i<input.size();   i)
    {
        cout<<input[i]<<" ";
    }
    return;
}
void dfs(int current, int previous)
{
    if(visited[current]==1)
    {
        return;
    }
    visited[current]=1;
    for(int next=(current 1); next<sentence.size();   next)
    {
        if(next==previous)
        {
            continue;
        }
        dfs(next, current);
    }
    reverseSentence.push_back(sentence[current]);
    if(current==0)
    {
        showContentVector(reverseSentence);
    }
    return;
}
int main()
{
    dfs(0, -1);
    return 0;
}

Input:

I have money

Here is the result:

money have I
  • Related