-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path5-argstostr.c
41 lines (37 loc) · 821 Bytes
/
5-argstostr.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <stdlib.h>
/**
*argstostr - concatenates all the arguments of your program.
*@ac: argument count
*@av: arguments
*Return: char ptr string or NULL if malloc fails or ac or av is 0
*/
char *argstostr(int ac, char **av)
{
char *string;
int i, j;
int k = 0;/*index through string char pointer*/
int s = 0;/*count all the characters in arguments*/
if (ac == 0 || av == NULL)
return (NULL);
/*count all the chars in arguments*/
for (i = 0; i < ac; i++)
{
for (j = 0; av[i][j]; j++, s++)
;
s++;
}
s++;
string = malloc(s * sizeof(char));
if (string == NULL)
return (NULL);
/*add all charcters from args into char ptr array*/
for (i = 0; i < ac; i++)
{
for (j = 0; av[i][j]; j++, k++)
string[k] = av[i][j];
string[k] = '\n';
k++;
}
string[k] = '\0';
return (string);
}