Skip to content

Instantly share code, notes, and snippets.

@kadel
Created January 2, 2012 20:20
Show Gist options
  • Save kadel/1551957 to your computer and use it in GitHub Desktop.
Save kadel/1551957 to your computer and use it in GitHub Desktop.
hakan - bwt
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define TRUE 1
#define FALSE 0
typedef char bool;
int cmp_rows(const void * a, const void * b) {
printf("compare: | %s | %s |", a,b);
return strcmp(a,b);
}
int main () {
FILE *fp = fopen("hakan.in", "r");
//get filesize
fseek(fp, 0L, SEEK_END);
long file_size = ftell(fp);
fseek(fp, 0L, SEEK_SET);
file_size = file_size - 1;
// allocate table
char **table = (char **)malloc(file_size * sizeof(char *));
for(long i = 0; i < file_size; i++) {
table[i] = (char *)malloc(file_size * sizeof(char));
}
// read file - first row in table
char c = 0;
long pos = 0;
while ((c = fgetc(fp)) != EOF ){
table[0][pos] = c;
pos++;
}
// do mutations
for (long i=1; i<file_size; i++) {
for (long j=0; j<file_size; j++) {
long copy_pos = j - 1;
if (copy_pos < 0) {
copy_pos = file_size -1;
}
table[i][j] = table[i-1][copy_pos];
}
}
//print table
for (long i=0; i<file_size; i++) {
for (long j=0; j<file_size; j++) {
printf("%c", table[i][j]);
}
printf("\n");
}
// sort
qsort(table, file_size, file_size*sizeof(char *), cmp_rows);
//print table
for (long i=0; i<file_size; i++) {
for (long j=0; j<file_size; j++) {
printf("%c", table[i][j]);
}
printf("\n");
}
fclose(fp);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment