could any one please give me a code to reverse a string of more than
1MB .???
Thanks in advance 47 5024
"sudharsan" <su*********@gm ail.com> wrote in news:1142873798 .329339.167240
@e56g2000cwe.go oglegroups.com: could any one please give me a code to reverse a string of more than 1MB .???
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 <1u**@llenroc.u de.invalid>
(remove .invalid and reverse each component for email address)
comp.lang.perl. misc guidelines on the WWW: http://mail.augustmail.com/~tadmc/cl...uidelines.html
sudharsan 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?
--
BR, Vladimir
I'll meet you... on the dark side of the moon...
-- Pink Floyd
sudharsan wrote: could any one please give me a code to reverse a string of more than 1MB .??? Thanks in advance
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 <su*********@gm ail.com> wrote: could any one please give me a code to reverse a string of more than 1MB .??? Thanks in advance
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: 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;
else {
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;
}
int main(void) {
char arr[32];
puts("Please enter your name:");
if(fgets(arr, 32, stdin) == NULL)
return EXIT_FAILURE;
else {
if(!strrev(arr) )
puts("strrev() returned false.");
else
printf("Your name reversed:\n%s\n ", arr);
}
return EXIT_SUCCESS;
}
"santosh" <sa*********@gm ail.com> wrote in news:1142879921 .107221.266900
@g10g2000cwb.go oglegroups.com: 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;
separation
if(s == NULL || (l = strlen(s)) < 2) return false;
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 <1u**@llenroc.u de.invalid>
(remove .invalid and reverse each component for email address)
comp.lang.perl. misc guidelines on the WWW: http://mail.augustmail.com/~tadmc/cl...uidelines.html
A. Sinan Unur wrote: "santosh" <sa*********@gm ail.com> wrote in news:1142879921 .107221.266900 @g10g2000cwb.go oglegroups.com:
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;
separation
if(s == NULL || (l = strlen(s)) < 2) return false;
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.
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?
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.
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.
Are you using the return value as a 'success' indicator?
Yes.
In that case, why is it an error condition to not modify the passed string?
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 <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?
else {
Its all an else in this case since you returned.
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 */
}
} int main(void) { char arr[32];
puts("Please enter your name:"); if(fgets(arr, 32, stdin) == NULL) return EXIT_FAILURE; else { if(!strrev(arr) ) puts("strrev() returned false."); else printf("Your name reversed:\n%s\n ", arr); }
return EXIT_SUCCESS; }
--
Debuggers : you know it makes sense. http://heather.cs.ucdavis.edu/~matlo...g.html#tth_sEc
"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
--
A. Sinan Unur <1u**@llenroc.u de.invalid>
(remove .invalid and reverse each component for email address) 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: 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...
|
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: 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...
|
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: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
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();...
|
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: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |