mirror of
https://github.com/SEPPDROID/Digital-Research-Source-Code.git
synced 2025-10-23 00:14:25 +00:00
1 line
1.5 KiB
C
1 line
1.5 KiB
C
/* -*-c,save-*- */
|
||
|
||
/*
|
||
|
||
* rperm.c - random permutate a string
|
||
|
||
* Robert Heller. Created: Sun Mar 9, 1986 16:13:17.62
|
||
|
||
* Last Mod:
|
||
|
||
*
|
||
|
||
* (c) Copyright 1986 by Robert Heller
|
||
|
||
* All Rights Reserved
|
||
|
||
*
|
||
|
||
*
|
||
|
||
*/
|
||
|
||
|
||
|
||
#include <stdio.h>
|
||
|
||
|
||
|
||
#define LOCAL static
|
||
|
||
|
||
|
||
/* #define DEBUG /* */
|
||
|
||
#ifdef DEBUG
|
||
|
||
#define LOCAL /* static */
|
||
|
||
#endif
|
||
|
||
|
||
|
||
rpermute(input,output)
|
||
|
||
char *input,*output;
|
||
|
||
{
|
||
|
||
register char *p1,*p2;
|
||
|
||
register int len,indx,t;
|
||
|
||
|
||
|
||
p2 = output;
|
||
|
||
*p2 = '\0';
|
||
|
||
for (p1=input,len=0;*p1 != '\0';p1++,len++) {
|
||
|
||
t = *p1;
|
||
|
||
indx = rand() % (len+1);
|
||
|
||
#ifdef DEBUG
|
||
|
||
printf("***len=%d, indx=%d, t=%c\n",len,indx,t);
|
||
|
||
#endif
|
||
|
||
pushdown(p2+indx,len-indx);
|
||
|
||
#ifdef DEBUG
|
||
|
||
printf("***(before insert)p2=%s\n",p2);
|
||
|
||
#endif
|
||
|
||
*(p2+indx) = t;
|
||
|
||
#ifdef DEBUG
|
||
|
||
printf("***(after insert)p2=%s\n",p2);
|
||
|
||
#endif
|
||
|
||
}
|
||
|
||
output[len] = '\0';
|
||
|
||
}
|
||
|
||
char *a_rpermute(input)
|
||
|
||
register char *input;
|
||
|
||
{
|
||
|
||
register char *output;
|
||
|
||
char *malloc();
|
||
|
||
|
||
|
||
output = malloc(strlen(input)+1);
|
||
|
||
if (output == NULL) {
|
||
|
||
perror("rpermute");
|
||
|
||
abort(0);
|
||
|
||
}
|
||
|
||
rpermute(input,output);
|
||
|
||
return(output);
|
||
|
||
}
|
||
|
||
LOCAL pushdown(start,len)
|
||
|
||
register char *start;
|
||
|
||
register int len;
|
||
|
||
{
|
||
|
||
register char *eos;
|
||
|
||
#ifdef DEBUG
|
||
|
||
printf("***(pushdown): len=%d, start=%s\n",len,start);
|
||
|
||
#endif
|
||
|
||
eos = start+len;
|
||
|
||
while (len-- > 0) {
|
||
|
||
*eos = *(eos-1);
|
||
|
||
eos--;
|
||
|
||
#ifdef DEBUG
|
||
|
||
printf("***(pushdown loop): len=%d, start=%s\n",len,start);
|
||
|
||
#endif
|
||
|
||
}
|
||
|
||
}
|
||
|
||
|