You are on page 1of 1

/* strtok example */

#include <stdio.h>
#include <string.h>

int main ()
{
char str[] ="- This, a sample string.";
char * pch;
printf ("Splitting string \"%s\" into tokens:\n",str);
pch = strtok (str," ,.-");
while (pch != NULL)
{
printf ("%s\n",pch);
pch = strtok (NULL, " ,.-");
}
return 0;
}

Output:
Splitting string "- This, a sample string." into tokens:
This
a
sample
string

#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <string.h>
#include <stdlib.h>
int main(int argc,char** argv)
{

char * Array_arg[50];
char command_line[200];//user input
gets(command_line);
char tmp[50];
char * subcommand=strtok(command_line," "); //divides the string according
to the spaces
int i=0;
while (subcommand!= NULL)//insert to array
{
Array_arg[i]=subcommand;
subcommand=strtok(NULL," ");
i++;
}
Array_arg[i]='\0';

execvp(Array_arg[0],Array_arg); //execution of cmd


printf(" %s :command not found \n",Array_arg[0]); //if arrived to this point
there was an error in the execution.
}

You might also like