I am trying to split strings into words. Can anyone help me on how to implement it without using strtok
and another function other than main
?
void main()
{
int i;
int myargc = 1;
char *myargv[256];
char buff[100];
int len = 0;
char string[256];
int j = 0;
printf("Enter text: ");
gets(buff);
for(i = 0; buff[i] != '\0'; i++){
len++;
}
for(i = 0; i < len; i++)
{
if(buff[i]!=' ' && buff[i+1]==' ')
{
myargc++;
}
}
printf("myargc %d\n",myargc);
**for(i = 0; i < len; i++){
if(buff[i] != ' '){
string[j++] = buff[i];
}
if(buff[i] != ' ' && buff[i] == ' '){
string[j++] = '\0';
j = 0;
}
if(buff[i] == '\0'){
break;
}
}**
for(i = 0; i < myargc - 1; i++){
myargv[i] = string;
printf("argv[%d]\t%s\n", i, myargv[i]);
}
}
When I entered "a b c" for example, my output looked like this:
myargc 3
argv[0] abc
argv[1] abc
argv[2] abc