Home > Enterprise >  Reversing each word in a string without changing the order in C
Reversing each word in a string without changing the order in C

Time:02-27

I am writing a code in C to reverse all the words in a string without changing the order of the list

This is my code:

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

int main()
{

string s = "God Ding";

int n = s.size();
    
int i = 0, j = 0;
    
while(j  < n){
    if(s[j] == ' ' || j == n-1){
        reverse(s.begin() i, s.begin() j);
        j  ;
        i = j;
    }
    else{
        j  ;
    }
}

cout << s;

return 0;

}

Expected Output: "doG gniD"

My output: doG Ding

2nd input: "Let's take LeetCode contest"

Expected Output: "s'teL ekat edoCteeL tsetnoc"

My Output: "s'teL ekat edoCteeL contest"

Everything is working fine but only the last word is not getting reversed

CodePudding user response:

You need either to detect in your if the special situation of processing the last character, or add an extra reverse after the while, if the last word was not processed (i.e. the last char was not a space).

In your case, you've added the detection of last character, but did not process the reverse before the increment, as would be the case if the word ended with a space. To correct this situation, you must take into account this special situation:

if(s[j] == ' ' || j == n-1){
    reverse(s.begin() i, s.begin() j (j==n-1?1:0));
    //                               ^ (if it's last, take  1)
    ...

CodePudding user response:

I think this would be a better implementation for your code:

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

int main()
{
    std::string s = "God Ding", reversed_string;

    std::string word;
    std::istringstream ss(s); // Declaring an in string stream

    while (std::getline(ss, word, ' ')) // Here getline gets every word in ss and stores it in the variable 'word'
    {
        // This loop reverses the variable 'word'
        for (int i = word.size() - 1; i >= 0; i--)
        {
            reversed_string.push_back(word[i]);
        }
        reversed_string.push_back(' ');
    }
    std::cout << reversed_string;

    return 0;
}

Also look up to why is "using namespace std" considered as a bad practice.

CodePudding user response:

Here you have a working example:

#include <string>
#include <algorithm>
#include <iostream>

int main()
{
    std::string s = "God Ding";
    
    int i = 0;
    int j = 0;
    
    while (i <= s.length()) {
        if (i == s.length() || s[i] == ' ') {
            std::reverse(s.begin()   j, s.begin()   i);
            j = i   1;
        }
        i  ;
    }
    
    std::cout << s << std::endl;
    
    return 0;
}

I changed a lot of things. These are the issues you need to fix:

  1. Make your loop go beyond the last char.
  2. Reverse if j (from your code) is beyond the last char.

CodePudding user response:

In order to reverse a word, you need i to index the first char, and j to index the char after the last one.

However, your loop exits whenever j indexes the char after the last one. That's why it never reverses the last word.

The simplest fix is to do one more check after the loop exits, and reverse the last word if there is one:

while(j < n) {
    if(s[j] == ' ') {  // remove the `j==n-1` test
        reverse(s.begin() i, s.begin() j);
        j  ;
        i = j;
    } else {
        j  ;
    }
}
if(i < j) {
    reverse(s.begin() i, s.begin() j);
}

The alternative would be to rewrite the loop in some way that finesses the problem. I don't really recommend this, given that you have something that almost works and that you know how to fix.

  • Related