Logo

Programming-Idioms

History of Idiom 41 > diff from v31 to v32

Edit summary for version 32 by 20Megajules:
[C] last version included stdlib but didnt actually use it.

Version 31

2017-11-18, 23:13:30

Version 32

2017-11-19, 03:06:05

Idiom #41 Reverse a string

Create string t containing the same characters as string s, in reverse order.
Original string s must remain unaltered. Each character must be handled correctly regardless its number of bytes in memory.

Illustration

Idiom #41 Reverse a string

Create string t containing the same characters as string s, in reverse order.
Original string s must remain unaltered. Each character must be handled correctly regardless its number of bytes in memory.

Illustration
Imports
#include <stdlib.h>
#include <string.h>
Imports
#include <stdlib.h>
#include <string.h>
Code
int len=strlen(s);
char *t=malloc((len+1)*sizeof(char));
int i;
for(i=0;i<len;i++)
{
	t[i]=s[len-i-1];
}
t[len]=0;
Code
std::string t = s
std::reverse(std::begin(t),std::end(t));
Comments bubble
std::reverse is the built in way of reversing any array form of data. Can be assumed to be the fastest general implementation.