DEV Community

Shakeeb Shahid
Shakeeb Shahid

Posted on

Reversing a string but word wise! using char array. No string library.

  • Input: Reverse this word
  • Output: word this Reverse
#include <iostream>
#include<cstring>
using namespace std;

void reverseStringWordWise(char input[]) {
    // Write your code here
    int sp = strlen(input);
    int o_size = sp;
     char output[sp];
    int p = sp;
    int j = sp-1;
    int i=0;
    while(j>=0 || j==-1) // Since the first word will not have any space before it So it wont count
    {

        if(input[j]==' ' ||j==-1)
        {
            for(int k = j+1; k<p;k++) // j gets iterated from -1 to 0. Thus giving the frist word upto the the last space traced.
            {
                output[i++] = input[k];
            }
             p = j;
             j--;

            output[i++] = ' ';
        }
        else
        {
            j--;
        }

    }
  for(int l=0;l<sp;l++)
  {
      input[l] = output[l];
  }
}


int main() {
    char input[1000];
    cin.getline(input, 1000);
    reverseStringWordWise(input);
    cout << input << endl;
}
Enter fullscreen mode Exit fullscreen mode

Qodo Takeover

Introducing Qodo Gen 1.0: Transform Your Workflow with Agentic AI

Rather than just generating snippets, our agents understand your entire project context, can make decisions, use tools, and carry out tasks autonomously.

Read full post

Top comments (0)

The Most Contextual AI Development Assistant

Pieces.app image

Our centralized storage agent works on-device, unifying various developer tools to proactively capture and enrich useful materials, streamline collaboration, and solve complex problems through a contextual understanding of your unique workflow.

👥 Ideal for solo developers, teams, and cross-company projects

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay