473,795 Members | 3,006 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 #1
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

Mar 20 '06 #2
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

Mar 20 '06 #3
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.

Mar 20 '06 #4
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.
Mar 20 '06 #5
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;
}

Mar 20 '06 #6
"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

Mar 20 '06 #7
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.

Mar 20 '06 #8
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
Mar 20 '06 #9
"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)
Mar 20 '06 #10

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

Similar topics

59
4350
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
3669
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
3987
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
33079
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
3284
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
3388
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
6271
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
10761
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
2729
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
9673
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
10448
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...
0
10217
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
10167
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,...
1
7544
isladogs
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...
0
6784
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
5440
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
5566
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3730
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.