"sudharsan" <suderson.tk@gm ail.com> wrote in news:1142873798 .329339.167240
@e56g2000cwe.go oglegroups.com:
[color=blue]
> could any one please give me a code to reverse a string of more than
> 1MB .???[/color]
I can think of several reasons why the length of the string might be
important, but why does it matter to you?
Is the strlen to find the end of the string bothering you?
Have you tried the trivial way of doing it in place for a string of any
size?
Sinan
--
A. Sinan Unur <1usa@llenroc.u de.invalid>
(remove .invalid and reverse each component for email address)
sudharsan wrote:[color=blue]
> could any one please give me a code to reverse a string of more than
> 1MB .???
> Thanks in advance[/color]
Many C library implementations provide a non-standard strrev() function
which does what you want. If not, then you'll have to roll your own.
The size of the string shouldn't be a consideration if you're doing
this to learn programming or C. If you can implement the function is a
readable and straightforward manner and post your attempt here, the
regulars can help you further. It's quite easy to do.
On 2006-03-20, sudharsan <suderson.tk@gm ail.com> wrote:[color=blue]
> could any one please give me a code to reverse a string of more than
> 1MB .???
> Thanks in advance
>[/color]
wtrite some code to reverse a string that works for 0,1,2 and 3
characters and it will probably work for a megabyte of them too. size
is no indicator of complexity.
sudharsan wrote:[color=blue]
> could any one please give me a code to reverse a string of more than
> 1MB .???
> Thanks in advance[/color]
Here's one way of doing it.
NOTE: If your implementation already defines a strrev() function, then
rename the corresponding function in the following code to avoid linker
errors.
"santosh" <santosh.k83@gm ail.com> wrote in news:1142879921 .107221.266900
@g10g2000cwb.go oglegroups.com:
[color=blue]
> sudharsan wrote:[color=green]
>> could any one please give me a code to reverse a string of more than
>> 1MB .???
>> Thanks in advance[/color]
>
> Here's one way of doing it.
> NOTE: If your implementation already defines a strrev() function, then
> rename the corresponding function in the following code to avoid
> linker errors.
>
> #include <stddef.h>
> #include <stdbool.h>
> #include <stdio.h>
> #include <stdlib.h>
> #include <string.h>
>
> bool strrev( char *s ) {
> char tmp, *adv_p = NULL, *reg_p = NULL;
> size_t l = 0;
> ptrdiff_t seperation = 0;[/color]
Passing a NULL to this function is a violation of contract, it seems to
me. I would have been inclined to code that requirement into an assert.
On the other hand, passing an empty string or a string consisting of a
single character is benign. I would have just returned from this
function upon detecting that.
Are you using the return value as a 'success' indicator? In that case,
why is it an error condition to not modify the passed string?
Sinan
--
A. Sinan Unur <1usa@llenroc.u de.invalid>
(remove .invalid and reverse each component for email address)
A. Sinan Unur wrote:[color=blue]
> "santosh" <santosh.k83@gm ail.com> wrote in news:1142879921 .107221.266900
> @g10g2000cwb.go oglegroups.com:
>[color=green]
> > sudharsan wrote:[color=darkred]
> >> could any one please give me a code to reverse a string of more than
> >> 1MB .???
> >> Thanks in advance[/color]
> >
> > Here's one way of doing it.
> > NOTE: If your implementation already defines a strrev() function, then
> > rename the corresponding function in the following code to avoid
> > linker errors.
> >
> > #include <stddef.h>
> > #include <stdbool.h>
> > #include <stdio.h>
> > #include <stdlib.h>
> > #include <string.h>
> >
> > bool strrev( char *s ) {
> > char tmp, *adv_p = NULL, *reg_p = NULL;
> > size_t l = 0;
> > ptrdiff_t seperation = 0;[/color]
>
> separation
>[color=green]
> > if(s == NULL || (l = strlen(s)) < 2)
> > return false;[/color]
>
> Passing a NULL to this function is a violation of contract, it seems to
> me. I would have been inclined to code that requirement into an assert.[/color]
Maybe your right. An assert() though would abort the program. What if
the caller may need to do other tasks, even if this function fails?
[color=blue]
> On the other hand, passing an empty string or a string consisting of a
> single character is benign. I would have just returned from this
> function upon detecting that.[/color]
Yes. You're right. I should probably just return true with 's'
unmodified. In that case I should seperate the tests for null pointer
and empty or single character string.
[color=blue]
> Are you using the return value as a 'success' indicator?[/color]
Yes.
[color=blue]
> In that case, why is it an error condition to not modify the passed string?[/color]
It shouldn't be. It was a decision on the spur of the moment. I'll
change it.
Thanks for your feedback.
On 2006-03-20, santosh <santosh.k83@gm ail.com> wrote:[color=blue]
> sudharsan wrote:[color=green]
>> could any one please give me a code to reverse a string of more than
>> 1MB .???
>> Thanks in advance[/color]
>
> Here's one way of doing it.
> NOTE: If your implementation already defines a strrev() function, then
> rename the corresponding function in the following code to avoid linker
> errors.
>
> #include <stddef.h>
> #include <stdbool.h>
> #include <stdio.h>
> #include <stdlib.h>
> #include <string.h>
>
> bool strrev( char *s ) {
> char tmp, *adv_p = NULL, *reg_p = NULL;
> size_t l = 0;
> ptrdiff_t seperation = 0;
>
> if(s == NULL || (l = strlen(s)) < 2)
> return false;[/color]
It's legal to reverse a string of one character. Or does "false" just
indicate no reversal was necessary?
[color=blue]
> else {[/color]
void reverse(char * refS){
/* assume parent does parameter checks */
unsigned int len = (unsigned int)strlen(refS );
char * refE=refS+len-1; /* if len is 0 no problem since nothing done*/
len=len/2; /* or len>>=1 */
while(len--){
char c = *refS; /* front char */
*refS++ = *refE; /* is now end char */
*refE-- = c; /* end char now start char */
}
}
Richard G. Riley wrote:[color=blue]
> On 2006-03-20, santosh <santosh.k83@gm ail.com> wrote:[color=green]
> > sudharsan wrote:[color=darkred]
> >> could any one please give me a code to reverse a string of more than
> >> 1MB .???
> >> Thanks in advance[/color]
> >
> > Here's one way of doing it.
> > NOTE: If your implementation already defines a strrev() function, then
> > rename the corresponding function in the following code to avoid linker
> > errors.
> >
> > #include <stddef.h>
> > #include <stdbool.h>
> > #include <stdio.h>
> > #include <stdlib.h>
> > #include <string.h>
> >
> > bool strrev( char *s ) {
> > char tmp, *adv_p = NULL, *reg_p = NULL;
> > size_t l = 0;
> > ptrdiff_t seperation = 0;
> >
> > if(s == NULL || (l = strlen(s)) < 2)
> > return false;[/color]
>
> It's legal to reverse a string of one character. Or does "false" just
> indicate no reversal was necessary?[/color]
Sinan pointed it out earlier. I should've decoupled the tests and
returned true for the case you suggest.
[color=blue][color=green]
> > else {[/color]
>
> Its all an else in this case since you returned.[/color]
I'm sorry, I don't get what you're trying to say here.
[color=blue][color=green]
> > adv_p = s;
> > --l;
> > reg_p = adv_p + l;
> > }
> >
> > do {
> > tmp = *adv_p;
> > *adv_p = *reg_p;
> > *reg_p = tmp;
> > ++adv_p;
> > --reg_p;
> > seperation = reg_p - adv_p;
> > } while(seperatio n > 0);
> >
> > return true;
> > }[/color]
>
> nicer just to use the strlen/2 IMO.
>
> void reverse(char * refS){
> /* assume parent does parameter checks */
> unsigned int len = (unsigned int)strlen(refS );
> char * refE=refS+len-1; /* if len is 0 no problem since nothing done*/
> len=len/2; /* or len>>=1 */
> while(len--){
> char c = *refS; /* front char */
> *refS++ = *refE; /* is now end char */
> *refE-- = c; /* end char now start char */
> }
> }[/color]
Your version is more concise. But why not use the proper type for
strlen()'s return value? What's the reason to cast it to an unsigned
int?
On 2006-03-20, A. Sinan Unur <1usa@llenroc.u de.invalid> wrote:[color=blue]
> "Richard G. Riley" <rgrdev@gmail.c om> wrote in news:tub3f3-03i.ln1
> @fujitsu.mydoma in.com:
>[color=green]
>> void reverse(char * refS){
>> /* assume parent does parameter checks */
>> unsigned int len = (unsigned int)strlen(refS );[/color]
>
> size_t len = strlen(refS);
>
> Sinan
>[/color]
I like using unsigned ints when I use bit shifts :
although I suppose I could cast it at that point so, yes, size_t is
better. But is it wrong to cast the return from strlen? What does
size_t "equate" to in the standard?
On 2006-03-20, santosh <santosh.k83@gm ail.com> wrote:[color=blue]
> Richard G. Riley wrote:[color=green]
>>
>> Its all an else in this case since you returned.[/color]
>
> I'm sorry, I don't get what you're trying to say here.
>[/color]
the "do" might just as well be in the "else" or you could not bother
with the else, e.g
if(error)
return whatever;
do other stuff;
[color=blue]
>
> Your version is more concise. But why not use the proper type for
> strlen()'s return value? What's the reason to cast it to an unsigned
> int?[/color]
See other reply. size_t is probably better, but Im waiting on a reply
to a question - its another thing Ive probably been lazy about in the
past : I just like unsigned ints for this type of stuff since then I
dont have to cast continually for printfs, logging but am willing to
be corrected if its really evil :-; Also unisgned ints are more "in
your face" for bit shifts to divide by powers of 2.
[color=blue]
>
> Thanks for the feedback.
>[/color]
"Richard G. Riley" <rgrdev@gmail.c om> wrote in news:did3f3-k3j.ln1
@fujitsu.mydoma in.com:
[color=blue]
> On 2006-03-20, A. Sinan Unur <1usa@llenroc.u de.invalid> wrote:[color=green]
>> "Richard G. Riley" <rgrdev@gmail.c om> wrote in news:tub3f3-03i.ln1
>> @fujitsu.mydoma in.com:
>>[color=darkred]
>>> void reverse(char * refS){
>>> /* assume parent does parameter checks */
>>> unsigned int len = (unsigned int)strlen(refS );[/color]
>>
>> size_t len = strlen(refS);[/color][/color]
....[color=blue]
>
> I like using unsigned ints when I use bit shifts :
> although I suppose I could cast it at that point so, yes, size_t is
> better. But is it wrong to cast the return from strlen? What does
> size_t "equate" to in the standard?[/color]
7.17 Common definitions <stddef.h>
1 The following types and macros are defined in the standard header
<stddef.h>. Some are also defined in other headers, as noted in their
respective subclauses.
2 The types are
....
size_t
which is the unsigned integer type of the result of the sizeof operator;
Sinan
--
A. Sinan Unur <1usa@llenroc.u de.invalid>
(remove .invalid and reverse each component for email address)
"Richard G. Riley" <rgrdev@gmail.c om> writes:[color=blue]
> On 2006-03-20, A. Sinan Unur <1usa@llenroc.u de.invalid> wrote:[color=green]
>> "Richard G. Riley" <rgrdev@gmail.c om> wrote in news:tub3f3-03i.ln1
>> @fujitsu.mydoma in.com:
>>[color=darkred]
>>> void reverse(char * refS){
>>> /* assume parent does parameter checks */
>>> unsigned int len = (unsigned int)strlen(refS );[/color]
>>
>> size_t len = strlen(refS);
>>
>> Sinan
>>[/color]
>
> I like using unsigned ints when I use bit shifts :
> although I suppose I could cast it at that point so, yes, size_t is
> better. But is it wrong to cast the return from strlen? What does
> size_t "equate" to in the standard?[/color]
size_t is an unsigned integer type.
"Is it wrong to cast" is the wrong question. The right question is,
"Is this cast really necessary?". If it isn't, lose it. In this
case, there will be an implicit conversion that will do exactly the
same thing the cast does -- except that the implicit conversion will
always get the type right, whereas it's easy to use the wrong type in
an explicit cast.
All casts should be viewed with suspicion.
--
Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Comment