Hi Guys,

I have a O(n) solution for this problem:

/*
* Input: "my name is ram"
* Output: "ram is name my"
* Approach: Divide the string into tokens and store the token pointers on a
* stack. Pull them up to display output.
* */
#include <string.h>
#define MAX_WORDS_PER_STRING 100
#define MAX_STRING_LENGTH 512
int stack[MAX_WORDS_PER_STRING]; // maximum 100 words
int top; // stack top pointer
void push(int Item)
{
   stack[top++] = Item;
}

int pop()
{
   return stack[--top];
}

int main()
{
   char InStr[MAX_STRING_LENGTH] = "my name is ram";
   int ii = 0, NewWord = 1, len = 0;
   //printf("Input string");
   //scanf("%s",InStr);
   len = strlen(InStr);
   while(ii < len) {
       if(InStr[ii] == ' ') {
           /* replace black space with '\0' */
           InStr[ii] = '\0';
           if(InStr[ii+1] != ' ') NewWord = 1;
       } else {
           if(NewWord) {
               push(ii);
               NewWord = 0;
           }
       }
       ii++;
   }
   /* Now print the words from stack */
   while(1) {
       ii = pop();
       printf("%s ",(char *)&InStr[ii]);
       if(ii == 0) break;
   }
}

 -Dinesh Bansal

On Wed, Jul 6, 2011 at 9:49 PM, Navneet Gupta <navneetn...@gmail.com> wrote:
> I think somebody on this thread has asked this question but i am not
> able to find that.
>
> Question was if a string is like "my name is ram", then output should
> be "ram is name my".
>
> Wrote the code for same, so sharing.
>
> #include<iostream>
> #include<string>
> using namespace std;
>
> void SwapStringChars(string &str, int pos1, int pos2)
> {
>        char ch = str[pos1];
>        str[pos1] = str[pos2];
>        str[pos2] = ch;
> }
>
> void reverseString(string &str, int left, int right)
> {
>        for(int i = left ; i <= left + (right-left)/2 ; i++)
>                SwapStringChars(str, i, right + left -i));
> }
>
> void reverseWordsInString(string &str)
> {
>        char space = ' ';
>        int len = str.length();
>        int startIndex = 0, endIndex = 0;
>        while(endIndex < len - 1)
>        {
>                while(str[endIndex] != space && endIndex < len)endIndex++;
>                reverseString(str, startIndex, endIndex-1);
>                startIndex = endIndex;
>                while(str[startIndex] == space)startIndex++;
>                endIndex = startIndex;
>        }
> }
>
> int main()
> {
>        string str;
>        cout<<"\nEnter enter the string :";
>        getline(cin,str);
>
>        //Reverse whole string at once
>        reverseString(str, 0, str.length() - 1);
>
>        //Reverse Individual words in string
>        reverseWordsInString(str);
>        cout<<str;
>        cin.get();
>        return 0;
> }
>
> --
> Regards,
> Navneet
>
> --
> You received this message because you are subscribed to the Google Groups
"Algorithm Geeks" group.
> To post to this group, send email to algogeeks@googlegroups.com.
> To unsubscribe from this group, send email to
algogeeks+unsubscr...@googlegroups.com.
> For more options, visit this group at
http://groups.google.com/group/algogeeks?hl=en.
>
>



-- 
Dinesh Bansal
The Law of Win says, "Let's not do it your way or my way; let's do it the
best way."

-- 
You received this message because you are subscribed to the Google Groups 
"Algorithm Geeks" group.
To post to this group, send email to algogeeks@googlegroups.com.
To unsubscribe from this group, send email to 
algogeeks+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/algogeeks?hl=en.

Reply via email to