Birds With Feathers
Birds With Feathers

Reputation: 31

Need Help Debugging my Code That Reverses the Words in a String

I need some help debugging my code. This code is intended to reverse the words in a string that is in the form of a sentence [assuming that the string does not have a "." at the end]. For some reason what I'm getting as an output is the indented output plus an extra space after the first word as well as the indented output minus the first word. I am a beginner at coding; so if possible, I would appreciate more simple to understand solutions, or a solution that uses a loop, strings, and arrays.

Sample Input:

My name is Edward

Intended Output:

Edward is name My

Output Received:

Edward  is name 

Here is my code so far:

#include <iostream>
#include <string>
#include <stdio.h>

using namespace std;

int main() {

string s, n, a;
getline(cin, s);

for (int i = s.length(); i >= 0; i--){
    if (s[i] != 32 ) {
        n += s[i];
    }
    else {
        for (int j = n.length() -1; j >= 0; j--){
            a += n[j];
        }
        cout << a << ' ';
        n.clear();
        a.clear();
    }
}

cin.ignore();
getchar();
return 0;

}

Also, I just noticed that there is also an extra space at the end. If there is a way to maybe cancel outputting the last space; please tell me.

Thanks for reading, I appreciate your help.

Upvotes: 0

Views: 95

Answers (4)

Jonathan Mee
Jonathan Mee

Reputation: 38929

So this is really just an additional step of abstraction from πάντα ῥεῖ's excellent answer. You can use istream_iterator and ostream_iterator to further simplify your code.

The entire code to answer your question can be boiled down to:

const vector<string> words{ istream_iterator<string>(cin), istream_iterator<string>() };

copy(crbegin(words), crend(words), ostream_iterator<string>(cout, " "));

Live Example

Upvotes: 2

Birds With Feathers
Birds With Feathers

Reputation: 31

Edit: Thanks for the help from the comments and answers, I fixed the problem with the extra space and added something at the end that outputs the final word. It's not perfect, but it works. :)

#include <iostream>
#include <string>

using namespace std;

int main() {

string s, n;
getline(cin, s);

for (int i = s.length() -1; i >= 0; i--){
if (s[i] != 32) {
    n += s[i];
}
else {
    for (int j = n.length() -1; j >= 0; j--){
        cout << n[j];
    }
    cout << ' ';
    n.clear();
}
}

for (int k = n.length() -1 ; k >= 0; k--)
cout << n[k];

cin.get();
return 0;

}

Upvotes: 0

you can use strrev(); function instead all of your for block.

Upvotes: -1

πάντα ῥεῖ
πάντα ῥεῖ

Reputation: 1

As mentioned in my comment, you're reversing the whole string by characters, but you need to split up for words and reverse:

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

using namespace std;

int main() {

    string s, n;
    getline(cin, s);
    std::istringstream iss(s);
    std::vector<string> words;
    while(iss >> n) {
        words.push_back(n);
    }

    std::reverse(words.begin(),words.end());

    for(auto word : words) {
        std::cout << word << ' ';
    }

    getchar();
    return 0;    

}

Live Demo

Upvotes: 5

Related Questions