473,320 Members | 2,020 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Reverse a string in place

Thanks for the additional comments.

Here is a solution to an exercise I had problems with. I still don't
think it's really what's wanted as it uses a "state variable" n - but
I can't see how to do it without this (or changing the function to
take an extra argument). Thanks for any hints.

#include<stdio.h>

#define SWAP(a,b) { char x; x=(a); (a)=(b); (b)=x; }

void reverse();

main(int argc, char**argv)
{
if(argc>1) {
reverse(argv[1]);
printf("%s\n", argv[1]);
} else
printf("Unspecified error\n");
}

void reverse(char *s)
{
static int n=-1;
if(n==-1)
n=strlen(s)-1;
SWAP(*s, s[n]);
if((n-=2)>=0)
reverse(s+1);
else
n=-1; /* make sure function is reentrant */
}
Dec 6 '07 #1
15 6226
On Dec 6, 3:15 pm, raj...@thisisnotmyrealemail.com wrote:
Thanks for the additional comments.

Here is a solution to an exercise I had problems with. I still don't
think it's really what's wanted as it uses a "state variable" n - but
I can't see how to do it without this (or changing the function to
take an extra argument). Thanks for any hints.
void reverse(char *string)
{
char *end;

for (end = string; *end ; ++end); --end;
/* end points to last character in string */

while (string end)
{
char temp;

temp = *string;
*string++ = *end;
*end-- = temp;
}
}

Dec 6 '07 #2
Oops... pardon my typo...

On Dec 6, 3:28 pm, Lew Pitcher <lpitc...@teksavvy.comwrote:
On Dec 6, 3:15 pm, raj...@thisisnotmyrealemail.com wrote:
Thanks for the additional comments.
Here is a solution to an exercise I had problems with. I still don't
think it's really what's wanted as it uses a "state variable" n - but
I can't see how to do it without this (or changing the function to
take an extra argument). Thanks for any hints.

void reverse(char *string)
{
char *end;

for (end = string; *end ; ++end); --end;
/* end points to last character in string */

while (string end)
{
char temp;

temp = *string;
*string++ = *end;
*end-- = temp;
}

}

void reverse(char *string)
{
char *end;

for (end = string; *end ; ++end); --end;
/* end points to last character in string */

while (string < end)
{
char temp;

temp = *string;
*string++ = *end;
*end-- = temp;
}
}
Dec 6 '07 #3
Lew Pitcher wrote:
On Dec 6, 3:15 pm, raj...@thisisnotmyrealemail.com wrote:
Thanks for the additional comments.

Here is a solution to an exercise I had problems with. I still don't
think it's really what's wanted as it uses a "state variable" n - but
I can't see how to do it without this (or changing the function to
take an extra argument). Thanks for any hints.

void reverse(char *string)
{
char *end;

for (end = string; *end ; ++end); --end;
/* end points to last character in string */

while (string end)
{
char temp;

temp = *string;
*string++ = *end;
*end-- = temp;
}
}
Uhh yes! I should have said clearly that it's Exercise 4.13 - write a
**recursive** version of reverse(s).
Dec 6 '07 #4
ra****@thisisnotmyrealemail.com wrote:
) Uhh yes! I should have said clearly that it's Exercise 4.13 - write a
) **recursive** version of reverse(s).

That's silly. Reversing a string is clearly an iterative operation.
However, any iteration can be easily transformed into a tail recursion.

Note, also, that it is perfectly valid to have two functions.
One that recurses, and one that makes the initial call to the other.

Anyways, Here's a stab at a very inefficient and silly solution, using
a single function:

void reverse(char *string)
{
size_t len = strlen(string);
if (len 1) {
char tmp = string[len-1];
string[len-1] = 0;
reverse(string + 1);
string[len-1] = string[0];
string[0] = tmp;
}
}
SaSW, Willem
--
Disclaimer: I am in no way responsible for any of the statements
made in the above text. For all I know I might be
drugged or something..
No I'm not paranoid. You all think I'm paranoid, don't you !
#EOT
Dec 6 '07 #5
ra****@thisisnotmyrealemail.com writes:
Thanks for the additional comments.

Here is a solution to an exercise I had problems with. I still don't
think it's really what's wanted as it uses a "state variable" n - but
I can't see how to do it without this (or changing the function to
take an extra argument). Thanks for any hints.

#include<stdio.h>

#define SWAP(a,b) { char x; x=(a); (a)=(b); (b)=x; }
I'd either just write this code out, or I'd make this a function. If
I really needed it to be a macro, I'd use the "do {} while (0)" idiom
so as to get syntactic unit that can be followed by a ; and remain a
single statement.
void reverse();
This is not a prototype for reverse, just a declaration. Tell the
full story by writing:

void reverse(char *s);
main(int argc, char**argv)
int main is better and required in C99.
{
if(argc>1) {
reverse(argv[1]);
printf("%s\n", argv[1]);
} else
printf("Unspecified error\n");
}

void reverse(char *s)
{
static int n=-1;
if(n==-1)
n=strlen(s)-1;
SWAP(*s, s[n]);
if((n-=2)>=0)
reverse(s+1);
else
n=-1; /* make sure function is reentrant */
}
Spaces have come right down in price recently after the discovery of
the Peruvian space deposits. Help yourself to a few more.

