Re: [algogeeks] Reversing a string
Hope this logic looks better. class Program { static void Main(string[] args) { string str = string; char[] char_str=str.ToCharArray(); char temp; int string_length = char_str.Length; int mid = string_length / 2; int j=0; for(int i=0;imid;i++){ j = str.Length-1-i; temp = char_str[i]; char_str[i] = char_str[j]; char_str[j] = temp; } Console.Write(char_str.ToString()); Console.Read(); } } On Sat, May 28, 2011 at 7:53 AM, adityasir...@gmail.com wrote: In java you can do this, take O(n) time. Is that correct? -Adi public class ReverseString { public static void main(String[] args){ String name = Aditya; String reverse = ; for(int i=0;iname.length();i++){ System.out.println(name.charAt(i) + reverse); reverse = name.charAt(i) + reverse; } } } On Sat, May 28, 2011 at 6:40 AM, abc abc may.i.answ...@gmail.com wrote: *Given an array of characters. How would you reverse it. ? How would you reverse it without using indexing in the array.* * * -- 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. -- 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. -- Jyothi -- 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 a string
Hi Pls see below. This is only with indexing. I am not sure how to do it w/o indexing if its not a C-style string or w/o pointer arithmetic. Pls let me know if there is such a technique. I would be very eager to know. Thanks. #includestdio.h #includestring.h void reverse(char s[]) { int len = sizeof(s)/sizeof(char); printf(%d\n, len); if(len == 1 || len == 0) return; int i,j; for(i = 0, j = len-1; i len, j = 0; i++, j--) { if(i =j) break; char temp = s[i]; s[i] = s[j]; s[j] = temp; printf(%c%c\n, s[i], s[j]); } } int main() { char s[7] = {'a','b','x','c', 'l', 'm', 'y'}; reverse(s); int i; for(i = 0; i 7; i++) printf(%c, s[i]); return 0; } On Tue, May 31, 2011 at 12:04 PM, nagajyothi gunti nagajyothi.gu...@gmail.com wrote: Hope this logic looks better. class Program { static void Main(string[] args) { string str = string; char[] char_str=str.ToCharArray(); char temp; int string_length = char_str.Length; int mid = string_length / 2; int j=0; for(int i=0;imid;i++){ j = str.Length-1-i; temp = char_str[i]; char_str[i] = char_str[j]; char_str[j] = temp; } Console.Write(char_str.ToString()); Console.Read(); } } On Sat, May 28, 2011 at 7:53 AM, adityasir...@gmail.com wrote: In java you can do this, take O(n) time. Is that correct? -Adi public class ReverseString { public static void main(String[] args){ String name = Aditya; String reverse = ; for(int i=0;iname.length();i++){ System.out.println(name.charAt(i) + reverse); reverse = name.charAt(i) + reverse; } } } On Sat, May 28, 2011 at 6:40 AM, abc abc may.i.answ...@gmail.com wrote: *Given an array of characters. How would you reverse it. ? How would you reverse it without using indexing in the array.* * * -- 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. -- 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. -- Jyothi -- 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. -- U -- 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 a string
This is one way of doing it without index. #include stdio.h #include string.h void reverse(char *begin, char *end) { char temp; while ( begin = end ) { temp = *begin; *begin = *end; *end = temp; begin++; end--; } } int main() { char str[100]; scanf (%s, str); reverse(str, str + strlen(str) - 1); printf (Reversed String = %s\n, str); return 0; } On Tue, May 31, 2011 at 12:14 PM, Supraja Jayakumar suprajasank...@gmail.com wrote: Hi Pls see below. This is only with indexing. I am not sure how to do it w/o indexing if its not a C-style string or w/o pointer arithmetic. Pls let me know if there is such a technique. I would be very eager to know. Thanks. #includestdio.h #includestring.h void reverse(char s[]) { int len = sizeof(s)/sizeof(char); printf(%d\n, len); if(len == 1 || len == 0) return; int i,j; for(i = 0, j = len-1; i len, j = 0; i++, j--) { if(i =j) break; char temp = s[i]; s[i] = s[j]; s[j] = temp; printf(%c%c\n, s[i], s[j]); } } int main() { char s[7] = {'a','b','x','c', 'l', 'm', 'y'}; reverse(s); int i; for(i = 0; i 7; i++) printf(%c, s[i]); return 0; } On Tue, May 31, 2011 at 12:04 PM, nagajyothi gunti nagajyothi.gu...@gmail.com wrote: Hope this logic looks better. class Program { static void Main(string[] args) { string str = string; char[] char_str=str.ToCharArray(); char temp; int string_length = char_str.Length; int mid = string_length / 2; int j=0; for(int i=0;imid;i++){ j = str.Length-1-i; temp = char_str[i]; char_str[i] = char_str[j]; char_str[j] = temp; } Console.Write(char_str.ToString()); Console.Read(); } } On Sat, May 28, 2011 at 7:53 AM, adityasir...@gmail.com wrote: In java you can do this, take O(n) time. Is that correct? -Adi public class ReverseString { public static void main(String[] args){ String name = Aditya; String reverse = ; for(int i=0;iname.length();i++){ System.out.println(name.charAt(i) + reverse); reverse = name.charAt(i) + reverse; } } } On Sat, May 28, 2011 at 6:40 AM, abc abc may.i.answ...@gmail.comwrote: *Given an array of characters. How would you reverse it. ? How would you reverse it without using indexing in the array.* * * -- 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. -- 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. -- Jyothi -- 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. -- U -- 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. -- -Aakash Johari (IIIT 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.
[algogeeks] Reversing a string
*Given an array of characters. How would you reverse it. ? How would you reverse it without using indexing in the array.* * * -- 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 a string
#includestdio.h int main() { char s[20],t[30],*p,*q; scanf(%s,s); p=s; q=t; while(*(++p)!='\0'); p--; while(p!=s) { *(q++)=*(p--); } *q='\0'; } Is this what you are looking for? I think an inplace solution is required? On Sat, May 28, 2011 at 4:10 PM, abc abc may.i.answ...@gmail.com wrote: *Given an array of characters. How would you reverse it. ? How would you reverse it without using indexing in the array.* * * -- 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 a string
In java you can do this, take O(n) time. Is that correct? -Adi public class ReverseString { public static void main(String[] args){ String name = Aditya; String reverse = ; for(int i=0;iname.length();i++){ System.out.println(name.charAt(i) + reverse); reverse = name.charAt(i) + reverse; } } } On Sat, May 28, 2011 at 6:40 AM, abc abc may.i.answ...@gmail.com wrote: *Given an array of characters. How would you reverse it. ? How would you reverse it without using indexing in the array.* * * -- 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. -- 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.
[algogeeks] Reversing a string !!
Hi, I was asked a question in an interview to Reverse the order of words in a sentence. eg. Google is an awesome place to work should be reversed as work to place awesome an is Google The solution should be efficient and shouldn't use any extra memory. Pl suggest me some solution. Regards, Somesh --~--~-~--~~~---~--~~ 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 [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/algogeeks -~--~~~~--~~--~--~---