I read that solution. But the same doubt as Navneet which I think you also raised i one of your posts on that thread
On Wed, Jul 6, 2011 at 10:34 PM, Navneet Gupta <[email protected]>wrote: > Saurabh, > > I understood your solution but wonder if it is purely single traversal > > In affect, you have a second traversal when you are popping the > strings from stack to form the reverse order string. > > Though the second activity is less than O(n) i.e. O(#words in string) > Nice solution, this way we can also get rid of extra spaces easily in > the actual string if that is also to be done. > > On Wed, Jul 6, 2011 at 10:16 PM, saurabh singh <[email protected]> > wrote: > > I have proposed my solution in one of the previous posts.Check the > solution > > there > > > > On Wed, Jul 6, 2011 at 10:10 PM, Tushar Bindal <[email protected]> > > wrote: > >> > >> good job > >> but how can this be done in one traversal as asked on the Adobe > Interview > >> Questions thread. > >> > >> > >> > >> On Wed, Jul 6, 2011 at 9:49 PM, Navneet Gupta <[email protected]> > >> 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 [email protected]. > >>> To unsubscribe from this group, send email to > >>> [email protected]. > >>> For more options, visit this group at > >>> http://groups.google.com/group/algogeeks?hl=en. > >>> > >> > >> > >> > >> -- > >> Tushar Bindal > >> Computer Engineering > >> Delhi College of Engineering > >> Mob: +919818442705 > >> E-Mail : [email protected] > >> Website: www.jugadengg.com > >> > >> -- > >> You received this message because you are subscribed to the Google > Groups > >> "Algorithm Geeks" group. > >> To post to this group, send email to [email protected]. > >> To unsubscribe from this group, send email to > >> [email protected]. > >> For more options, visit this group at > >> http://groups.google.com/group/algogeeks?hl=en. > > > > > > > > -- > > Saurabh Singh > > B.Tech (Computer Science) > > MNNIT ALLAHABAD > > > > > > -- > > You received this message because you are subscribed to the Google Groups > > "Algorithm Geeks" group. > > To post to this group, send email to [email protected]. > > To unsubscribe from this group, send email to > > [email protected]. > > For more options, visit this group at > > http://groups.google.com/group/algogeeks?hl=en. > > > > > > -- > 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 [email protected]. > To unsubscribe from this group, send email to > [email protected]. > For more options, visit this group at > http://groups.google.com/group/algogeeks?hl=en. > > -- Tushar Bindal Computer Engineering Delhi College of Engineering Mob: +919818442705 E-Mail : [email protected] Website: www.jugadengg.com -- You received this message because you are subscribed to the Google Groups "Algorithm Geeks" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.
