Re: How can i Right Trim all the spaces of a very long (2000 chars) Charecter string ?

From: Mac (foo_at_bar.net)
Date: 12/04/04


Date: Sat, 04 Dec 2004 18:21:25 GMT

On Sat, 04 Dec 2004 04:10:33 -0800, Durgesh Sharma wrote:

> Hi All,
> Pleas help me .I am a starter as far as C Language is concerned .
>
> How can i Right Trim all the white spaces of a very long (2000 chars)
> Charecter string ( from the Right Side ) ? or how can i make a fast
> Right Trim Function in c,using Binary search kind of fast algorithm ?
>
> Offcourse...I can use the classical approach too.
> like :
> Start from the right most charecter of the string to the left of the
> string and stop where we find the first none-whitespace charecter and
> insert a NULL charecter,after increasing one place.It is working
> fine.But problem is that suppose my string can hold 2000
> charecters,but in a particular case i have only 1 charecter in my
> string ,then i will have to sequentially traverse from charecter
> position 2000 to 1 (1999 sequential searches) to put a NULL char at
> position number 2.
>
> String is of varying length sometime it can be of 2 charecters long
> and other time it can have 2000 charecters too.
>
> How can i use Binary search kind of fast search mechanism in the above
> scenario in my function.
>
> Please help me.
> Thanks in advance.
>
> Regards,
> Durgesh Sharma

I don't think there is any better way to solve your problem in general.

But if the string satisfies some special constraints, it may be possible.

Is the string allowed to contain white spaces in the significant part? If
so, I don't see any way to find the last white space except to start from
the end of the string and work backwards as you are currently doing.

If the string is NOT allowed to contain white spaces in the significant
part, then you can use a binary search algorithm. Something like the
following, which I threw together far too fast and didn't test:

#include <string.h>
void right_trim(char *s, long len)
{
  long part_len, current;

  part_len = len;
  current = len/2;
  while(part_len > 1)
  {
    part_len /= 2;
    if (isspace(s[current]))
      current -= part_len;
    else
    {
      if ((current < len)
      {
        if (isspace(s[current+1]))
        {
          s[current + 1] = '\0';
          return;
        }
        else
          current += part_len;
      }
    }
  }
}

Like I said, it is untested and probably won't work as written, but you
will hopefully get the idea.

--Mac



Relevant Pages

  • Re: How can i Right Trim all the spaces of a very long (2000 chars) Charecter string ?
    ... > Right Trim Function in c,using Binary search kind of fast algorithm? ... > Start from the right most charecter of the string to the left of the ... would first look at the character in the middle of the string. ...
    (comp.lang.c)
  • How can i Right Trim all the spaces of a very long (2000 chars) Charecter string ?
    ... Pleas help me .I am a starter as far as C Language is concerned. ... Charecter string? ... Right Trim Function in c,using Binary search kind of fast algorithm? ...
    (comp.lang.c)
  • Re: VB help
    ... retrieving the a srting value in the database through a record ... retrieved string shows some junk charecters in place of white spaces ... string in the database 8a Church Road Barry ...
    (microsoft.public.vb.general.discussion)
  • Re: Regex for white space
    ... > Suppose a series of white spaces in between a string, ... > Now I wonder how can I get a new string contains no spaces or substuted by symbols like _ ... Lew Pitcher, IT Specialist, Enterprise Data Systems ...
    (comp.unix.shell)
  • Re: Regex for white space
    ... Ying Zu wrote: ... > Suppose a series of white spaces in between a string, ... > Now I wonder how can I get a new string contains no spaces or substuted by symbols like _ ... BashDiff: Super Bash shell ...
    (comp.unix.shell)