Reverse is not a natural candidate for recursion (even in functional
languages a little care is needed to stop it being very inefficient)
but in C we have some extra options because we can modify the string.
I'd write it like this:

void rev(char *start, char *end)
{
if (start + 1 >= end)
return;
else {
char t = end[-1];
end[-1] = *start;
*start = t;
rev(start + 1, end - 1);
}
}

void reverse(char *s)
{
rev(s, strchr(s, 0));
}

--
Ben.
Dec 6 '07 #6
Lew Pitcher wrote:
>
Oops... pardon my typo...

On Dec 6, 3:28 pm, Lew Pitcher <lpitc...@teksavvy.comwrote:
On Dec 6, 3:15 pm, raj...@thisisnotmyrealemail.com wrote:
Thanks for the additional comments.
Here is a solution to an exercise I had problems with. I still don't
think it's really what's wanted as it uses a "state variable" n - but
I can't see how to do it without this (or changing the function to
take an extra argument). Thanks for any hints.
void reverse(char *string)
{
char *end;

for (end = string; *end ; ++end); --end;
/* end points to last character in string */

while (string end)
{
char temp;

temp = *string;
*string++ = *end;
*end-- = temp;
}

}

void reverse(char *string)
{
char *end;

for (end = string; *end ; ++end); --end;
/* end points to last character in string */
Unless the string is (""),
in which case (end) is undefined.
while (string < end)
{
char temp;

temp = *string;
*string++ = *end;
*end-- = temp;
}
}
--
pete
Dec 7 '07 #7
pete wrote:
Lew Pitcher wrote:
.... snip ...
>
>void reverse(char *string) {
char *end;
for (end = string; *end ; ++end); --end;
/* end points to last character in string */

Unless the string is (""), in which case (end) is undefined.
> while (string < end) {
char temp;

temp = *string;
*string++ = *end;
*end-- = temp;
}
}
So try:

void reverse(char *string) {
char *end, temp;

for (end = string; *end; end++) continue;
/* leaving *end == 0 */
while (string < end) {
temp = *string;
*string++ = *(--end);
*end = temp;
)
} /* reverse, untested */

--
Chuck F (cbfalconer at maineline dot net)
<http://cbfalconer.home.att.net>
Try the download section.

--
Posted via a free Usenet account from http://www.teranews.com

Dec 8 '07 #8
RoS
In data Fri, 14 Dec 2007 12:56:08 -0500, pete scrisse:
what is the difference of the below 2 routines?
>This version of reverse, does nothing:

void reverse(char *string)
{
char *end;

for (end = string; *end ; ++end);
if (*end) --end; else return;
/* end points to last character in string */

while (string < end)
{
char temp;

temp = *string;
*string++ = *end;
*end-- = temp;
}
}
This version of reverse, reverses a string:

void reverse(char *string)
{
char *end;

for (end = string; *end ; ++end);
if (*string) --end; else return;
/* end points to last character in string */

while (string < end)
{
char temp;

temp = *string;
*string++ = *end;
*end-- = temp;
}
}

If you can't see that, then you can't read code anymore.

Nobody else is seeing that code the way that you do.
Dec 14 '07 #9
RoS
In data Fri, 14 Dec 2007 20:53:42 +0100, RoS scrisse:
>In data Fri, 14 Dec 2007 12:56:08 -0500, pete scrisse:
what is the difference of the below 2 routines?
yes i have seen it ...

void reverse(char *aa)
{char *a=aa, *b, t;
if(a==0||*a==0) return;
for( ; *a; ++a);
for(--a, b=aa; b<a; ++b, --a)
{t=*b; *b=*a; *a=t;}
}

this is my version whithout compile it nor debug it
>>This version of reverse, does nothing:

void reverse(char *string)
but str* were not implementation reserved?
>>{
char *end;

for (end = string; *end ; ++end);
if (*end) --end; else return;
when this case "*end!=0" is verified here? (never)

> /* end points to last character in string */

while (string < end)
{
char temp;

temp = *string;
*string++ = *end;
*end-- = temp;
}
}
This version of reverse, reverses a string:

void reverse(char *string)
{
char *end;

for (end = string; *end ; ++end);
if (*string) --end; else return;
/* end points to last character in string */

while (string < end)
{
char temp;

temp = *string;
*string++ = *end;
*end-- = temp;
}
}

If you can't see that, then you can't read code anymore.

Nobody else is seeing that code the way that you do.
Dec 14 '07 #10
RoS wrote:
>
In data Fri, 14 Dec 2007 20:53:42 +0100, RoS scrisse:
In data Fri, 14 Dec 2007 12:56:08 -0500, pete scrisse:
what is the difference of the below 2 routines?

yes i have seen it ...

void reverse(char *aa)
{char *a=aa, *b, t;
if(a==0||*a==0) return;
for( ; *a; ++a);
for(--a, b=aa; b<a; ++b, --a)
{t=*b; *b=*a; *a=t;}
}

this is my version whithout compile it nor debug it
OK
>This version of reverse, does nothing:

