What is the easiest way to reverse a sting? I thought there was a function, but I cannot remember it. Is there a page that lists all functions of all classes I could look at?
What is the easiest way to reverse a sting? I thought there was a function, but I cannot remember it. Is there a page that lists all functions of all classes I could look at?
easy way would be to copy is to use a loop and some functions from the string header file
http://www.cplusplus.com/ref/cstring/
>What is the easiest way to reverse a sting?
If your compiler supports strrev or anything similar then that's the easiest way. Otherwise, a loop that walks from either end of the string toward the middle and swaps until the two indices are the same would come a close second.
My best code is written with the delete key.
Here is an implementation of strrev
http://64.233.167.104/search?q=cache...hl=en&ie=UTF-8
You could easily implement one yourself, e.g.
1. Iterate from the last character to the first, placing them into a new string.
2. Swap the nth character with the (length - n)th character.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
>Here is an implementation of strrev
A good example of the programmer trying to be clever...
My best code is written with the delete key.
huh Prelude ????
>huh Prelude ????
I was just commenting on this:
It's cute, but shouldn't be used in any real code without an exceptionally good reason. It's hard to use correctly, and very few people understand the nuances.Code:*p1 ^= *p2; *p2 ^= *p1; *p1 ^= *p2;
My best code is written with the delete key.
aha
Wow. Thanks for all the help! I thought there was a built in function, but aparrently my GCC compiler does not support it. So I just wrote my own:
BTW, what does the ^= operator do?Code:char* reverse_str(char *m)/*reflects rotor*/ { char rev[26]; strcpy(rev,m);/*stores rotor for reversal*/ unsigned length=strlen(m); unsigned count;/*counter for string reversal*/ for(count=0;count<length;count++) { m[count]=rev[length-count]; } return m; }
Last edited by Dragoon_42; 04-16-2004 at 10:09 AM. Reason: Forgot a question
hi dragon, i am following this topic. i got segmentation fault while calling your reverse_str function.
my main code:
Code:int main() { char*s ; s = reverse_str("mango"); printf("%s",s); }
output > segmentation fault
why ??
another question, what is unsigned ?? it should be unsigned int. r u sure ur code is ok ?
N.B : is it not necesarry to null terminate inside ?
blue_gene
Hi all,
Just for my opinion, and the nice implentation, I'd definetly go for the strrev() code. It's really nice in my opinion, and it worked perfectly first time I compiled it:
Code 1.1: Using my_strrev()Code:#include <stdlib.h> // for printf char* my_strrev(char *); char* my_strrev(char *string1) { char *p1, *p2; if (!string1 || !*string1) return string1; for (p1 = string1, p2 = string1 + strlen(string1) - 1; p2 > p1; ++p1, --p2) { *p1 ^= *p2; *p2 ^= *p1; *p1 ^= *p2; } return string1; } int main () { char str[256] = {"!looc si sihT"}; printf("%s\n", my_strrev(str) ); // call on function in here for ex. return 0; }
Now I only called the function my_strrev() because any previous header file or DLL linkage might try figthing over which function to use and etc...
I take my hat off to noob2c for finding this function I'm making a replica of the string library, so many thanks, I'll have to add this implementation to my project.
Edit (after noob2c post): Ah, ok. Much thanks to you too Prelude
Hope this helps,
- Stack Overflow
Last edited by Stack Overflow; 04-16-2004 at 12:14 PM.
Segmentation Fault: I am an error in which a running program attempts to access memory not allocated to it and core dumps with a segmentation violation error. This is often caused by improper usage of pointers, attempts to access a non-existent or read-only physical memory address, re-use of memory if freed within the same scope, de-referencing a null pointer, or (in C) inadvertently using a non-pointer variable as a pointer.
no problem..... it was prelude who knew the name of the function .... i did a search to find it. As for ^= that means exclusive or or XOR
Exclusive OR (XOR) returns true only when the two values are different.
Truth table for XOR:^ is a bitwise XOR so if you have:Code:0 1 0| 0 1 1| 1 0
1010 and 1101 you would get: 0111
I always prefer to take the easist route until I'm sure of what is going on. As such I would recommend what Prelude suggest and avoid the codes that use ^= until you are 125% sure you know what it is doing.
Below is a quicky I threw together that uses 0 tricks and should be easy enough to walk through. Hope it is of help:
A test program could look like this:Code:char *strrev(char *str) { unsigned len = strlen(str) - 1; /* Minus one so we don't move the null character */ int count; char ch; for (count=0; count < len; count++, len --) { ch = str[count]; str[count] = str[len]; str[len] = ch; } return str; }
Note: Changing the declaration of msg1 and msg2 from char [] to char * will result in a segmentation fault since that area of memory will be marked as read only.Code:#include <stdio.h> int main (void) { char msg1[]="Hello my baby hello my darling hello my ragtime gal!"; char msg2[]="Save me a kiss by wire, baby my heart is on fire!"; puts("Before"); puts(msg1); puts(msg2); puts("After"); puts(strrev(msg1)); puts(strrev(msg2)); return 0; }