Re: [algogeeks] Re: Reversing the order of words in String

2011-07-13 Thread hary rathor
swap(char **a,char **b)
{
char  *t=*a;
  *a=*b;
  *b=t;

}

#define wd 3
int main()
{

char *r[wd];
char *str=ram is good;

//splite requiere O(n) where n is number of charecter

int i=0;
int k=0;
while(str[i]!='\0')
{
 char arr[10],j=0;
 while(str[i]!=' '||str[i]!='\0')
 arr[j++]=str[i++];
 arr[j]='\0';
 r[k]=(char *)malloc(strlen(arr)+1);
 strcpy(r[k],arr);
 k++;

}

//swaping in log(m) where m is number of word

int l=0,m=wd-1;
while(l=wd)
swap(r[l++],r[m--]);

for(i=0;iwd;i++)
{
strcpy(str,r[i]);
strcpy(str, );
}

print(str);
}


overall time complexity; O(n+logm)   which is O(n)  and O(n) memory
complexty

NOTE: error handling is remaining

-- 
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] Re: Reversing the order of words in String

2011-07-13 Thread Anand Saha
On Wed, Jul 13, 2011 at 7:32 AM, Gene gene.ress...@gmail.com wrote:

 You can recognize a word W, recur to print the rest of the words in
 reverse, then print W:

 #include stdio.h
 #include string.h

 void print_words_in_reverse(char *s)
 {
  char *e;
  while (isspace(*s)) s++;
  if (*s == '\0') return;
  e = s + 1;
  while (*e  !isspace(*e)) e++;
  print_words_in_reverse(e);
  printf(%.*s , e - s, s);
 }

 int main(void)
 {
  char line[16 * 1024];
  fgets(line, sizeof line, stdin);
  print_words_in_reverse(line);
  return 0;
 }




Nice. Here is a similar version which, instead of printing, reverses the
string in place in O(n), if that's what's needed.

http://ideone.com/nHcQ3 http://www.ideone.com/tA6rS

--

-- 
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] Re: Reversing the order of words in String

2011-07-12 Thread nicks
@saurabh why are you reading the string character by character..why
don't just read it word by word ? . what's the problem in it ?

btw...nice soln...


On Wed, Jul 13, 2011 at 7:32 AM, Gene gene.ress...@gmail.com wrote:

 You can recognize a word W, recur to print the rest of the words in
 reverse, then print W:

 #include stdio.h
 #include string.h

 void print_words_in_reverse(char *s)
 {
  char *e;
  while (isspace(*s)) s++;
  if (*s == '\0') return;
  e = s + 1;
  while (*e  !isspace(*e)) e++;
  print_words_in_reverse(e);
  printf(%.*s , e - s, s);
 }

 int main(void)
 {
  char line[16 * 1024];
  fgets(line, sizeof line, stdin);
  print_words_in_reverse(line);
  return 0;
 }


 On Jul 6, 12:40 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
 https://groups.google.com/forum/#%21msg/algogeeks/oEL8z4wwMJY/FAVdr2M...
  .
 
 
 
 
 
  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- Hide quoted text -
 
  - Show quoted text -

 --
 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.