I am mostly concerned with the find and separation function. When I reused them in another program, I found that they don't always work and sometimes show segmentation fault. I tried every possible thing I could do, but nothing worked.
#include <stdio.h>
#include<stdlib.h>
#include <string.h>
int find(char *argument)
{
int count = 0;
int i = 0;
int state = 0;
while (argument[i] != '\0')
{
if (argument[i] == ' ' || argument[i + 1] == '\0')
{
state = 0;
}
else if (state == 0)
{
state = 1;
count++;
}
i++;
}
return count;
}
char **sepration(int args, char str[])
{
int i = 0;
char **argv = (char **)malloc(args);
int k = 0;
int len = strlen(str);
while (i < len)
{
if (str[i] != ' ')
{
char *st = &str[i];
argv[k] = st;
k++;
while (str[i] != ' ' && str[i] != '\0' && i < len)
{
i++;
}
str[i] = '\0';
}
i++;
}
return argv;
}
int main()
{
char argument[100];
scanf("%[^\n]s", &argument);
//finds the no of words in the given argument
int args = find(argument);
char **argv = (char **)malloc(args + 1);
//stores the word separately in **char pointer array
argv = sepration(args, argument);
//adds the arguments whichs are numbers
add(args, argv);
}