Home > Software design >  Reverse Word Wise using basics of C
Reverse Word Wise using basics of C

Time:07-10

The question Goes like this ( my code in the last )

Reverse the given string word wise. That is, the last word in given string should come at 1st place, last second word at 2nd place and so on. Individual words should remain as it is.

Input format : String in a single line

Output format : Word wise reversed string in a single line

Constraints : 0 <= |S| <= 10^7 where |S| represents the length of string, S.

Sample Input 1: Welcome to Coding Ninjas

Sample Output 1: Ninjas Coding to Welcome

Sample Input 2: Always indent your code

Sample Output 2: code your indent Always

This code is in c :

void reverseStringWordWise(char input[]) 
{
    // Length
    int count=0;
    for(int i=0; input[i]!='\0'; i  )
    {
        count  ;
    }
    int len=count;
    
    //reversing the complete string
    int i=0;
    int j=len-1;
    while(i<j)
    {
        char temp=input[i];
        input[i]=input[j];
        input[j]=temp;
        i  ;
        j--;
    }
    
    //individual reverse
    
    
    int k=0;
    int a,b;
    for(;k<len;)
    {
        for(;input[k]==' ';k  )
        {
            b=k-1;
            break;
        }
        
        while(a<b)
        {
            char temp=input[a];
            input[a]=input[b];
            input[b]=temp;
        }
    }      
}

can someone help me with the logic of reversing the individual word, c or c works.

CodePudding user response:

I would get rid of the char[]s and use std::string.

Example:

#include <algorithm> // reverse
#include <iostream>
#include <sstream>   // istringstream
#include <string>

void reverseStringWordWise(const std::string& input) {
    std::istringstream is(input); // put the input in an istringstream

    // extract one word at a time from the istringstream
    for(std::string word; is >> word;) {
        std::reverse(word.begin(), word.end()); // reverse the word
        std::cout << word << ' ';               // and print it
    }
    std::cout << '\n';
}

int main() {
    reverseStringWordWise("Hello world");    
}

Output

olleH dlrow 

CodePudding user response:

You can use the standard library algorithms to shorten the code. If you've got start and end iterators, you can use std::reverse, you can use std::strlen to calculate the end iterator and you can use std::find to identify the next word boundary. Assuming every word seperator is a space character, this could result in the following algorithm

void reverseStringWordWise(char input[])
{
    if (input[0] == '\0')
    {
        return;
    }

    auto const end = input   std::strlen(input);

    std::reverse(input, end);
    
    auto wordEnd = input;

    while(true)
    {
        auto wordStart = wordEnd;
        wordEnd = std::find(wordStart, end, ' ');
        std::reverse(wordStart, wordEnd);
        if (wordEnd == end)
        {
            break;
        }
          wordEnd;
    }
}

int main() {
    char input1[] = "Welcome to Coding Ninjas";
    char input2[] = "Always indent your code";

    reverseStringWordWise(input1);
    reverseStringWordWise(input2);
    std::cout << input1 << '\n'
        << input2 << '\n';
}

CodePudding user response:

With boost, it's easier:

#include <boost/tokenizer.hpp>
#include <boost/algorithm/string/join.hpp>

std::string reverse_words(std::string s)
{
    boost::char_delimiters_separator<char> const sep(" ");
    boost::tokenizer<boost::char_delimiters_separator<char>> const words(s, sep);
    return boost::algorithm::join(std::reverse(words.begin(), words.end()), " ");
}

In C , you are expected to use algorithms and not bother with rewriting everything from scratch (unless you are writing a library such as boost). Especially, as others mentioned, reading/writing to arrays directly is most often going to end up with errors (your code is missing several boundary checks).

  • Related