[Libclc-developers] new function: clc_strrev
Status: Planning
Brought to you by:
augestad
|
From: <bo...@me...> - 2003-03-23 11:09:39
|
We can't have a string module without a strrev function, can we?
I could not find a proposal for one on google, so here is my version.
q1: Do we need it?
q2: any bugs?
q3: other comments?
Enjoy.
Bjørn
/* $id$
* Copyright(c) 2003, B. Augestad (bo...@me...)
*/
/* documentation start:
SYNOPSIS
#include <clc_string.h>
char* clc_strrev(char* s);
DESCRIPTION
clc_strrev() - reverses the contents of a string.
PARAMETERS
s - The string to reverse
RETURN VALUE
Returns the parameter s
ERROR HANDLING
The release version of libclc performs no error checking. The debug
version asserts that s isn't a NULL pointer.
EXAMPLE
char buf[100];
strcpy(buf, "Hello, World");
clc_strrev(buf);
printf("%s\n", buf);
AUTHOR
B. Augestad, bo...@me...
BUGS
None known
* documentation end:
*/
#include <string.h>
#include "clc_assert.h"
#include "clc_string.h"
char* clc_strrev(char* s)
{
size_t left, right, middle, cb;
clc_assert_not_null(clc_strrev, s);
cb = strlen(s);
middle = cb / 2;
for(left = 0; left < middle; left++) {
char tmp;
right = cb - left - 1;
tmp = s[left];
s[left] = s[right];
s[right] = tmp;
}
return s;
}
#ifdef CLC_TEST
int main(void)
{
char odd[100], even[100], empty[100];
strcpy(odd, "12345");
strcpy(even, "123456");
strcpy(empty, "");
printf("odd :%s\n", clc_strrev(odd));
printf("even :%s\n", clc_strrev(even));
printf("empty:%s\n", clc_strrev(empty));
return 0;
}
#endif
|