/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
/*
 * coins.c
 * Copyright (C) 2013 drdev <drdev@localhost.com>
 * 
 * coins is free software: you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * coins is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along
 * with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <stdio.h>
#include <string.h>

//Prototypes
char *replace_5(char *source_string);
char *replace_10(char *source_string);
char *replace_25(char *source_string);

char initial_string[]="25,25,25,10,10,01,01,01,01";
char working_string[512] = "";
char buffer_string[512] = "";
int str_len = 0;
int counter = 0;


char *replace_25(char *source_string)
{
//replaces first occurence of "25" in string by "10,10,05"
char *pos;
char new_string[2000]="";

if ((pos=strstr(source_string,"25"))!=NULL)
{
//Note pointer subtraction to get a length
strncpy(new_string, source_string, pos-source_string);
strcat(new_string, "10,10,05");
strcat(new_string, pos+2);
}
else
{
strcat(new_string, source_string);
}

return new_string;
}



char *replace_10(char *source_string)
{
//replaces first occurence of "10" in string by "05,05"
char *pos;
char new_string[2000]="";

if ((pos=strstr(source_string,"10"))!=NULL)
{
//Note pointer subtraction to get a length
strncpy(new_string, source_string, pos-source_string);
strcat(new_string, "05,05");
strcat(new_string, pos+2);
}
else
{
strcat(new_string, source_string);
}

return new_string;
}


char *replace_5(char *source_string)
{
//replaces first occurence of "05" in string by "01,01,01,01,01"
char *pos;
char new_string[2000]="";

if ((pos=strstr(source_string,"05"))!=NULL)
{
//Note pointer subtraction to get a length
strncpy(new_string, source_string, pos-source_string);
strcat(new_string, "01,01,01,01,01");
strcat(new_string, pos+2);
}
else
{
strcat(new_string, source_string);
}

return new_string;
}


int main()
{
str_len=0;
working_string[0]='\0';

strcpy(working_string,initial_string);

//Handle initial string
printf("{%s}\n",working_string);
counter++;

do
{
//Replace 25 with 10+10+5
str_len=strlen(working_string);
buffer_string[0]='\0';
strcat(buffer_string,replace_25(working_string));
strcpy(working_string,buffer_string);
if(strlen(working_string)!=str_len)
{
printf("{%s}\n",working_string);
counter++;
}
}
while (strlen(working_string)!=str_len);


do
{
//Replace 10 by 5+5
str_len=strlen(working_string);
buffer_string[0]='\0';
strcat(buffer_string,replace_10(working_string));
strcpy(working_string,buffer_string);
if(strlen(working_string)!=str_len)
{
printf("{%s}\n",working_string);
counter++;
}
}
while (strlen(working_string)!=str_len);

do
{
//Replace 5 by 1+1+1+1+1
str_len=strlen(working_string);
buffer_string[0]='\0';
strcat(buffer_string,replace_5(working_string));
strcpy(working_string,buffer_string);
if(strlen(working_string)!=str_len)
{
printf("{%s}\n",working_string);
counter++;
}
}
while (strlen(working_string)!=str_len);

printf("\n\nTotal number = %d",counter);
	
	return (0);
}

