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

Home Posts Topics Members FAQ

To reverse a string

could any one please give me a code to reverse a string of more than
1MB .???
Thanks in advance

Mar 20 '06
47 5026
pete opined:
Vladimir S. Oka wrote:

pete wrote:
>>>>>pete wrote:
>>>>>>Vladimi r S. Oka wrote:
>>>>>
>>>>>>>sudharsa n opined:
>>>>>
>>>>>>>> could any one please give me a code to reverse a string of
>>>>>>>> more than 1MB .???
>>>>>
>>>>>>>Why do you think it would be different to
>>>>>>>the one for strings less than 1MB?
>>>>>
>>>>>>Because a conforming implementation of C,
>>>>>>is not required to be able to translate and execute
>>>>>>a program which excedes the minimum translation limits.
>>>>>
>>>>>>strlen isn't required to work right on a string that long.
>>>>>
>>>>>>The business in the standard about the ptrdiff_t type
>>>>>>not being guaranteed to be able to do its job,
>>>>>>is most appropriately applied to code with objects
>>>>>>which exceede the translation limits.
>>>>>>I've seen strlen implemented on this newsgroup
>>>>>>in ways which depend upon the ptrdiff_t type
>>>>>>being able to do its job.
>>>>>
>>>>>All you say is fine,
>>>>>but I don't see how it applies to your quote of
>>>>>what I said.
>>>>>
>>>>>Unless you took to the word "code" in the OP to meant "exact
>>>>>implementa tion detail",
>>>>>while I read it as "the [general] method". In
I took it to mean exact "implementa tion detail"
and the reason that I did,
is because the exact implementation detail
depends on the length of the string.
In which case we actually agree.
The difference in methodology,
depending on the length of the string, is a c language issue.
To me, "the [general] method" sounds like an algorithm issue.
This is a c language newsgroup, not an algorithm newsgroup.


Yes, but in this case they seem to be intertwined since C language
features have a bearing on the method chosen. In any case, I tend not
to /discuss/ algorithm issues here, but I do offer an odd idea.
>>>>>that case we actually agree,
>>>>>but are talking about diferent parts of
>>>>>the problem.
>>>>>
>>>>>The fact that you may have to implement your own `strlen()`, or
>>>>>whatever , to me, does not change the way this is done.
>>>>
>>>>To me, whether or not you have to implement your own strlen,
>>>>does change the way this is done.
>>>
>>>How so?
>>>
>>>> char *str_rev(char *s)
>>>> {
>>>> char *t, swap;
>>>> char *const p = s;
>>>>
>>>> if (*s != '\0') {
>>>> t = s + strlen(s + 1);
>>>
>>>What's wrong with:
>>>
>>> t = s + my_strlen(s + 1);
>>>
>>>> while (t > s) {
>>>> swap = *t;
>>>> *t-- = *s;
>>>> *s++ = swap;
>>>> }
>>>> }
>>>> return p;
>>>>
>>>> }
>>>
>>>It seems Chuck thinks the above doesn't work
>>> -- I didn't check myself.
Too bad that you didn't.
You could have opinion of your own about the code, if you had.


Admittedly, my comment was unnecessary, to say the least, and certainly
vacuous. I did not express /any/ opinion about your code, though.
Let's say I made a clumsy, and unnecessary reference to another post
that did.
>>Why would you write your own strlen to reverse a string,
>>instead of using the one in the standard library?
>>
>>I was replying to what you said earlier:
>>
>>> Because a conforming implementation of C,
>>> is not required to be able to translate and execute
>>> a program which excedes the minimum translation limits.
>>> strlen isn't required to work right on a string that long.
>>
>>Which you snipped.
>>
>>To expand:
>>if `strlen()` won't necessarilly work on huge strings (your
>>assertion above, I didn't check the Standard), then rolling your
>>own may help.
>
>That's the answer to your original question:
>
>"Why do you think it would be different to the one
> for strings less than 1MB?"
>
>This is a c language newsgroup, not an algorithm newsgroup.


I have now, painstaikingly, re-built the whole exchange. Without
snipping *anything* will you, please,
point to exact things that I said
that you have an issue with, and exactly why?


If you still feel I was grossly off-topic, I apologise, but beg to
disagree, and let's leave it at that.

--
BR, Vladimir

So far as I can remember, there is not one word in the Gospels in
praise of intelligence.
-- Bertrand Russell

Mar 24 '06 #41
pete wrote:
CBFalconer wrote:
pete wrote:
Vladimir S. Oka wrote:

The fact that you may have to implement your own `strlen()`, or
whatever, to me, does not change the way this is done.

