Basic and Advance C Question:
Download Questions PDF

How can I split up a string into whitespace-separated fields?

Answers:

Answer #1
How can I split up a string into whitespace-separated fields? How can I duplicate the process by which main() is handed argc and argv?

The only Standard function available for this kind of ``tokenizing'' is strtok, although it can be tricky to use and it may not do everything you want it to. (For instance, it does not handle quoting.) Here is a usage example, which simply prints each field as it's extracted:
#include <stdio.h>
#include <string.h>
char string[] = "this is a test"; /* not char *;

char *p;
for(p = strtok(string, " tn"); p != NULL;
p = strtok(NULL, " tn"))
printf(""%s"n", p);

As an alternative, here is a routine I use for building an argv all at once:
#include <ctype.h>
int makeargv(char *string, char *argv[], int argvsize)
{
char *p = string;
int i;
int argc = 0;

for(i = 0; i < argvsize; i++) {
/* skip leading whitespace */
while(isspace(*p))
p++;

if(*p != '')
argv[argc++] = p;
else {
argv[argc] = 0;
break;
}

/* scan over arg */
while(*p != '' && !isspace(*p))
p++;
/* terminate arg: */
if(*p != '' && i < argvsize-1)
*p++ = '';
}

return argc;
}

Calling makeargv is straightforward:
char *av[10];
int i, ac = makeargv(string, av, 10);
for(i = 0; i < ac; i++)
printf(""%s"n", av[i]);

Answer #2
If you want each separator character to be significant, for instance if you want two tabs in a row to indicate an omitted field, it's probably more straightforward to use strchr:
#include <stdio.h>
#include <string.h>

char string[] = "thisthasttmissingtfield";
char *p = string;

while(1) { /* break in middle */
char *p2 = strchr(p, 't');
if(p2 != NULL)
*p2 = '';
printf(""%s"n", p);
if(p2 == NULL)
break;
p = p2 + 1;
}

All the code fragments presented here modify the input string, by inserting 's to terminate each field (meaning that the string must be writable;
If you'll need the original string later, make a copy before breaking it up.

Download C Programming Interview Questions And Answers PDF

Previous QuestionNext Question
How can I get the current date or time of day in a C program?Why do some versions of toupper act strangely if given an upper-case letter?