Files
Digital-Research-Source-Code/CPM OPERATING SYSTEMS/CPM 68K/cpm68k_pgms/snobol4/RPERM.C
Sepp J Morris 31738079c4 Upload
Digital Research
2020-11-06 18:50:37 +01:00

1 line
1.5 KiB
C
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/* -*-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
}
}