-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstring.c
30 lines (27 loc) · 822 Bytes
/
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
#include "string.h"
int freadline( FILE * fp, char * line ) {
int i = 0;
while ( ( line[ i++ ] = fgetc( fp ) ) != EOF ) {
if ( line[ i - 1 ] == '\n' ) {
line[ i - 1 ] = '\0';
return 0;
}
}
return EOF;
}
void string_array_ncopy( char ** to, char ** from, int n ) {
int i;
for ( i = 0; i < n; ++i ) {
to[ i ] = (char*)malloc( strlen( from[ i ] ) * sizeof( char ) );
strcpy( to[ i ], from[ i ] );
}
}
int string_split( char * s, char * delims, char ** parts ) {
int count = 0;
char * result = strtok( s, delims );
do {
parts[ count++ ] = (char*)malloc( strlen( result ) * sizeof( char ) );
strcpy( parts[ count - 1 ], result );
} while ( ( result = strtok( NULL, delims ) ) != NULL );
return count;
}