Created
January 20, 2016 03:11
-
-
Save Discordanian/c44ee991a01fe1f9b776 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <stdio.h> | |
| #include <stdlib.h> | |
| // pretend that 'a' is 0 and 'b' is one. | |
| // return 1 if we can continue | |
| // return 0 if we are finished | |
| int addOne(char a, char b, int i, char * str) { | |
| int leftMostZero = i; | |
| for (leftMostZero = i -1; leftMostZero >=0 && str[leftMostZero] == b;leftMostZero--) { | |
| } | |
| if (leftMostZero < 0) { return 0; } | |
| else { | |
| str[leftMostZero] = b; | |
| while(++leftMostZero < i) { str[leftMostZero] = a; } | |
| } | |
| // find first instance of a zero | |
| return 1; | |
| } | |
| void splay(char a, char b, int i) { | |
| char * buffer = (char *) malloc(i+1); // need to terminate with a null | |
| // init buffer to all 'zeros'. | |
| for (int j = 0; j < i; j++) buffer[j] = a; | |
| buffer[i] = '\0'; | |
| printf("%s\n",buffer); | |
| while(addOne(a,b,i,buffer)) { | |
| printf("%s\n",buffer); | |
| } | |
| free(buffer); | |
| } | |
| int main(int argc, char * argv[]) { | |
| if(argc < 4) { | |
| printf("Pass more stuff in (%d).\n",argc); | |
| return 1; | |
| } | |
| splay(argv[1][0], argv[2][0], (int) strtol(argv[3], (char **)NULL, 10)); | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment