Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
22
Task/Tokenize-a-string/C/tokenize-a-string-1.c
Normal file
22
Task/Tokenize-a-string/C/tokenize-a-string-1.c
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
#include<string.h>
|
||||
#include<stdio.h>
|
||||
#include<stdlib.h>
|
||||
|
||||
int main(void)
|
||||
{
|
||||
char *a[5];
|
||||
const char *s="Hello,How,Are,You,Today";
|
||||
int n=0, nn;
|
||||
|
||||
char *ds=strdup(s);
|
||||
|
||||
a[n]=strtok(ds, ",");
|
||||
while(a[n] && n<4) a[++n]=strtok(NULL, ",");
|
||||
|
||||
for(nn=0; nn<=n; ++nn) printf("%s.", a[nn]);
|
||||
putchar('\n');
|
||||
|
||||
free(ds);
|
||||
|
||||
return 0;
|
||||
}
|
||||
26
Task/Tokenize-a-string/C/tokenize-a-string-2.c
Normal file
26
Task/Tokenize-a-string/C/tokenize-a-string-2.c
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
#include<stdio.h>
|
||||
|
||||
typedef void (*callbackfunc)(const char *);
|
||||
|
||||
void doprint(const char *s) {
|
||||
printf("%s.", s);
|
||||
}
|
||||
|
||||
void tokenize(char *s, char delim, callbackfunc cb) {
|
||||
char *olds = s;
|
||||
char olddelim = delim;
|
||||
while(olddelim && *s) {
|
||||
while(*s && (delim != *s)) s++;
|
||||
*s ^= olddelim = *s; // olddelim = *s; *s = 0;
|
||||
cb(olds);
|
||||
*s++ ^= olddelim; // *s = olddelim; s++;
|
||||
olds = s;
|
||||
}
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
char array[] = "Hello,How,Are,You,Today";
|
||||
tokenize(array, ',', doprint);
|
||||
return 0;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue