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. Also, there is also an extra space at the end. If there is a way to maybe cancel outputting the last space; please tell me.


Sample Input:
My name is Edward

Intended Output:
Edward is name My

Output Received:
Edward is name

Here is my code so far:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#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;

}


Thanks for reading, I appreciate your help.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <iostream>
#include <string>
//#include <stdio.h>
#include <cctype> // for std::issspace

// using namespace std;

int main() {

    std::string s, n ; //, a; // consider using semantically richer names for these

    std::getline( std::cin, s );

    std::cout << '"' ; // print an opening quote

    //for( int i = s.length(); i >= 0; i-- ) {
    for( int i = s.length() - 1; i >= 0; i-- ) { // start from length - 1

        //if( s[i] != 32 ) {
        if( !std::isspace(s[i]) ) { // the value of the space character need not be 32

            n += s[i];
        }
        else {

            // print out the word (the contents of n in reverse)
            for( int j = n.length() - 1; j >= 0; j-- ) {

                // a += n[j];
                std::cout << n[j] ;
            }
            // std::cout << a << ' ';
            std::cout << ' ' ; // and then a space
            n.clear();
            //a.clear();
        }
    }

    // print the last word - the residual characters
    // that may be remaining in n (in reverse)
    for( int i = n.length() - 1 ; i >= 0 ; --i ) std::cout << n[i] ;

    std::cout << "\"\n" ; // print a closing quote and a new line

    //cin.ignore();
    //getchar();
    //return 0;
    std::cout << "\npress enter to exit program: " ;
    std::cin.get() ; // wait for the user to hit enter
}

http://coliru.stacked-crooked.com/a/5bec11505b8bde0f
Thank you for helping me refine and debug my code, I realized my problem with the extra space as well as that I needed a space to read the first word in the string.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#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;

}
Topic archived. No new replies allowed.