method 1:
algo:
step 1 :reverse entire string .. (letter by letter)
step 2: take two pointers ... keep first pointer at the starting of the word
... keep incrementing the second pointer , till space hits.. then , swap
first pointer and second pointere data , by incrementing first pointer , and
decrementing second pointer till length(word)/2 .. repeat the same gtill end
of the string..

space o(1)
time .. o(n)+ // guys .. please help me in calculating the time comp[lexxity
for this..

method 2:

algo ..
step 1: reverse entire string .. (letter by letter)
step 2:  push char by char till space occurs , then pop the stack and copy
into the word ..

space : o(largest size of word)
time : o(n)


On Mon, Aug 15, 2011 at 9:07 PM, Dipankar Patro <[email protected]> wrote:

> @ MeHdi : Please read the problem properly yaar. You are just reversing the
> string by characters, not by words.
>
>
> On 15 August 2011 20:34, MeHdi KaZemI <[email protected]> wrote:
>
>> string str = "i am a programmer"
>> for(int i = 0; i < str.size()/2; i ++)
>>     swap(str[i], str[str.size()-i-1]);
>> time complexity O(n)
>>
>>
>> On Mon, Aug 15, 2011 at 6:39 PM, sukran dhawan <[email protected]>wrote:
>>
>>> reverse(string,n) // do it in place
>>>
>>>
>>>  p = str;
>>> for(i=0;i<length(str);i++)
>>> {
>>>      if(str[i] == '\0' || str[i] == ' ')
>>>       {
>>>          reverse(p,len);
>>>         p = p+len+1;
>>>         len = 0;
>>> }
>>> else
>>> len++;
>>> }
>>>
>>> On Mon, Aug 15, 2011 at 4:48 PM, programming love <
>>> [email protected]> wrote:
>>>
>>>> write a program to reverse the words in a give string.
>>>> also state the time complexity of the algo.
>>>>
>>>> if the string is "i am a programmer"
>>>> the output should be "programmer a am i"
>>>>
>>>> --
>>>> You received this message because you are subscribed to the Google
>>>> Groups "Algorithm Geeks" group.
>>>> To post to this group, send email to [email protected].
>>>> To unsubscribe from this group, send email to
>>>> [email protected].
>>>> For more options, visit this group at
>>>> http://groups.google.com/group/algogeeks?hl=en.
>>>>
>>>
>>>  --
>>> You received this message because you are subscribed to the Google Groups
>>> "Algorithm Geeks" group.
>>> To post to this group, send email to [email protected].
>>> To unsubscribe from this group, send email to
>>> [email protected].
>>> For more options, visit this group at
>>> http://groups.google.com/group/algogeeks?hl=en.
>>>
>>
>>
>>
>> --
>>    MeHdi KaZemI
>>
>>   --
>> You received this message because you are subscribed to the Google Groups
>> "Algorithm Geeks" group.
>> To post to this group, send email to [email protected].
>> To unsubscribe from this group, send email to
>> [email protected].
>> For more options, visit this group at
>> http://groups.google.com/group/algogeeks?hl=en.
>>
>
>
>
> --
>
> ___________________________________________________________________________________________________________
>
> Please do not print this e-mail until urgent requirement. Go Green!!
> Save Papers <=> Save Trees
>
> --
> You received this message because you are subscribed to the Google Groups
> "Algorithm Geeks" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to
> [email protected].
> For more options, visit this group at
> http://groups.google.com/group/algogeeks?hl=en.
>



-- 
Thx,
--Gopi

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

Reply via email to