Re: [algogeeks] Reversing the order of words in String
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. #includeiostream #includestring 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); coutstr; 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.
Re: [algogeeks] Reversing the order of words in String
One solution can be in one traversal :- take an example:- char str[]=hello my friends; int i=strlen(str),j=i; char *srt1=malloc(sizeof(char)*i); 1. start from end of string (while i=0,i--) 2. whenever u find any space do a. strcat(srt1,srt[i]); b. srt[i]='\0'; d. srt[j-i]='space'; //replacing nulls by space, can be done using strlen(srt1) also Note:- I have not executed the code so modifications may be needed. :) On Thu, Jul 7, 2011 at 10:58 PM, Piyush Kapoor pkjee2...@gmail.com wrote: char a[20]; int l,i,j,k; int main() { char str[100]; gets(str); l=strlen(str); for(i=l-1;i=0;){ k=19; a[k]='\0'; while(i=0 str[i]!=' '){ a[--k]=str[i--]; } printf(%s,a+k); while(i=0 str[i]==' ') i--; printf( ); } } On Thu, Jul 7, 2011 at 10:37 PM, Piyush Kapoor pkjee2...@gmail.comwrote: Pls Ignore my above post.. On Thu, Jul 7, 2011 at 10:36 PM, Piyush Kapoor pkjee2...@gmail.comwrote: char a[20]; int l; int main() { char str[100]; scanf(%s,str); l=strlen(str); } -- *Regards,* *Piyush Kapoor,* *CSE-IT-BHU* -- *Regards,* *Piyush Kapoor,* *CSE-IT-BHU* -- *Regards,* *Piyush Kapoor,* *CSE-IT-BHU* -- 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. -- **Regards SAGAR PAREEK COMPUTER SCIENCE AND ENGINEERING NIT ALLAHABAD -- 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.
Re: [algogeeks] Reversing the order of words in String
and how would strlen compute the length without traversing through the array? On Tue, Jul 12, 2011 at 2:48 PM, sagar pareek sagarpar...@gmail.com wrote: One solution can be in one traversal :- take an example:- char str[]=hello my friends; int i=strlen(str),j=i; char *srt1=malloc(sizeof(char)*i); 1. start from end of string (while i=0,i--) 2. whenever u find any space do a. strcat(srt1,srt[i]); b. srt[i]='\0'; d. srt[j-i]='space'; //replacing nulls by space, can be done using strlen(srt1) also Note:- I have not executed the code so modifications may be needed. :) On Thu, Jul 7, 2011 at 10:58 PM, Piyush Kapoor pkjee2...@gmail.comwrote: char a[20]; int l,i,j,k; int main() { char str[100]; gets(str); l=strlen(str); for(i=l-1;i=0;){ k=19; a[k]='\0'; while(i=0 str[i]!=' '){ a[--k]=str[i--]; } printf(%s,a+k); while(i=0 str[i]==' ') i--; printf( ); } } On Thu, Jul 7, 2011 at 10:37 PM, Piyush Kapoor pkjee2...@gmail.comwrote: Pls Ignore my above post.. On Thu, Jul 7, 2011 at 10:36 PM, Piyush Kapoor pkjee2...@gmail.comwrote: char a[20]; int l; int main() { char str[100]; scanf(%s,str); l=strlen(str); } -- *Regards,* *Piyush Kapoor,* *CSE-IT-BHU* -- *Regards,* *Piyush Kapoor,* *CSE-IT-BHU* -- *Regards,* *Piyush Kapoor,* *CSE-IT-BHU* -- 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. -- **Regards SAGAR PAREEK COMPUTER SCIENCE AND ENGINEERING NIT ALLAHABAD -- 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. -- 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 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.
Re: [algogeeks] Reversing the order of words in String
@Saurabh, just to let you know, compiler actually does that. Without traversing the array (read specifying the array size), you are able to free up all the memory being consumed by simply writing delete []a. On Tue, Jul 12, 2011 at 10:13 PM, saurabh singh saurab...@gmail.com wrote: and how would strlen compute the length without traversing through the array? On Tue, Jul 12, 2011 at 2:48 PM, sagar pareek sagarpar...@gmail.com wrote: One solution can be in one traversal :- take an example:- char str[]=hello my friends; int i=strlen(str),j=i; char *srt1=malloc(sizeof(char)*i); 1. start from end of string (while i=0,i--) 2. whenever u find any space do a. strcat(srt1,srt[i]); b. srt[i]='\0'; d. srt[j-i]='space'; //replacing nulls by space, can be done using strlen(srt1) also Note:- I have not executed the code so modifications may be needed. :) On Thu, Jul 7, 2011 at 10:58 PM, Piyush Kapoor pkjee2...@gmail.com wrote: char a[20]; int l,i,j,k; int main() { char str[100]; gets(str); l=strlen(str); for(i=l-1;i=0;){ k=19; a[k]='\0'; while(i=0 str[i]!=' '){ a[--k]=str[i--]; } printf(%s,a+k); while(i=0 str[i]==' ') i--; printf( ); } } On Thu, Jul 7, 2011 at 10:37 PM, Piyush Kapoor pkjee2...@gmail.com wrote: Pls Ignore my above post.. On Thu, Jul 7, 2011 at 10:36 PM, Piyush Kapoor pkjee2...@gmail.com wrote: char a[20]; int l; int main() { char str[100]; scanf(%s,str); l=strlen(str); } -- Regards, Piyush Kapoor, CSE-IT-BHU -- Regards, Piyush Kapoor, CSE-IT-BHU -- Regards, Piyush Kapoor, CSE-IT-BHU -- 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. -- Regards SAGAR PAREEK COMPUTER SCIENCE AND ENGINEERING NIT ALLAHABAD -- 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. -- 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 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. -- 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.
Re: [algogeeks] Reversing the order of words in String
can strlen do the same job? I doubt...? On Tue, Jul 12, 2011 at 10:51 PM, Navneet Gupta navneetn...@gmail.comwrote: @Saurabh, just to let you know, compiler actually does that. Without traversing the array (read specifying the array size), you are able to free up all the memory being consumed by simply writing delete []a. On Tue, Jul 12, 2011 at 10:13 PM, saurabh singh saurab...@gmail.com wrote: and how would strlen compute the length without traversing through the array? On Tue, Jul 12, 2011 at 2:48 PM, sagar pareek sagarpar...@gmail.com wrote: One solution can be in one traversal :- take an example:- char str[]=hello my friends; int i=strlen(str),j=i; char *srt1=malloc(sizeof(char)*i); 1. start from end of string (while i=0,i--) 2. whenever u find any space do a. strcat(srt1,srt[i]); b. srt[i]='\0'; d. srt[j-i]='space'; //replacing nulls by space, can be done using strlen(srt1) also Note:- I have not executed the code so modifications may be needed. :) On Thu, Jul 7, 2011 at 10:58 PM, Piyush Kapoor pkjee2...@gmail.com wrote: char a[20]; int l,i,j,k; int main() { char str[100]; gets(str); l=strlen(str); for(i=l-1;i=0;){ k=19; a[k]='\0'; while(i=0 str[i]!=' '){ a[--k]=str[i--]; } printf(%s,a+k); while(i=0 str[i]==' ') i--; printf( ); } } On Thu, Jul 7, 2011 at 10:37 PM, Piyush Kapoor pkjee2...@gmail.com wrote: Pls Ignore my above post.. On Thu, Jul 7, 2011 at 10:36 PM, Piyush Kapoor pkjee2...@gmail.com wrote: char a[20]; int l; int main() { char str[100]; scanf(%s,str); l=strlen(str); } -- Regards, Piyush Kapoor, CSE-IT-BHU -- Regards, Piyush Kapoor, CSE-IT-BHU -- Regards, Piyush Kapoor, CSE-IT-BHU -- 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. -- Regards SAGAR PAREEK COMPUTER SCIENCE AND ENGINEERING NIT ALLAHABAD -- 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. -- 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 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. -- 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. -- 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 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.
Re: [algogeeks] Reversing the order of words in String
My bad in not being clear. I only meant it is possible to know the size of strings without traversing them as compiler does that but I am not sure if is it possible for APIs to do the same. Compiler writes that size in some memory location and use it during delete []str On Tue, Jul 12, 2011 at 10:54 PM, saurabh singh saurab...@gmail.com wrote: can strlen do the same job? I doubt...? On Tue, Jul 12, 2011 at 10:51 PM, Navneet Gupta navneetn...@gmail.com wrote: @Saurabh, just to let you know, compiler actually does that. Without traversing the array (read specifying the array size), you are able to free up all the memory being consumed by simply writing delete []a. On Tue, Jul 12, 2011 at 10:13 PM, saurabh singh saurab...@gmail.com wrote: and how would strlen compute the length without traversing through the array? On Tue, Jul 12, 2011 at 2:48 PM, sagar pareek sagarpar...@gmail.com wrote: One solution can be in one traversal :- take an example:- char str[]=hello my friends; int i=strlen(str),j=i; char *srt1=malloc(sizeof(char)*i); 1. start from end of string (while i=0,i--) 2. whenever u find any space do a. strcat(srt1,srt[i]); b. srt[i]='\0'; d. srt[j-i]='space'; //replacing nulls by space, can be done using strlen(srt1) also Note:- I have not executed the code so modifications may be needed. :) On Thu, Jul 7, 2011 at 10:58 PM, Piyush Kapoor pkjee2...@gmail.com wrote: char a[20]; int l,i,j,k; int main() { char str[100]; gets(str); l=strlen(str); for(i=l-1;i=0;){ k=19; a[k]='\0'; while(i=0 str[i]!=' '){ a[--k]=str[i--]; } printf(%s,a+k); while(i=0 str[i]==' ') i--; printf( ); } } On Thu, Jul 7, 2011 at 10:37 PM, Piyush Kapoor pkjee2...@gmail.com wrote: Pls Ignore my above post.. On Thu, Jul 7, 2011 at 10:36 PM, Piyush Kapoor pkjee2...@gmail.com wrote: char a[20]; int l; int main() { char str[100]; scanf(%s,str); l=strlen(str); } -- Regards, Piyush Kapoor, CSE-IT-BHU -- Regards, Piyush Kapoor, CSE-IT-BHU -- Regards, Piyush Kapoor, CSE-IT-BHU -- 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. -- Regards SAGAR PAREEK COMPUTER SCIENCE AND ENGINEERING NIT ALLAHABAD -- 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. -- 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 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. -- 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. -- 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 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. -- 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.
Re: [algogeeks] Reversing the order of words in String
@Navneet This is my version of how delete and strlen must be working.Sorry the presentation is not in correspondence with the style of the group.Apologies for that, *delete[]* Suppose u are a compiler and u see a line like delete[] a,that will mean that Hey the programmer borrowed some memory from heap,he is now giving it back,let me check with my table how much he took (My friend new must have put it somewhere in the records).Ok done from the address of a to a+no_of_blocks_allocated I have given back to heap and recorded it so. (2 steps).But the programmer may be greedy(and foolish) enough to use that memory again,so I wont allow that to happen till he makes a proper request.So here I blacklist this memory for further access till he grants another request for it. So delete[] can do the work in 2 or 3 steps of assembler routine no matter array is of 10 or 100. Now see for *strlen*: when the coder uses strlen he actually wants to say Hey listen I am giving you the beginning address of a bunch of random characters.I guarantee you there is a bad guy with value 0 somewhere in there.Find me how many skips you have to make from the starting address to get to that guy. Now the poor function is left with no other option to search for that bad guy looking in every box and counting how many times he skipped.Since he can't make assumptions that's the only way he can obey his caller. If you know any better way of searching for the bad guy '\0' let me know please. -- 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 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.
Re: [algogeeks] Reversing the order of words in String
And sorry for the late response but my original solution in my previous post doesn't actually required 2 traversals of the complete string. A more clearer algo:- 1.Read original string character by character appending to another string.The string implementation can be that of the std. C++ string. 2.if space or null char encountered,push the *string(and not individual chars) *in the stack. 3.Now if null occured.pop each string from the stack till it is empty. This way we do o(n+m) iterations where m is the number of words in the string.In real world scenario mn. Still its not the best solution but I am not traversing the string twice as some earlier members have commented. -- 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 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.
Re: [algogeeks] Reversing the order of words in String
@Navneettake a look at the solution below and tell if there is any bug in it... *#include string.h typedef struct revll { char s[100]; struct revll *next; }revll; revll *rev_str(char *a) { char temp[100]; int i,len=strlen(a),j=0; revll *head,*p; head=NULL; for(i=0;ilen;i++) { if(a[i]!=' ') { temp[j++] = a[i]; } else { temp[j] = '\0'; p = (revll *)malloc(sizeof(revll)); p-next = head; strcpy(p-s,temp); head = p; j=0; } } /*for last word*/ temp[j] = '\0'; p = (revll *)malloc(sizeof(revll)); p-next = head; strcpy(p-s,temp); head = p; return head; } main() { char a[100]; revll *head; gets(a); head = rev_str(a); while(head) { printf(%s-,head-s); head=head-next; } system(pause); }* On Thu, Jul 7, 2011 at 9:10 AM, Navneet Gupta navneetn...@gmail.com wrote: @Piyush, could you elaborate your approach with Linked List? From what i am getting, even with Linked List, you would need two traversals at least. On Thu, Jul 7, 2011 at 2:07 AM, Piyush Sinha ecstasy.piy...@gmail.com wrote: Can we do it using linked list if ONE TIME TRAVERSAL is a constraint?? On 7/6/11, Tushar Bindal tushicom...@gmail.com wrote: 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 navneetn...@gmail.com 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 saurab...@gmail.com 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 tushicom...@gmail.com 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 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. #includeiostream #includestring 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); coutstr; 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. -- Tushar Bindal Computer Engineering Delhi College of Engineering Mob: +919818442705 E-Mail : tushicom...@gmail.com 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 algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more
Re: [algogeeks] Reversing the order of words in String
Off Topic: Sorry for the diversion, but I was just wondering how easy it has become to code in languages other than c. Here is the code i wrote for the above mentioned problem in Python. It takes command line arg as string. something like vishal@ubuntu:~/progs/python\ 02:45:07 PM $ cat rev.py #!/usr/bin/python import sys strn= for i in (sys.argv[1].split())[::-1]: strn+=i+ print strn vishal@ubuntu:~/progs/python\ 02:45:09 PM $ ./rev.py This is a new world world new a is This vishal@ubuntu:~/progs/python\ 02:47:15 PM $ On Thu, Jul 7, 2011 at 1:19 PM, Piyush Sinha ecstasy.piy...@gmail.com wrote: @Navneettake a look at the solution below and tell if there is any bug in it... #include string.h typedef struct revll { char s[100]; struct revll *next; }revll; revll *rev_str(char *a) { char temp[100]; int i,len=strlen(a),j=0; revll *head,*p; head=NULL; for(i=0;ilen;i++) { if(a[i]!=' ') { temp[j++] = a[i]; } else { temp[j] = '\0'; p = (revll *)malloc(sizeof(revll)); p-next = head; strcpy(p-s,temp); head = p; j=0; } } /*for last word*/ temp[j] = '\0'; p = (revll *)malloc(sizeof(revll)); p-next = head; strcpy(p-s,temp); head = p; return head; } main() { char a[100]; revll *head; gets(a); head = rev_str(a); while(head) { printf(%s-,head-s); head=head-next; } system(pause); } On Thu, Jul 7, 2011 at 9:10 AM, Navneet Gupta navneetn...@gmail.com wrote: @Piyush, could you elaborate your approach with Linked List? From what i am getting, even with Linked List, you would need two traversals at least. On Thu, Jul 7, 2011 at 2:07 AM, Piyush Sinha ecstasy.piy...@gmail.com wrote: Can we do it using linked list if ONE TIME TRAVERSAL is a constraint?? On 7/6/11, Tushar Bindal tushicom...@gmail.com wrote: 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 navneetn...@gmail.comwrote: 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 saurab...@gmail.com 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 tushicom...@gmail.com 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 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. #includeiostream #includestring 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); coutstr; 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
Re: [algogeeks] Reversing the order of words in String
@Vishal, can you see if your program works well for more than single space between words? Not sure how split functions helps. BTW, Perl also is very strong language for string manipulations. (Specially designed for efficient string operations) On Thu, Jul 7, 2011 at 2:48 PM, Vishal Thanki vishaltha...@gmail.com wrote: Off Topic: Sorry for the diversion, but I was just wondering how easy it has become to code in languages other than c. Here is the code i wrote for the above mentioned problem in Python. It takes command line arg as string. something like vishal@ubuntu:~/progs/python\ 02:45:07 PM $ cat rev.py #!/usr/bin/python import sys strn= for i in (sys.argv[1].split())[::-1]: strn+=i+ print strn vishal@ubuntu:~/progs/python\ 02:45:09 PM $ ./rev.py This is a new world world new a is This vishal@ubuntu:~/progs/python\ 02:47:15 PM $ On Thu, Jul 7, 2011 at 1:19 PM, Piyush Sinha ecstasy.piy...@gmail.com wrote: @Navneettake a look at the solution below and tell if there is any bug in it... #include string.h typedef struct revll { char s[100]; struct revll *next; }revll; revll *rev_str(char *a) { char temp[100]; int i,len=strlen(a),j=0; revll *head,*p; head=NULL; for(i=0;ilen;i++) { if(a[i]!=' ') { temp[j++] = a[i]; } else { temp[j] = '\0'; p = (revll *)malloc(sizeof(revll)); p-next = head; strcpy(p-s,temp); head = p; j=0; } } /*for last word*/ temp[j] = '\0'; p = (revll *)malloc(sizeof(revll)); p-next = head; strcpy(p-s,temp); head = p; return head; } main() { char a[100]; revll *head; gets(a); head = rev_str(a); while(head) { printf(%s-,head-s); head=head-next; } system(pause); } On Thu, Jul 7, 2011 at 9:10 AM, Navneet Gupta navneetn...@gmail.com wrote: @Piyush, could you elaborate your approach with Linked List? From what i am getting, even with Linked List, you would need two traversals at least. On Thu, Jul 7, 2011 at 2:07 AM, Piyush Sinha ecstasy.piy...@gmail.com wrote: Can we do it using linked list if ONE TIME TRAVERSAL is a constraint?? On 7/6/11, Tushar Bindal tushicom...@gmail.com wrote: 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 navneetn...@gmail.comwrote: 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 saurab...@gmail.com 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 tushicom...@gmail.com 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 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. #includeiostream #includestring 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);
Re: [algogeeks] Reversing the order of words in String
@Navneet, it works with multiple spaces between words.. And here is the two line solution :) import sys print .join((sys.argv[1].split())[::-1]) On Thu, Jul 7, 2011 at 3:12 PM, Navneet Gupta navneetn...@gmail.com wrote: @Vishal, can you see if your program works well for more than single space between words? Not sure how split functions helps. BTW, Perl also is very strong language for string manipulations. (Specially designed for efficient string operations) On Thu, Jul 7, 2011 at 2:48 PM, Vishal Thanki vishaltha...@gmail.com wrote: Off Topic: Sorry for the diversion, but I was just wondering how easy it has become to code in languages other than c. Here is the code i wrote for the above mentioned problem in Python. It takes command line arg as string. something like vishal@ubuntu:~/progs/python\ 02:45:07 PM $ cat rev.py #!/usr/bin/python import sys strn= for i in (sys.argv[1].split())[::-1]: strn+=i+ print strn vishal@ubuntu:~/progs/python\ 02:45:09 PM $ ./rev.py This is a new world world new a is This vishal@ubuntu:~/progs/python\ 02:47:15 PM $ On Thu, Jul 7, 2011 at 1:19 PM, Piyush Sinha ecstasy.piy...@gmail.com wrote: @Navneettake a look at the solution below and tell if there is any bug in it... #include string.h typedef struct revll { char s[100]; struct revll *next; }revll; revll *rev_str(char *a) { char temp[100]; int i,len=strlen(a),j=0; revll *head,*p; head=NULL; for(i=0;ilen;i++) { if(a[i]!=' ') { temp[j++] = a[i]; } else { temp[j] = '\0'; p = (revll *)malloc(sizeof(revll)); p-next = head; strcpy(p-s,temp); head = p; j=0; } } /*for last word*/ temp[j] = '\0'; p = (revll *)malloc(sizeof(revll)); p-next = head; strcpy(p-s,temp); head = p; return head; } main() { char a[100]; revll *head; gets(a); head = rev_str(a); while(head) { printf(%s-,head-s); head=head-next; } system(pause); } On Thu, Jul 7, 2011 at 9:10 AM, Navneet Gupta navneetn...@gmail.com wrote: @Piyush, could you elaborate your approach with Linked List? From what i am getting, even with Linked List, you would need two traversals at least. On Thu, Jul 7, 2011 at 2:07 AM, Piyush Sinha ecstasy.piy...@gmail.com wrote: Can we do it using linked list if ONE TIME TRAVERSAL is a constraint?? On 7/6/11, Tushar Bindal tushicom...@gmail.com wrote: 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 navneetn...@gmail.comwrote: 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 saurab...@gmail.com 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 tushicom...@gmail.com 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 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. #includeiostream #includestring 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 =
Re: [algogeeks] Reversing the order of words in String
@Vishal, Don't confuse printing in reverse with actually modifying the actual string to reverse word order in it :) On Thu, Jul 7, 2011 at 3:34 PM, Vishal Thanki vishaltha...@gmail.com wrote: @Navneet, it works with multiple spaces between words.. And here is the two line solution :) import sys print .join((sys.argv[1].split())[::-1]) On Thu, Jul 7, 2011 at 3:12 PM, Navneet Gupta navneetn...@gmail.com wrote: @Vishal, can you see if your program works well for more than single space between words? Not sure how split functions helps. BTW, Perl also is very strong language for string manipulations. (Specially designed for efficient string operations) On Thu, Jul 7, 2011 at 2:48 PM, Vishal Thanki vishaltha...@gmail.com wrote: Off Topic: Sorry for the diversion, but I was just wondering how easy it has become to code in languages other than c. Here is the code i wrote for the above mentioned problem in Python. It takes command line arg as string. something like vishal@ubuntu:~/progs/python\ 02:45:07 PM $ cat rev.py #!/usr/bin/python import sys strn= for i in (sys.argv[1].split())[::-1]: strn+=i+ print strn vishal@ubuntu:~/progs/python\ 02:45:09 PM $ ./rev.py This is a new world world new a is This vishal@ubuntu:~/progs/python\ 02:47:15 PM $ On Thu, Jul 7, 2011 at 1:19 PM, Piyush Sinha ecstasy.piy...@gmail.com wrote: @Navneettake a look at the solution below and tell if there is any bug in it... #include string.h typedef struct revll { char s[100]; struct revll *next; }revll; revll *rev_str(char *a) { char temp[100]; int i,len=strlen(a),j=0; revll *head,*p; head=NULL; for(i=0;ilen;i++) { if(a[i]!=' ') { temp[j++] = a[i]; } else { temp[j] = '\0'; p = (revll *)malloc(sizeof(revll)); p-next = head; strcpy(p-s,temp); head = p; j=0; } } /*for last word*/ temp[j] = '\0'; p = (revll *)malloc(sizeof(revll)); p-next = head; strcpy(p-s,temp); head = p; return head; } main() { char a[100]; revll *head; gets(a); head = rev_str(a); while(head) { printf(%s-,head-s); head=head-next; } system(pause); } On Thu, Jul 7, 2011 at 9:10 AM, Navneet Gupta navneetn...@gmail.com wrote: @Piyush, could you elaborate your approach with Linked List? From what i am getting, even with Linked List, you would need two traversals at least. On Thu, Jul 7, 2011 at 2:07 AM, Piyush Sinha ecstasy.piy...@gmail.com wrote: Can we do it using linked list if ONE TIME TRAVERSAL is a constraint?? On 7/6/11, Tushar Bindal tushicom...@gmail.com wrote: 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 navneetn...@gmail.comwrote: 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 saurab...@gmail.com 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 tushicom...@gmail.com 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 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. #includeiostream #includestring 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++;
Re: [algogeeks] Reversing the order of words in String
yea, expression .join((sys.argv[1].split())[::-1]) will return the string!! On Thu, Jul 7, 2011 at 3:36 PM, Navneet Gupta navneetn...@gmail.com wrote: @Vishal, Don't confuse printing in reverse with actually modifying the actual string to reverse word order in it :) On Thu, Jul 7, 2011 at 3:34 PM, Vishal Thanki vishaltha...@gmail.com wrote: @Navneet, it works with multiple spaces between words.. And here is the two line solution :) import sys print .join((sys.argv[1].split())[::-1]) On Thu, Jul 7, 2011 at 3:12 PM, Navneet Gupta navneetn...@gmail.com wrote: @Vishal, can you see if your program works well for more than single space between words? Not sure how split functions helps. BTW, Perl also is very strong language for string manipulations. (Specially designed for efficient string operations) On Thu, Jul 7, 2011 at 2:48 PM, Vishal Thanki vishaltha...@gmail.com wrote: Off Topic: Sorry for the diversion, but I was just wondering how easy it has become to code in languages other than c. Here is the code i wrote for the above mentioned problem in Python. It takes command line arg as string. something like vishal@ubuntu:~/progs/python\ 02:45:07 PM $ cat rev.py #!/usr/bin/python import sys strn= for i in (sys.argv[1].split())[::-1]: strn+=i+ print strn vishal@ubuntu:~/progs/python\ 02:45:09 PM $ ./rev.py This is a new world world new a is This vishal@ubuntu:~/progs/python\ 02:47:15 PM $ On Thu, Jul 7, 2011 at 1:19 PM, Piyush Sinha ecstasy.piy...@gmail.com wrote: @Navneettake a look at the solution below and tell if there is any bug in it... #include string.h typedef struct revll { char s[100]; struct revll *next; }revll; revll *rev_str(char *a) { char temp[100]; int i,len=strlen(a),j=0; revll *head,*p; head=NULL; for(i=0;ilen;i++) { if(a[i]!=' ') { temp[j++] = a[i]; } else { temp[j] = '\0'; p = (revll *)malloc(sizeof(revll)); p-next = head; strcpy(p-s,temp); head = p; j=0; } } /*for last word*/ temp[j] = '\0'; p = (revll *)malloc(sizeof(revll)); p-next = head; strcpy(p-s,temp); head = p; return head; } main() { char a[100]; revll *head; gets(a); head = rev_str(a); while(head) { printf(%s-,head-s); head=head-next; } system(pause); } On Thu, Jul 7, 2011 at 9:10 AM, Navneet Gupta navneetn...@gmail.com wrote: @Piyush, could you elaborate your approach with Linked List? From what i am getting, even with Linked List, you would need two traversals at least. On Thu, Jul 7, 2011 at 2:07 AM, Piyush Sinha ecstasy.piy...@gmail.com wrote: Can we do it using linked list if ONE TIME TRAVERSAL is a constraint?? On 7/6/11, Tushar Bindal tushicom...@gmail.com wrote: 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 navneetn...@gmail.comwrote: 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 saurab...@gmail.com 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 tushicom...@gmail.com 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 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. #includeiostream #includestring 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();
Re: [algogeeks] Reversing the order of words in String
#!/usr/bin/python import sys string=sys.argv[1] string= .join((string.split())[::-1]) print string How does this sound? :P On Thu, Jul 7, 2011 at 3:43 PM, Vishal Thanki vishaltha...@gmail.com wrote: yea, expression .join((sys.argv[1].split())[::-1]) will return the string!! On Thu, Jul 7, 2011 at 3:36 PM, Navneet Gupta navneetn...@gmail.com wrote: @Vishal, Don't confuse printing in reverse with actually modifying the actual string to reverse word order in it :) On Thu, Jul 7, 2011 at 3:34 PM, Vishal Thanki vishaltha...@gmail.com wrote: @Navneet, it works with multiple spaces between words.. And here is the two line solution :) import sys print .join((sys.argv[1].split())[::-1]) On Thu, Jul 7, 2011 at 3:12 PM, Navneet Gupta navneetn...@gmail.com wrote: @Vishal, can you see if your program works well for more than single space between words? Not sure how split functions helps. BTW, Perl also is very strong language for string manipulations. (Specially designed for efficient string operations) On Thu, Jul 7, 2011 at 2:48 PM, Vishal Thanki vishaltha...@gmail.com wrote: Off Topic: Sorry for the diversion, but I was just wondering how easy it has become to code in languages other than c. Here is the code i wrote for the above mentioned problem in Python. It takes command line arg as string. something like vishal@ubuntu:~/progs/python\ 02:45:07 PM $ cat rev.py #!/usr/bin/python import sys strn= for i in (sys.argv[1].split())[::-1]: strn+=i+ print strn vishal@ubuntu:~/progs/python\ 02:45:09 PM $ ./rev.py This is a new world world new a is This vishal@ubuntu:~/progs/python\ 02:47:15 PM $ On Thu, Jul 7, 2011 at 1:19 PM, Piyush Sinha ecstasy.piy...@gmail.com wrote: @Navneettake a look at the solution below and tell if there is any bug in it... #include string.h typedef struct revll { char s[100]; struct revll *next; }revll; revll *rev_str(char *a) { char temp[100]; int i,len=strlen(a),j=0; revll *head,*p; head=NULL; for(i=0;ilen;i++) { if(a[i]!=' ') { temp[j++] = a[i]; } else { temp[j] = '\0'; p = (revll *)malloc(sizeof(revll)); p-next = head; strcpy(p-s,temp); head = p; j=0; } } /*for last word*/ temp[j] = '\0'; p = (revll *)malloc(sizeof(revll)); p-next = head; strcpy(p-s,temp); head = p; return head; } main() { char a[100]; revll *head; gets(a); head = rev_str(a); while(head) { printf(%s-,head-s); head=head-next; } system(pause); } On Thu, Jul 7, 2011 at 9:10 AM, Navneet Gupta navneetn...@gmail.com wrote: @Piyush, could you elaborate your approach with Linked List? From what i am getting, even with Linked List, you would need two traversals at least. On Thu, Jul 7, 2011 at 2:07 AM, Piyush Sinha ecstasy.piy...@gmail.com wrote: Can we do it using linked list if ONE TIME TRAVERSAL is a constraint?? On 7/6/11, Tushar Bindal tushicom...@gmail.com wrote: 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 navneetn...@gmail.comwrote: 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 saurab...@gmail.com 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 tushicom...@gmail.com 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 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. #includeiostream #includestring 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 =
Re: [algogeeks] Reversing the order of words in String
I meant, having the result in same string which was used as param. Is that the case? I think the below will use a separate string. On Thu, Jul 7, 2011 at 3:43 PM, Vishal Thanki vishaltha...@gmail.com wrote: yea, expression .join((sys.argv[1].split())[::-1]) will return the string!! On Thu, Jul 7, 2011 at 3:36 PM, Navneet Gupta navneetn...@gmail.com wrote: @Vishal, Don't confuse printing in reverse with actually modifying the actual string to reverse word order in it :) On Thu, Jul 7, 2011 at 3:34 PM, Vishal Thanki vishaltha...@gmail.com wrote: @Navneet, it works with multiple spaces between words.. And here is the two line solution :) import sys print .join((sys.argv[1].split())[::-1]) On Thu, Jul 7, 2011 at 3:12 PM, Navneet Gupta navneetn...@gmail.com wrote: @Vishal, can you see if your program works well for more than single space between words? Not sure how split functions helps. BTW, Perl also is very strong language for string manipulations. (Specially designed for efficient string operations) On Thu, Jul 7, 2011 at 2:48 PM, Vishal Thanki vishaltha...@gmail.com wrote: Off Topic: Sorry for the diversion, but I was just wondering how easy it has become to code in languages other than c. Here is the code i wrote for the above mentioned problem in Python. It takes command line arg as string. something like vishal@ubuntu:~/progs/python\ 02:45:07 PM $ cat rev.py #!/usr/bin/python import sys strn= for i in (sys.argv[1].split())[::-1]: strn+=i+ print strn vishal@ubuntu:~/progs/python\ 02:45:09 PM $ ./rev.py This is a new world world new a is This vishal@ubuntu:~/progs/python\ 02:47:15 PM $ On Thu, Jul 7, 2011 at 1:19 PM, Piyush Sinha ecstasy.piy...@gmail.com wrote: @Navneettake a look at the solution below and tell if there is any bug in it... #include string.h typedef struct revll { char s[100]; struct revll *next; }revll; revll *rev_str(char *a) { char temp[100]; int i,len=strlen(a),j=0; revll *head,*p; head=NULL; for(i=0;ilen;i++) { if(a[i]!=' ') { temp[j++] = a[i]; } else { temp[j] = '\0'; p = (revll *)malloc(sizeof(revll)); p-next = head; strcpy(p-s,temp); head = p; j=0; } } /*for last word*/ temp[j] = '\0'; p = (revll *)malloc(sizeof(revll)); p-next = head; strcpy(p-s,temp); head = p; return head; } main() { char a[100]; revll *head; gets(a); head = rev_str(a); while(head) { printf(%s-,head-s); head=head-next; } system(pause); } On Thu, Jul 7, 2011 at 9:10 AM, Navneet Gupta navneetn...@gmail.com wrote: @Piyush, could you elaborate your approach with Linked List? From what i am getting, even with Linked List, you would need two traversals at least. On Thu, Jul 7, 2011 at 2:07 AM, Piyush Sinha ecstasy.piy...@gmail.com wrote: Can we do it using linked list if ONE TIME TRAVERSAL is a constraint?? On 7/6/11, Tushar Bindal tushicom...@gmail.com wrote: 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 navneetn...@gmail.comwrote: 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 saurab...@gmail.com 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 tushicom...@gmail.com 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 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. #includeiostream #includestring 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 =
Re: [algogeeks] Reversing the order of words in String
Okay, I think NO EXTRA SPACE is what i should have mentioned clearly. Anyways dude, i appreciate the point of simplicity which you are trying to show. On Thu, Jul 7, 2011 at 3:45 PM, Navneet Gupta navneetn...@gmail.com wrote: I meant, having the result in same string which was used as param. Is that the case? I think the below will use a separate string. On Thu, Jul 7, 2011 at 3:43 PM, Vishal Thanki vishaltha...@gmail.com wrote: yea, expression .join((sys.argv[1].split())[::-1]) will return the string!! On Thu, Jul 7, 2011 at 3:36 PM, Navneet Gupta navneetn...@gmail.com wrote: @Vishal, Don't confuse printing in reverse with actually modifying the actual string to reverse word order in it :) On Thu, Jul 7, 2011 at 3:34 PM, Vishal Thanki vishaltha...@gmail.com wrote: @Navneet, it works with multiple spaces between words.. And here is the two line solution :) import sys print .join((sys.argv[1].split())[::-1]) On Thu, Jul 7, 2011 at 3:12 PM, Navneet Gupta navneetn...@gmail.com wrote: @Vishal, can you see if your program works well for more than single space between words? Not sure how split functions helps. BTW, Perl also is very strong language for string manipulations. (Specially designed for efficient string operations) On Thu, Jul 7, 2011 at 2:48 PM, Vishal Thanki vishaltha...@gmail.com wrote: Off Topic: Sorry for the diversion, but I was just wondering how easy it has become to code in languages other than c. Here is the code i wrote for the above mentioned problem in Python. It takes command line arg as string. something like vishal@ubuntu:~/progs/python\ 02:45:07 PM $ cat rev.py #!/usr/bin/python import sys strn= for i in (sys.argv[1].split())[::-1]: strn+=i+ print strn vishal@ubuntu:~/progs/python\ 02:45:09 PM $ ./rev.py This is a new world world new a is This vishal@ubuntu:~/progs/python\ 02:47:15 PM $ On Thu, Jul 7, 2011 at 1:19 PM, Piyush Sinha ecstasy.piy...@gmail.com wrote: @Navneettake a look at the solution below and tell if there is any bug in it... #include string.h typedef struct revll { char s[100]; struct revll *next; }revll; revll *rev_str(char *a) { char temp[100]; int i,len=strlen(a),j=0; revll *head,*p; head=NULL; for(i=0;ilen;i++) { if(a[i]!=' ') { temp[j++] = a[i]; } else { temp[j] = '\0'; p = (revll *)malloc(sizeof(revll)); p-next = head; strcpy(p-s,temp); head = p; j=0; } } /*for last word*/ temp[j] = '\0'; p = (revll *)malloc(sizeof(revll)); p-next = head; strcpy(p-s,temp); head = p; return head; } main() { char a[100]; revll *head; gets(a); head = rev_str(a); while(head) { printf(%s-,head-s); head=head-next; } system(pause); } On Thu, Jul 7, 2011 at 9:10 AM, Navneet Gupta navneetn...@gmail.com wrote: @Piyush, could you elaborate your approach with Linked List? From what i am getting, even with Linked List, you would need two traversals at least. On Thu, Jul 7, 2011 at 2:07 AM, Piyush Sinha ecstasy.piy...@gmail.com wrote: Can we do it using linked list if ONE TIME TRAVERSAL is a constraint?? On 7/6/11, Tushar Bindal tushicom...@gmail.com wrote: 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 navneetn...@gmail.comwrote: 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 saurab...@gmail.com 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 tushicom...@gmail.com 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 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. #includeiostream #includestring using namespace
Re: [algogeeks] Reversing the order of words in String
char a[20]; int l; int main() { char str[100]; scanf(%s,str); l=strlen(str); } -- *Regards,* *Piyush Kapoor,* *CSE-IT-BHU* -- 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.
Re: [algogeeks] Reversing the order of words in String
Pls Ignore my above post.. On Thu, Jul 7, 2011 at 10:36 PM, Piyush Kapoor pkjee2...@gmail.com wrote: char a[20]; int l; int main() { char str[100]; scanf(%s,str); l=strlen(str); } -- *Regards,* *Piyush Kapoor,* *CSE-IT-BHU* -- *Regards,* *Piyush Kapoor,* *CSE-IT-BHU* -- 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.
Re: [algogeeks] Reversing the order of words in String
char a[20]; int l,i,j,k; int main() { char str[100]; gets(str); l=strlen(str); for(i=l-1;i=0;){ k=19; a[k]='\0'; while(i=0 str[i]!=' '){ a[--k]=str[i--]; } printf(%s,a+k); while(i=0 str[i]==' ') i--; printf( ); } } On Thu, Jul 7, 2011 at 10:37 PM, Piyush Kapoor pkjee2...@gmail.com wrote: Pls Ignore my above post.. On Thu, Jul 7, 2011 at 10:36 PM, Piyush Kapoor pkjee2...@gmail.comwrote: char a[20]; int l; int main() { char str[100]; scanf(%s,str); l=strlen(str); } -- *Regards,* *Piyush Kapoor,* *CSE-IT-BHU* -- *Regards,* *Piyush Kapoor,* *CSE-IT-BHU* -- *Regards,* *Piyush Kapoor,* *CSE-IT-BHU* -- 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.
Re: [algogeeks] Reversing the order of words in String
good job but how can this be done in one traversal as asked on the Adobe Interview Questions threadhttps://groups.google.com/forum/#%21msg/algogeeks/oEL8z4wwMJY/FAVdr2McPqIJ . 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. #includeiostream #includestring 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); coutstr; 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. -- Tushar Bindal Computer Engineering Delhi College of Engineering Mob: +919818442705 E-Mail : tushicom...@gmail.com 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 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.
Re: [algogeeks] Reversing the order of words in String
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 tushicom...@gmail.comwrote: good job but how can this be done in one traversal as asked on the Adobe Interview Questions threadhttps://groups.google.com/forum/#%21msg/algogeeks/oEL8z4wwMJY/FAVdr2McPqIJ . On Wed, Jul 6, 2011 at 9:49 PM, Navneet Gupta navneetn...@gmail.comwrote: 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. #includeiostream #includestring 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); coutstr; 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. -- Tushar Bindal Computer Engineering Delhi College of Engineering Mob: +919818442705 E-Mail : tushicom...@gmail.com 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 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. -- 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 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.
Re: [algogeeks] Reversing the order of words in String
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 saurab...@gmail.com 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 tushicom...@gmail.com 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 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. #includeiostream #includestring 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); coutstr; 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. -- Tushar Bindal Computer Engineering Delhi College of Engineering Mob: +919818442705 E-Mail : tushicom...@gmail.com 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 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. -- 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 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. -- 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.
Re: [algogeeks] Reversing the order of words in String
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 navneetn...@gmail.comwrote: 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 saurab...@gmail.com 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 tushicom...@gmail.com 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 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. #includeiostream #includestring 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); coutstr; 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. -- Tushar Bindal Computer Engineering Delhi College of Engineering Mob: +919818442705 E-Mail : tushicom...@gmail.com 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 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. -- 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 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. -- 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. -- Tushar Bindal Computer Engineering Delhi College of Engineering Mob: +919818442705 E-Mail : tushicom...@gmail.com 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 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.
Re: [algogeeks] Reversing the order of words in String
Can we do it using linked list if ONE TIME TRAVERSAL is a constraint?? On 7/6/11, Tushar Bindal tushicom...@gmail.com wrote: 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 navneetn...@gmail.comwrote: 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 saurab...@gmail.com 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 tushicom...@gmail.com 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 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. #includeiostream #includestring 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); coutstr; 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. -- Tushar Bindal Computer Engineering Delhi College of Engineering Mob: +919818442705 E-Mail : tushicom...@gmail.com 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 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. -- 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 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. -- 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. -- Tushar Bindal Computer Engineering Delhi College of Engineering Mob: +919818442705 E-Mail : tushicom...@gmail.com 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 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. -- *Piyush Sinha* *IIIT, Allahabad* *+91-8792136657* *+91-7483122727* *https://www.facebook.com/profile.php?id=10655377926 * -- You received this message
Re: [algogeeks] Reversing the order of words in String
@Piyush, could you elaborate your approach with Linked List? From what i am getting, even with Linked List, you would need two traversals at least. On Thu, Jul 7, 2011 at 2:07 AM, Piyush Sinha ecstasy.piy...@gmail.com wrote: Can we do it using linked list if ONE TIME TRAVERSAL is a constraint?? On 7/6/11, Tushar Bindal tushicom...@gmail.com wrote: 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 navneetn...@gmail.comwrote: 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 saurab...@gmail.com 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 tushicom...@gmail.com 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 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. #includeiostream #includestring 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); coutstr; 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. -- Tushar Bindal Computer Engineering Delhi College of Engineering Mob: +919818442705 E-Mail : tushicom...@gmail.com 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 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. -- 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 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. -- 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. -- Tushar Bindal Computer Engineering Delhi College of Engineering Mob: +919818442705 E-Mail : tushicom...@gmail.com 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 algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this
Re: [algogeeks] Reversing the order of words in String
If we only need to print the words in reverse order, strtok+recursion can help. Following is the code (which also stores the string into another string, not memory efficient though): #include stdio.h #include string.h #include stdlib.h char str[] = This is a new world; char sstr[sizeof(str)]; char *print_rev(char *tok) { char *nstr = NULL; if (tok) { nstr = strtok(NULL, ); print_rev(nstr); if (nstr) { // printf(%s , nstr); strcpy(sstr[strlen(sstr)], nstr); sstr[strlen(sstr)] = ' '; } } return nstr; } int main(int argc, char *argv[]) { char *nstr; printf(Org string -- %s\n, str); nstr = strtok(str, ); print_rev(str); strcpy(sstr[strlen(sstr)], nstr); printf(New string -- %s\n, sstr); return 0; } On Thu, Jul 7, 2011 at 9:10 AM, Navneet Gupta navneetn...@gmail.com wrote: @Piyush, could you elaborate your approach with Linked List? From what i am getting, even with Linked List, you would need two traversals at least. On Thu, Jul 7, 2011 at 2:07 AM, Piyush Sinha ecstasy.piy...@gmail.com wrote: Can we do it using linked list if ONE TIME TRAVERSAL is a constraint?? On 7/6/11, Tushar Bindal tushicom...@gmail.com wrote: 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 navneetn...@gmail.comwrote: 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 saurab...@gmail.com 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 tushicom...@gmail.com 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 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. #includeiostream #includestring 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); coutstr; 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. -- Tushar Bindal Computer Engineering Delhi College of Engineering Mob: +919818442705 E-Mail : tushicom...@gmail.com 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 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. -- 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