ph****@uk-businessdirectory.co.uk said:
To practice some C I have been looking at the Burrows-Wheeler
Transform.
Ive got a small program that builds the initial table of possible
combinations of the original string, used to find the compressed
result, but there are some notes about using BWT without having to
build a table of strings, instead using pointers to the offset in the
original and just sorting those.
My question is how do you return a pointer to "bcda" from "abcd"
I started by doing something like
char * ptr[input_str];
for i =0 ; i < len(input_str); i++) {
ptr[i] = (input_str + i);
}
but then that only gives "abcd" -"bcd " when i = 1.
This looks very shaky. Where is the memory for the strings? Do you "own"
it, or do the pointers (for char *ptr[input_str] is an array of pointers,
not an array of strings) point to string literals? If they do, modifying
the strings invokes undefined behaviour - all you might usefully do is
reorder the pointers.
If you own memory for a string, you can "rotate" it like this:
#include <string.h>
void rotate_string_one_place(char *s, size_t len)
{
char t = *s;
memmove(s, s + 1, len - 1);
s[len - 1] = t;
}
/* sample driver */
#include <stdio.h>
#include <string.h/* not needed if you're doing this all in one file
because it's already included above */
int main(void)
{
char sample[] = "Yes, I own the memory for this string";
size_t len = strlen(sample);
size_t i;
for(i = 0; i < len; i++)
{
rotate_string_one_place(sample, len);
printf("%s\n", sample);
}
return 0;
}
Sample output:
es, I own the memory for this stringY
s, I own the memory for this stringYe
, I own the memory for this stringYes
I own the memory for this stringYes,
I own the memory for this stringYes,
own the memory for this stringYes, I
own the memory for this stringYes, I
wn the memory for this stringYes, I o
n the memory for this stringYes, I ow
the memory for this stringYes, I own
the memory for this stringYes, I own
he memory for this stringYes, I own t
e memory for this stringYes, I own th
memory for this stringYes, I own the
memory for this stringYes, I own the
emory for this stringYes, I own the m
mory for this stringYes, I own the me
ory for this stringYes, I own the mem
ry for this stringYes, I own the memo
y for this stringYes, I own the memor
for this stringYes, I own the memory
for this stringYes, I own the memory
or this stringYes, I own the memory f
r this stringYes, I own the memory fo
this stringYes, I own the memory for
this stringYes, I own the memory for
his stringYes, I own the memory for t
is stringYes, I own the memory for th
s stringYes, I own the memory for thi
stringYes, I own the memory for this
stringYes, I own the memory for this
tringYes, I own the memory for this s
ringYes, I own the memory for this st
ingYes, I own the memory for this str
ngYes, I own the memory for this stri
gYes, I own the memory for this strin
Yes, I own the memory for this string
--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999