include stdio include stdlib include string int string_length char poi

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int string_length(char *pointer)
{
int c = 0;
while( *(pointer+c) != '\0' )
c++;
return c;
}
void reverse(char *string)
{
int length, c;
char *begin, *end, temp;
length = string_length(string);
begin = string;
end = string;
for ( c = 0 ; c < ( length - 1 ) ; c++ )
end++;
for ( c = 0 ; c < length/2 ; c++ )
{
temp = *end;
*end = *begin;
*begin = temp;
begin++;
end--;
}
}
char *fibstr (int n)
{
unsigned long fibArray[92];
unsigned long i;
char FibString1[60]="a",FibString2[60]="b";
char *FibString;
fibArray[1]=1;
fibArray[2]=2;
for(i=3;i<=n;i++)
{
fibArray[i] = fibArray[i-1] + fibArray[i-2];
}
i--;
FibString =(char*)malloc((fibArray[i]+1)*sizeof(char));
FibString[0] = '\0';
if(n==1)
{
strncat(FibString,FibString1,fibArray[1]);
}
if(n==2)
{
strncat(FibString,FibString2,fibArray[2]);
}
if(n>2)
{
strncat(FibString,FibString2,fibArray[1]);
strncat(FibString,FibString1,fibArray[2]);
i=2;
if(n==3)
{
reverse(FibString);
return FibString;
}
while(i<n-1)
{
strncpy(FibString1,FibString,fibArray[i-1]+1);
strncat(FibString,FibString1,fibArray[i-1]);
i++;
}
}
reverse(FibString);
return FibString;
}
int main()
{
int n;
char *FibString2;
scanf("%d", &n);
FibString2=fibstr(n);
printf("%s\n", FibString2 );
free(FibString2);
return 0;
}