could any one please give me a code to reverse a string of more than
1MB .???
Thanks in advance
Mar 20 '06
47 5025
Richard G. Riley wrote: On 2006-03-20, santosh <sa*********@gm ail.com> wrote: sudharsan wrote: could any one please give me a code to reverse a string of more than 1MB .??? Thanks in advance
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;
It's legal to reverse a string of one character. Or does "false" just indicate no reversal was necessary?
Sinan pointed it out earlier. I should've decoupled the tests and
returned true for the case you suggest. else {
Its all an else in this case since you returned.
I'm sorry, I don't get what you're trying to say here. 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; }
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 */ } }
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?
Thanks for the feedback.
On 2006-03-20, A. Sinan Unur <1u**@llenroc.u de.invalid> wrote: "Richard G. Riley" <rg****@gmail.c om> wrote in news:tub3f3-03i.ln1 @fujitsu.mydoma in.com:
void reverse(char * refS){ /* assume parent does parameter checks */ unsigned int len = (unsigned int)strlen(refS );
size_t len = strlen(refS);
Sinan
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 <sa*********@gm ail.com> wrote: Richard G. Riley wrote: Its all an else in this case since you returned. I'm sorry, I don't get what you're trying to say here.
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; 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?
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. Thanks for the feedback.
"Richard G. Riley" <rg****@gmail.c om> wrote in news:did3f3-k3j.ln1
@fujitsu.mydoma in.com: On 2006-03-20, A. Sinan Unur <1u**@llenroc.u de.invalid> wrote: "Richard G. Riley" <rg****@gmail.c om> wrote in news:tub3f3-03i.ln1 @fujitsu.mydoma in.com:
void reverse(char * refS){ /* assume parent does parameter checks */ unsigned int len = (unsigned int)strlen(refS ); size_t len = strlen(refS);
.... 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?
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 <1u**@llenroc.u de.invalid>
(remove .invalid and reverse each component for email address)
"Richard G. Riley" <rg****@gmail.c om> writes: On 2006-03-20, A. Sinan Unur <1u**@llenroc.u de.invalid> wrote: "Richard G. Riley" <rg****@gmail.c om> wrote in news:tub3f3-03i.ln1 @fujitsu.mydoma in.com:
void reverse(char * refS){ /* assume parent does parameter checks */ unsigned int len = (unsigned int)strlen(refS );
size_t len = strlen(refS);
Sinan
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?
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) ks***@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.
sudharsan wrote: could any one please give me a code to reverse a string of more than 1MB .???
Sure. It may not be very fast for such a size, but assuming you
can create the string in the first place it should reverse it.
Don't forget to #include <string.h>. This avoids extra calls on
strlen by returning its value.
/* =============== ======== */
/* reverse string in place */
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 */
--
"If you want to post a followup via groups.google.c om, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell. org/google/>
Also see <http://www.safalra.com/special/googlegroupsrep ly/>
In article <cv************ @fujitsu.mydoma in.com>,
Richard G. Riley <rg****@gmail.c om> wrote: could any one please give me a code to reverse a string of more than 1MB .???
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.
Here's a program that reverses a string in an obvious way. It
reverses the substring after the first character and then puts the
first character onto the end, taking care to free the temporary
strings as soon as possible. It seems to work nicely for 0, 1, 2, and
3 characters.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static char *nrev(const char *old);
int main(int argc, char **argv)
{
char *old, *new;
int l;
if(argc != 2)
{
fprintf(stderr, "usage: nrev length\n");
return 2;
}
l = atoi(argv[1]);
old = malloc(l + 1);
memset(old, 'x', l);
old[l] = 0;
new = nrev(old);
return 0;
}
static char *nrev(const char *old)
{
int len;
char *new;
char *t;
len = strlen(old);
new = malloc(len+1);
if(len > 0)
{
t = nrev(old+1);
memcpy(new, t, len-1);
free(t);
new[len-1] = old[0];
}
new[len] = 0;
return new;
}
-- Richard
On 2006-03-20, sudharsan <su*********@gm ail.com> wrote: could any one please give me a code to reverse a string of more than 1MB .??? Thanks in advance
What have you tried that doesn't work with a 1MB string?
A favorite job interview question is to reverse a string of arbitrary
size in place.
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.
In article <rc************ ********@comcas t.com>,
Charles Krug <cd****@aol.com > 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...
-- Richard ri*****@cogsci. ed.ac.uk (Richard Tobin) writes: In article <rc************ ********@comcas t.com>, Charles Krug <cd****@aol.com > 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.
--
Keith Thompson (The_Other_Keit h) ks***@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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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
|
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,
|
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
|
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...
|
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*);
| |
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
|
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; }
|
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
|
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;
}
|
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,...
|
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...
| |
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,...
|
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...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
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...
| |