@pradad

1. Never put strlen in the condition of a loop. That instantly makes
an O(n) loop into O(n^2).
2. Be sure to put the null terminator at the end of the compacted
string. Your current code stops one short of doing that.
3. Put {} around the body of the for loop if the body contains more
than one semicolon.
4. If there is common code in an if block and the else block, you can
move it out of the if/else.
5. The variable i is extraneous.

The cleanest way I see to write the code is like this:

for(ptr1 = ptr2 = string; *ptr2; ++ptr2)
    if (*ptr2 != ' ') *(ptr1++) = *ptr2;
*ptr1 = 0;

Don

On Oct 11, 3:41 pm, prasad jondhale <jondhale.pra...@gmail.com> wrote:
> *ptr1=*ptr2=string;
> for(i=0;i<strlen(string);i++)
> if(*str==' ')
>    ptr2++;
> else
>    {
>       *ptr1=*ptr2;
>        ptr1++;
>        ptr2++;
>   }
>
> hey guys if anything wrong in this code pls let me know............
>
> On Tue, Oct 11, 2011 at 11:04 AM, DIPANKAR DUTTA <dutta.dipanka...@gmail.com
>
>
>
> > wrote:
> > Solution 1:
> > need two scan :
>
> > start from left and replace one by one by non-space character ( thus
> > have minimal replacemnt)
>
> > count =0;
> > for (int i=0;i<len(str);i++)
> > {
> >    if(str[i] !=NONSPCECHAR)
> >    {    str[count++]=str[i]
> >    }
>
> > }
>
> > ps: any way it needs Left shift...and all solution must need left
> > shift if you try to compact it in lower index area.
>
> > On 10/11/11, abhishek sharma <abhishek.p...@gmail.com> wrote:
> > > Can in place compaction be done without left shifts?
>
> > > --
> > > Nice Day
>
> > > Abhishek Sharma
> > > Bachelor of Technology
> > > IIT Kanpur (2009)
>
> > > --
> > > 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.
>
> > --
> > Thanks and Regards,
> > ------------------------------
> > **DIPANKAR DUTTA
> > Software Development Engineer
> > Xen Server - OpenStack Development Team (DataCenter and Cloud)
>
> > Citrix R&D India Pvt Ltd
> > 69/3, Millers Road, Bangalore – 560052
> > Phone: +91 8147830733
> > Office: Extn: 16429
> > Email: dipankar.du...@citrix.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.
>
> --
>
> Thanks & Regards
> Prasad Y.Jondhale
> M.Tech(software engg.),
> Delhi College Of Engineering,
> Main Bawana road,Delhi-110042
> Ph-09540208001

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

Reply via email to