forked from qtrinh/qpipeline
-
Notifications
You must be signed in to change notification settings - Fork 0
/
my_string.c
123 lines (95 loc) · 2.43 KB
/
my_string.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
//
// written by Quang M Trinh <[email protected]>
//
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <assert.h>
#include "my_string.h"
#include "common.h"
#include "input_data.h"
/**
convert an array of chars to an array of 'strings'
return array with first index starting at 0
**/
char ** my_string_arrayOfChars2arrayOfStrings(struct input_data *id, char input[], char delimeter, int *length) {
char *ptr = input;
int i,n;
char str[MAX_CHAR_PER_LINE];
char **array ;
int localVerbose = 0;
if (id->verbose && localVerbose)
printf ("\n[%s:%d] - input string is '%s' .. \n", __FILE__, __LINE__, input);
// count the number of strings separated by delimeter
n = 0;
while (*ptr) {
if (*ptr == delimeter)
n++;
ptr++;
}
// one more at the end
n++;
if (localVerbose && id->verbose)
printf ("\n[%s:%d] - %d elements in input string '%s'", __FILE__, __LINE__, n,input); fflush(stdout);
// create array and set it to NULL
array = (char **) malloc ((n) * sizeof(char *));
for (i = 0; i < n; i++)
array[i] = NULL;
ptr = input;
i = 0;
n = 0;
while ((*ptr) && (*ptr != '\0')) {
if (*ptr == delimeter) {
str[i] = '\0';
if (localVerbose && id->verbose)
printf ("\n[%s:%d] - complete parsing sub string '%s' ( index %d ) ", __FILE__, __LINE__, str, n); fflush(stdout);
array[n] = (char *) malloc ((strlen(str) + 1)* sizeof(char));
strcpy(array[n], str);
n++;
i = 0;
} else {
str[i] = *ptr;
i++;
}
ptr++;
}
// get the last string
str[i] = '\0';
if (localVerbose && id->verbose)
printf ("\n[%s:%d] - complete parsing sub string '%s' ( index %d ) ", __FILE__, __LINE__, str, n); fflush(stdout);
array[n] = (char *) malloc ((strlen(str)+1) * sizeof(char));
strcpy(array[n], str);
*length = n+1;
return array;
}
void my_string_printArray(char **a, int length) {
int i;
printf ("\n[%s:%d] - ", __FILE__, __LINE__);
printf ("\nlength %d", length);
for (i = 1; i < length; i++) {
printf ("\n%d\t'%s'", i, a[i]);
}
printf ("\n");
}
void my_string_printArrayHorizontal(char **a, int length) {
int i;
printf ("\n[%s:%d] - ", __FILE__, __LINE__);
printf ("\n");
for (i = 1; i < length; i++) {
printf ("%s\t", a[i]);
}
printf ("\n");
}
/**
convert to upper case
**/
void my_string_toupper(char *ptr) {
char *p = ptr;
while (*p != '\0') {
*p = toupper(*p);
p++;
}
}
void my_string_removeTrailingSpaces(char *ptr) {
}