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
- Next message: Flash Gordon: "Re: malloc stumper"
- Previous message: Malcolm: "Re: malloc stumper"
- In reply to: Durgesh Sharma: "How can i Right Trim all the spaces of a very long (2000 chars) Charecter string ?"
- Next in thread: Jack Klein: "Re: How can i Right Trim all the spaces of a very long (2000 chars) Charecter string ?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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
- Next message: Flash Gordon: "Re: malloc stumper"
- Previous message: Malcolm: "Re: malloc stumper"
- In reply to: Durgesh Sharma: "How can i Right Trim all the spaces of a very long (2000 chars) Charecter string ?"
- Next in thread: Jack Klein: "Re: How can i Right Trim all the spaces of a very long (2000 chars) Charecter string ?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|