-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstringtofloat.c
172 lines (100 loc) · 2.18 KB
/
stringtofloat.c
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#include <stdio.h>
#include <math.h>
// FINDING THE LENGTH OF THE STRING
int length ( char word[])
{
int count=0;
while(word[count] != '\0')
++count;
return count;
}
int strToInt ( char string[])
{
int i,intValue,result=0;
if(string[0] == '-')
{
for(i=1;string[i]>= '0' && string[i] <='9';++i)
{
intValue = string[i] -'0' ;
result =result*10+intValue;
}
return (result*-1);
}
else if(string[0] != '-')
{
for(i=0;string[i]>= '0' && string[i] <='9';++i)
{
intValue = string[i] - '0' ;
result =result*10+intValue;
}
}
return (result);
}
//FINDING THE POSTION OF THE POINT
int findString ( char source[], char temp[])
{
int i,j,k;
int length ( char word[]);
int length1,length2;
length1 = length(source);
length2 = length(temp);
for(i=0;i<length1;++i)
{
if(source[i] == temp[0])
{
for(j=0;j<length2;++j)
if(source[i] == temp[j] && j!= (length2 - 1))
++i;
else if((source[i] == temp[j]) && j == (length2 - 1) )
return i-(length2-1);
else
break;
}
}
return -1;
}
//REMOVING THE POINT AND CONVERTING TO AN INTEGER STRING
int removeString (char phrase[],int start,int count)
{
int findString ( char source[], char temp[]);
int strToInt ( char string[]);
int i,number,j;
int length ( char word[]);
int m,intValue,result=0;
float final;
int where;
where = findString(phrase,".");
number = length(phrase);
char new[81];
int x;
for(j=0;j<where;++j)
{
new[j] = phrase[j];
}
for(j=where;j<number;++j)
{
new[j] = phrase[j+1];
}
final = strToInt(new);
float answer;
int value;
int cow;
cow = number-where-1;
value = pow(10,cow);
answer = final/value;
printf("%.5f",answer);
return 0;
}
int main()
{
int length ( char word[]);
int findString ( char source[], char temp[]);
int removeString (char phrase[],int start,int count);
char number[] = "382.679";
int lengthOfString;
lengthOfString = length(number);
int position;
position = findString(number,".");
removeString(number,position,1);
return 0;
}