void reverse(char *string)

but str* were not implementation reserved?
No.
The rules are more complicated than that.

N869
7.26.10 General utilities <stdlib.h>
[#1] Function names that begin with str and a lowercase
letter (possibly followed by any combination of digits,
letters, and underscore) may be added to the declarations in
the <stdlib.hheader.

7.26.11 String handling <string.h>
[#1] Function names that begin with str, mem, or wcs and a
lowercase letter (possibly followed by any combination of
digits, letters, and underscore) may be added to the
declarations in the <string.hheader.

>{
char *end;

for (end = string; *end ; ++end);
if (*end) --end; else return;

when this case "*end!=0" is verified here? (never)
You are correct!
/* end points to last character in string */

while (string < end)
{
char temp;

temp = *string;
*string++ = *end;
*end-- = temp;
}
}
--
pete
Dec 14 '07 #11
CBFalconer wrote:
However, if *string is 0 the else
clause arises, and the function returns without doing anything.
That's what it's supposed to do.

No action is required to reverse a zero length string.

If (*string) is 0,
then you are reversing a zero length string.

void reverse(char *string)
{
char *end;

for (end = string ; *end ; ++end);
if (*string) --end; else return;

--
pete
Dec 15 '07 #12
ozbear wrote:
CBFalconer <cb********@yahoo.comwrote:
.... snip ...
>>
If the string is non-empty the test (*string) is true, --end is
executed, and end no longer points to a zero, but to the last
character in the string.

I didn't miss that point, but it isn't what Lew originally wrote,
since he was testing *end, not *string, for the early return
in the case of an empty string (please look at his original
"correction" to cater for empty strings). I corrected it.

Do you dispute that:
1) The original addition of testing *end was incorrect.
2) My modification corrects that flaw.
3) Lew's original correction for empty strings will cause the
code to not reverse -any- string (just not have UB for
empty ones).

If you disagree with any of (1), (2) or (3), please tell me why.
If you don't, then I don't understand what you were objecting to
in the first place.
Here is the original quote. string is the input parameter, and end
is local to the function.
>>>>>> for (end = string ; *end ; ++end);
>> if (*string) --end; else return;
> ^^^^^^
The above code exits for an empty string input. Only. I was
objecting to the misinterpretation of the code by others.

--
Chuck F (cbfalconer at maineline dot net)
<http://cbfalconer.home.att.net>
Try the download section.

--
Posted via a free Usenet account from http://www.teranews.com

Dec 15 '07 #13
pete wrote:
CBFalconer wrote:
>However, if *string is 0 the else
clause arises, and the function returns without doing anything.

That's what it's supposed to do.
That's what I said, and you objected to.

--
Chuck F (cbfalconer at maineline dot net)
<http://cbfalconer.home.att.net>
Try the download section.

--
Posted via a free Usenet account from http://www.teranews.com

Dec 15 '07 #14
CBFalconer <cb********@yahoo.comwrites:
Here is the original quote. string is the input parameter, and end
is local to the function.
>>>>>>> for (end = string ; *end ; ++end);
>>> if (*string) --end; else return;
>> ^^^^^^

The above code exits for an empty string input. Only. I was
objecting to the misinterpretation of the code by others.
Who misinterpreted it? You certainly seemed to, since when the
correction (from *end to *string) was suggested by ozbear, your reply
started with the word "No". It was:

| ozbear wrote:
| Shouldn't that be:
| >
| if (*string) --end; else return;
| ^^^^^^
|
| No, because that clause detects the original supply of string as
| "", when end was never advanced, and exits early.

What you say after "No" suggests you know what it does, but I just
can't see why you start "No, ...". It made me think you had missed
the point of the correction. If you had replied "Yes, that clause..."
then I think this whole sub-thread would never have happened.

--
Ben.
Dec 15 '07 #15
CBFalconer wrote:
>
pete wrote:
CBFalconer wrote:
However, if *string is 0 the else
clause arises, and the function returns without doing anything.
That's what it's supposed to do.

That's what I said, and you objected to.
I like ozbear's correction to Lew Pitcher's code.
If you do too, then we're all agreed.

--
pete
Dec 15 '07 #16

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Jerry | last post by:
Hi, I want to draw a reverse string, does anyone know how to implement it? And I want to draw a vertical string, it can be implemented by setting StringFormat.StringFormatFlags...
0
by: Alan T | last post by:
I have a string to execute: myExe.exe %1 %2 How do I use in System.Diagnostics.Process.Start? System.Diagnostics.Process.Start("myExe.exe", ........)
9
by: HELLO $$$ | last post by:
From Beginner : I am studying a C++ program, using: char, arrays, etc. The aim of this program is to let it make "Reverse" of any statement (string) as example: if the original string is " this...
11
by: Dustan | last post by:
Is there any builtin function or module with a function similar to my made-up, not-written deformat function as follows? I can't imagine it would be too easy to write, but possible... 'I am...
38
by: ssecorp | last post by:
char* reverse(char* str) { int length = strlen(str); char* acc; int i; for (i=0; i<=length-1; i++){ acc = str; } return acc; }
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.