Skip to content

Instantly share code, notes, and snippets.

@iAmWillShepherd
Created November 8, 2017 18:17
Show Gist options
  • Save iAmWillShepherd/b5d034e1fc345b79978fe747cfb404e4 to your computer and use it in GitHub Desktop.
Save iAmWillShepherd/b5d034e1fc345b79978fe747cfb404e4 to your computer and use it in GitHub Desktop.
//
// main.c
// StringGenerator
//
// Created by William Shepherd on 11/8/17.
// Copyright © 2017 William Shepherd. All rights reserved.
//
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
const char *ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
const int ALPHABET_SIZE = 52;
void genStr(int k);
void genStrHelper(char *seed, int index);
char *strRep(char c, int n);
int main(int argc, const char * argv[])
{
genStr(5);
return 0;
}
void genStr(int k)
{
char *seedStr;
for (int i = 1; i <= k; i++)
{
for (int j = 0; j < ALPHABET_SIZE; j++)
{
seedStr = strRep(ALPHABET[j], i);
// printf("%s\n", seedStr);
genStrHelper(seedStr, 1);
free(seedStr);
}
}
}
void genStrHelper(char *seed, int index)
{
long seed_length = strlen(seed);
if(seed == NULL || seed_length < 2 || index >= seed_length)
return;
int currCharIdx = seed[index] - ALPHABET[0];
for (int i = 0; i < ALPHABET_SIZE; i++)
{
seed[index] = ALPHABET[(currCharIdx + i + 1) % ALPHABET_SIZE];
// printf("%s\n", seed);
genStrHelper(seed, index + 1);
}
}
char *strRep(char c, int n)
{
char *result = (char *)malloc(sizeof(char) * (n + 1));
for (int i = 0; i < n; i++)
result[i] = c;
result[n] = '\0';
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment