-
Notifications
You must be signed in to change notification settings - Fork 486
Expand file tree
/
Copy path5-argstostr.c
More file actions
50 lines (39 loc) · 955 Bytes
/
5-argstostr.c
File metadata and controls
50 lines (39 loc) · 955 Bytes
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
42
43
44
45
46
47
48
49
50
/*
* File: 5-argstostr.c
* Auth: Brennan D Baraban
*/
#include "holberton.h"
#include <stdlib.h>
/**
* argstostr - Concatenates all arguments of the program into a string;
* arguments are separated by a new line in the string.
* @ac: The number of arguments passed to the program.
* @av: An array of pointers to the arguments.
*
* Return: If ac == 0, av == NULL, or the function fails - NULL.
* Otherwise - a pointer to the new string.
*/
char *argstostr(int ac, char **av)
{
char *str;
int arg, byte, index, size = ac;
if (ac == 0 || av == NULL)
return (NULL);
for (arg = 0; arg < ac; arg++)
{
for (byte = 0; av[arg][byte]; byte++)
size++;
}
str = malloc(sizeof(char) * size + 1);
if (str == NULL)
return (NULL);
index = 0;
for (arg = 0; arg < ac; arg++)
{
for (byte = 0; av[arg][byte]; byte++)
str[index++] = av[arg][byte];
str[index++] = '\n';
}
str[size] = '\0';
return (str);
}