I am trying to create a function fib(n) that takes the two characters a and b as initial arguments then prints every single term of the Fibonacci sequence for a given index n
the sequence is described as:
S0=" "
S1="b"
S2="a"
Sn="S(n-1)S(n-2)"
meaning for example the result for n=6 should be :
"b","a","ab","aba",abaab","abaababa"...
My issue is when I run the code the strings get random symbols added to them which if removed would give the desired result and I can't find the reason for that anywhere.
When I run the code and give n the value 6 This is the returned result
here is the code :
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void combine(char**s,char**ch1,char**ch2)
{
    int size=strlen(*ch1)+strlen(*ch2);
    free(*s);
    *s=(char*)malloc(size*sizeof(char)+1);
    strcpy(*s,*ch1);
    strcat(*s,*ch2);
}
void replace(char**ch1,char**ch2)
{
    free(*ch1);
    *ch1=(char*)malloc(strlen(*ch2)*sizeof(char)+1);
    strcpy(*ch1,*ch2);
}
void fib(int n)
{
    char*a,*b,*s;
    int i;
    printf("S0 = ' '\n");
    if(n>=1)
    {   
        printf("S1 = 'b'\n");
        if(n>=2)
        {
            printf("S2 = 'a'\n");
            if(n>2)
            {
                s=(char*)malloc(sizeof(char)+1);
                b=(char*)malloc(sizeof(char)+1);b[0]='b';
                a=(char*)malloc(sizeof(char)+1);a[0]='a';
                for(i=3;i<=n;i++)
                {
                    combine(&s,&a,&b);
                    printf("S%d = '%s'\n",i,s);
                    replace(&b,&a);
                    replace(&a,&s);
                }
                
            }
        }
    }
}
int main()
{
    int n;
    char *a,*b,*s;
    printf("Give the index at which to stop : ");
    scanf("%d",&n);
    fib(n);
}
 
     
    