To me, whether or not you have to implement your own strlen,
does change the way this is done.

char *str_rev(char *s)
{
char *t, swap;
char *const p = s;

if (*s != '\0') {
t = s + strlen(s + 1);
while (t > s) {
swap = *t;
*t-- = *s;
*s++ = swap;
}
}
return p;
}


Er - that doesn't work. Check the boundary conditions.


I can't find a problem.


I spoke too quickly. It does work. Sorry.

Boundary conditions mean just that, in this case 0 length strings,
or because of the initial elimination 1 length strings.

--
Read about the Sony stealthware that is a security leak, phones
home, and is generally illegal in most parts of the world. Also
the apparent connivance of the various security software firms.
http://www.schneier.com/blog/archive...drm_rootk.html
Mar 24 '06 #42
pete wrote:
CBFalconer wrote:
.... snip ...
I hadn't thought about it at first,
but since your function returned length and mine didn't,
they were not really the same.

size_t str_rev(char *s)
{
char *t, swap;
size_t length;

if (s[0] != '\0') {
if (s[1] != '\0') {
length = strlen(s);
t = s + length - 1;
do {
swap = *t;
*t-- = *s;
*s++ = swap;
} while (t > s);
} else {
length = 1;
}
} else {
length = 0;
}
return length;
}


size_t revstring(char *stg)
{
char *last, temp;
size_t lgh;

lgh = strlen(stg);
if (lgh > 1) {
last = stg + lgh; /* points to '\0' */
while (last-- > stg) {
temp = *stg; *stg++ = *last; *last = temp;
}
}
return lgh;
} /* revstring */

Just for side by side comparison. The fundamental formatting is
the same, the only basic difference being that I use multiple
instructions per line to implement the "tradechars " operation. I
maintain my code is clearer, and probably generates more compact
object code, barring a very smart optimizer. It is certainly more
succint.

--
Read about the Sony stealthware that is a security leak, phones
home, and is generally illegal in most parts of the world. Also
the apparent connivance of the various security software firms.
http://www.schneier.com/blog/archive...drm_rootk.html
Mar 24 '06 #43
Ben Pfaff wrote
(in article <87************ @benpfaff.org>) :
ri*****@cogsci. ed.ac.uk (Richard Tobin) writes:
In article <ln************ @nuthaus.mib.or g>,
Keith Thompson <ks***@mib.or g> wrote:
> Draw yourself a picture of an arbitrary string and figure out how you'd
> reverse it in place. You should only need a single temporary.

You don't even need one - you can use the nul at the end of the
string...

Yes, if saving a single temporary object is worth writing abominably
ugly code.


I repeatedly see people people denouncing the use of smilies on the
grounds that intelligent readers don't need them, but I'm beginning
to doubt it.


You think the articles from folks who think XOR is a good way to
swap two variables are all jokes then?


They're always funny, even if the poster doesn't realize he is
making a joke of himself. :-)
--
Randy Howard (2reply remove FOOBAR)
"The power of accurate observation is called cynicism by those
who have not got it." - George Bernard Shaw

Mar 24 '06 #44
CBFalconer wrote:
size_t revstring(char *stg)
{
char *last, temp;
size_t lgh;

lgh = strlen(stg);
if (lgh > 1) {
last = stg + lgh; /* points to '\0' */
while (last-- > stg) {
temp = *stg; *stg++ = *last; *last = temp;
}
}
return lgh;
} /* revstring */

Just for side by side comparison. The fundamental formatting is
the same, the only basic difference being that I use multiple
instructions per line to implement the "tradechars " operation. I
maintain my code is clearer, and probably generates more compact
object code, barring a very smart optimizer. It is certainly more
succint.


It swaps the middle element
of an odd length string with itself!
Holy redundancy Batman!
You have not a single thought to efficiency,
expressed in this code.

--
pete
Mar 25 '06 #45
pete wrote:

CBFalconer wrote:
size_t revstring(char *stg)
{
char *last, temp;
size_t lgh;

lgh = strlen(stg);
if (lgh > 1) {
last = stg + lgh; /* points to '\0' */
while (last-- > stg) {
temp = *stg; *stg++ = *last; *last = temp;
}
}
return lgh;
} /* revstring */

Just for side by side comparison. The fundamental formatting is
the same, the only basic difference being that I use multiple
instructions per line to implement the "tradechars " operation. I
maintain my code is clearer, and probably generates more compact
object code, barring a very smart optimizer. It is certainly more
succint.


It swaps the middle element
of an odd length string with itself!
Holy redundancy Batman!
You have not a single thought to efficiency,
expressed in this code.


