I am trying to create a program in C which takes a user's input and 'evolves' a random string until it matches the input. So far it works fine on input which is 7 characters or less, but as soon as a user enters 8 characters the program doesn't work. I can't figure out where I am going wrong. Here's the code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
void mutate(char ancestor[], char descendent[]);
void evolve(char descendent[]);
int main(void)
{
srand(time(NULL));
char input[50];
printf("Please Enter a String:n");
scanf("%s", input);
evolve(input);
retu 0;
void evolve(char descendent[])
{
size_t length = strlen(descendent);
char ancestor[50];
int i;
for(i = 0; i < length; i++)
ancestor[i] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"[rand() % 52];
while(strcmp(ancestor, descendent)){
mutate(ancestor, descendent);
printf("%sn", ancestor);
}
}
void mutate(char ancestor[], char descendent[])
{
int i;
size_t length = strlen(descendent);
for(i = 0; i < length; i++){
if(ancestor[i] != descendent[i]){
ancestor[i] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"[rand() % 52];
}
}
}
