-
Notifications
You must be signed in to change notification settings - Fork 9
/
helper_functions.c
109 lines (89 loc) · 2.01 KB
/
helper_functions.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
//
// helper_functions.c
// CleverModels
//
// Created by Bruno Martins on 3/27/12.
// Copyright (c) 2012. All rights reserved.
//
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "helper_functions.h"
void preppend_to_file(const char *t, FILE *fp)
{
char* buf;
long len = 0;
fseek(fp,0,SEEK_END);
len = ftell(fp);
fseek(fp, 0, SEEK_SET);
buf = (char *)malloc(len);
fread(buf,len,1,fp);
fseek(fp,0,SEEK_SET);
fprintf(fp, "%s",t);
fprintf(fp, "%s",buf);
fseek(fp,0,SEEK_END);
free(buf);
}
//TODO: Actually implement this
char* str_plural_to_singular(char str[])
{
// long pos_s_char = str_pos_reverse('s', str, strlen(str), 1);
// //to avoid taking away the last S on double S's ending strings. Like : Adress, is still singular.
// long pos_s_second_char = str_pos_reverse('s', str, strlen(str),2);
//
// if (-1 == pos_s_char || pos_s_char == pos_s_second_char+1)
// return str;
//
// long len = strlen(str);
//
// printf("str : %s \n", str);
//
// if(len > 1)
// str[pos_s_char] = '\0';
//
// printf("after str : %s \n", str);
return str;
}
int str_pos(char c, char haystack[], int occurence)
{
int times_found = 0;
int i;
for (i=0; i<strlen(haystack); i++) {
if (c == haystack[i]) {
times_found++;
if (occurence == times_found)
return i;
}
}
return -1;
}
int str_pos_reverse (char c, char haystack[], int star_pos, int occurence)
{
int times_found = 0;
int i;
for (i = star_pos; i>=0; i--) {
if (c == haystack[i]) {
times_found++;
if (occurence == times_found)
return i;
}
}
return -1;
}
void str_to_lower(char str [])
{
int i;
for (i = 0; str[i]; i++) {
str[i] = tolower(str[i]);
}
}
char find_first_character(char haystack[])
{
int i;
for (i = 0; i<strlen(haystack); i++) {
if (' ' != haystack[i])
return haystack[i];
}
return -1;
}