size_t revstring(char *stg)
{
char *last, temp;
size_t lgh;

lgh = strlen(stg);
if (lgh > 1) {
last = stg + lgh - 1;
do {
temp = *stg; *stg++ = *last; *last-- = temp;
} while (last > stg);
}
return lgh;
} /* revstring */
--
pete
Mar 25 '06 #46
pete wrote:
CBFalconer wrote:
size_t revstring(char *stg)
{
char *last, temp;
size_t lgh;

lgh = strlen(stg);
if (lgh > 1) {
last = stg + lgh; /* points to '\0' */
while (last-- > stg) {
temp = *stg; *stg++ = *last; *last = temp;
}
}
return lgh;
} /* revstring */

Just for side by side comparison. The fundamental formatting is
the same, the only basic difference being that I use multiple
instructions per line to implement the "tradechars " operation. I
maintain my code is clearer, and probably generates more compact
object code, barring a very smart optimizer. It is certainly more
succint.


It swaps the middle element of an odd length string with itself!
Holy redundancy Batman!
You have not a single thought to efficiency, expressed in this code.


If that really bothers you change last-- to --last.

--
Read about the Sony stealthware that is a security leak, phones
home, and is generally illegal in most parts of the world. Also
the apparent connivance of the various security software firms.
http://www.schneier.com/blog/archive...drm_rootk.html
Mar 25 '06 #47
pete wrote:
.... snip ...
size_t revstring(char *stg)
{
char *last, temp;
size_t lgh;

lgh = strlen(stg);
if (lgh > 1) {
last = stg + lgh - 1;
do {
temp = *stg; *stg++ = *last; *last-- = temp;
} while (last > stg);
}
return lgh;
} /* revstring */


I think that works too, and is clear.

--
Read about the Sony stealthware that is a security leak, phones
home, and is generally illegal in most parts of the world. Also
the apparent connivance of the various security software firms.
http://www.schneier.com/blog/archive...drm_rootk.html
Mar 25 '06 #48

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

Similar topics

59
4357
by: Raymond Hettinger | last post by:
Please comment on the new PEP for reverse iteration methods. Basically, the idea looks like this: for i in xrange(10).iter_backwards(): # 9,8,7,6,5,4,3,2,1,0 <do something with i> The HTML version is much more readable than the ReST version. See: http://www.python.org/peps/pep-0322.html
8
3671
by: Jim Langston | last post by:
I have a class I designed that stores text chat in a std::vector<sd::string>. This class has a few methods to retrieve these strings to be displayed on the screen. void ResetRead( bool Reverse, bool wordWrap ); // Resets iterator. We can ignore wordwrap for now. std::string GetLine(); // Reads a line using iterator. Increments Iterator. Right now it only works in Reverse, since that's the method I used first,
21
3989
by: google | last post by:
I'm trying to implement something that would speed up data entry. I'd like to be able to take a string, and increment ONLY the right-most numerical characters by one. The type structure of the data that is in this field can vary. It's a list of mechanical equipment, and how it is designated varies based on how the customer has them labeled. For example, a list of their equipment might look like: CH-1 CH-2 CH-3
20
33082
by: sahukar praveen | last post by:
Hello, I have a question. I try to print a ascii file in reverse order( bottom-top). Here is the logic. 1. Go to the botton of the file fseek(). move one character back to avoid the EOF. 2. From here read a character, print it, move the file pointer (FILE*) to 2 steps back (using fseek(fp, -2, SEEK_CUR)) to read the previous character. This seems to be ok if the file has a single line (i.e. no new line character). The above logic...
24
3287
by: Sathyaish | last post by:
This one question is asked modally in most Microsoft interviews. I started to contemplate various implementations for it. This was what I got. #include <stdio.h> #include <stdlib.h> #include <string.h> char* StrReverse(char*);
41
3395
by: rick | last post by:
Why can't Python have a reverse() function/method like Ruby? Python: x = 'a_string' # Reverse the string print x Ruby: x = 'a_string' # Reverse the string
15
6274
by: rajash | last post by:
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; }
1
10763
by: sunnyluthra1 | last post by:
Hi, I was creating an Application in MS Access for Geocoding a particular Address from Google to get the Lat & Long. I successfully able to did that. Here is the code: **************************** On Error Resume Next 'if address not found, just move along Dim xml_document As DOMDocument Set xml_document = New DOMDocument Dim rootNode As IXMLDOMNode
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
9710
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
10593
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10329
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
10085
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
9163
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
6858
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5527
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...
0
5663
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
3000
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.