473,804 Members | 3,397 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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("Unspeci fied 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
15 6272
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********@yah oo.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 misinterpretati on 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********@yah oo.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 misinterpretati on 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
2909
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 DirectionVertical, but it's not the effect I expected. I want to get reverse vertical string, like the number of MS Word vertical ruler. How to implement it?
0
1811
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
11905
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 is test " ; the output of the program is reversing this statement to be " tset si siht ". so the string became reversed. I am asking: 1- why some of programmers do this ? 2- What's the benefit if they do this ? at any circumstances? Thank to all.
11
5005
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 coding, and he coded last week.' ('coding', 'coded', 'week') expanded (for better visual): ('coding', 'coded', 'week')
38
2730
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
9705
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9575
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10320
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10308
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10073
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9134
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5513
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3806
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2981
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.