-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstringUtils.c
218 lines (178 loc) · 4.47 KB
/
stringUtils.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#include "stringUtils.h"
#define MAX_STRING_LENGTH 256
#define DATE_STRING_LENGTH 11
#define _CRT_SECURE_NO_WARNINGS
#pragma warning(disable:4996)
#pragma warning(disable:4013) // gets dosn't fit for this ass
char getCharNOnewLine() {
char input[10] = {0};
int fuckCCompilerForThisError = gets(input);
return input[0];
}
char* getStrExactLength() {
char* pN;
char input[MAX_STRING_LENGTH];
gets(input);
pN = _strdup(input);
removeSpaces(pN);
capitalizeFirstLetter(pN);
return pN;
}
char* getStrExactLengthFromFile(FILE* f) {
char temp[MAX_STRING_LENGTH];
myGets(temp, MAX_STRING_LENGTH, f);
return _strdup(temp);
//if (!f)
// return NULL;
//char sentence[MAX_STRING_LENGTH], *pN;
//
//fgets(sentence, MAX_STRING_LENGTH, f); // dropeed &
//pN = _strdup(sentence);
//return pN;
}
int capitalizeFirstLetter(char* changeMe) {
// call function after calling _removeSpaces_
int asciVal = (int)changeMe[0];
if (asciVal >= 65 && asciVal <= 90) return 1;
if (asciVal >= 97 && asciVal <= 122) {
changeMe[0] = changeMe[0] - 32;
return 1;
}
return 0; // ascii is not a char
}
void removeSpaces(char* trimMe) {
int i, j;
for (i = 0; trimMe[i] == ' '; i++) {}
int spacesCounter = 0;
for (j = i; trimMe[j] != '\0'; j++) {
trimMe[j - i] = trimMe[j];
if (trimMe[j] == ' ' && (trimMe[j + 1] == ' ' || trimMe[j + 1] == '\0'))
i++; // if found more than one space - jump over the rest.
}
if (trimMe[j - 1] == ' ')
trimMe[j - 1] = trimMe[j];
}
char* getDate() {
printf("insert date in next format 23/05/1995\n");
char* date;
date = getStrExactLength();
while (checkDate(date) == 0) {
printf("Use the next format : 23/05/2002\n");
date = getStrExactLength();
}
return date;
} // modify
int DateToInt(char* dateString, int* day, int* month, int* year) {
char* input = dateString;
int nDay = 0, nMonth = 0, nYear = 0; // new day
if (strlen(input) != DATE_STRING_LENGTH - 1) return 0;
int i = 0;
for (i; i < 2; i++) {
if (!isdigit(input[i]))return 0;
if (i == 0)
nDay += 10 * charToDigit(input[i]);
else // i==1
nDay += charToDigit(input[i]);
}
// _ _
if (input[i] != 47) return 0; // ASCII -> [ '/' == 47 ]
i++;
// _ _ /
for (i; i < 5; i++) {
if (!isdigit(input[i]))return 0;
if (i == 3)
nMonth += 10 * charToDigit(input[i]);
else // i==4
nMonth += charToDigit(input[i]);
}
// _ _ / _ _
if (input[i] != 47) return 0;
i++;
// _ _ / _ _ /
for (i; i < 10; i++) {
if (!isdigit(input[i]))return 0;
}
nYear += 1000 * charToDigit(input[6]);
nYear += 100 * charToDigit(input[7]);
nYear += 10 * charToDigit(input[8]);
nYear += charToDigit(input[9]);
// _ _ / _ _ / _ _ _ _ GOOD
*day = nDay;
*month = nMonth;
*year = nYear;
return 1;
}
void intsToDate(int day, int month, int year, char* resultExtern) {
snprintf(resultExtern, DATE_STRING_LENGTH, "%02d/%02d/%d", day, month, year);
}
char* intToChar(int TransformMe) {
char retrnMe[DATE_STRING_LENGTH];
snprintf(retrnMe, 5, "%d", TransformMe);
return _strdup(retrnMe); // trims to right size
}
int checkDate(char* input) {
char* pDate = input;
if (strlen(pDate) != 10) return 0;
int i = 0;
for (i; i < 2; i++) {
if (!isdigit(pDate[i]))return 0;
}
// _ _
if (pDate[i] != 47) return 0; // ASCII -> [ '/' == 47 ]
i++;
// _ _ /
for (i; i < 5; i++) {
if (!isdigit(pDate[i]))return 0;
}
// _ _ / _ _
if (pDate[i] != 47) return 0;
i++;
// _ _ / _ _ /
for (i; i < 10; i++) {
if (!isdigit(pDate[i]))return 0;
}
// _ _ / _ _ / _ _ _ _ GOOD
return 1;
}
char* getIATA()
{
char* IATA;
do {
printf("insert 3dig IATA:");
IATA = getStrExactLength();
} while (strlen(IATA) != 3);
return IATA;
}
void copyString(char* source,char* destination)
{
destination = _strdup(source);
//int size1 = sizeof(source);
//if (!(destination = (char*)malloc(sizeof(char)* size1))) //+1 is to make room for the NULL char that terminates C strings
//{
// return;
//}
//strncpy(destination, source, size1);
//note that '\0' is not included with Alpha, it is handled by strcpy
//do whatever with one.name
}
int charToDigit(char replaceMe) {
return (int)replaceMe - 48;
}
char* myGets(char* buffer, int size, FILE* fp)
{
// get a string to buffer.
// after it use (getStrExactSize)
// get from user using: myGets(temp, MAX_STR_LEN, stdin);
// get from file using: myGets(temp, MAX_STR_LEN,(FILE*) fp);
if (buffer != NULL && size > 0)
{
if (fgets(buffer, size, fp))
{
buffer[strcspn(buffer, "\n")] = '\0';
return buffer;
}
buffer[0] = '\0';
}
return NULL;
}
// get from user using: