Zappy - Year end project 2
This is a project that Epitech asked us to create in order to allow us to reveiw the notions of the current year.
Loading...
Searching...
No Matches
array_to_str.c
Go to the documentation of this file.
1/*
2** EPITECH PROJECT, 2023
3** my_zappy
4** File description:
5** array_to_str.c
6*/
7
8#include <string.h>
9#include <stdlib.h>
10
11static size_t get_nb_word(char *str)
12{
13 size_t nb_word = 1;
14
15 for (int i = 0; str[i] != '\0'; ++i) {
16 if (str[i] == ' ')
17 ++nb_word;
18 }
19 return (nb_word);
20}
21
22static void fill_str(int ac, const char **av, char *str,
23 int *quoted_string_index)
24{
25 int quoted_count = 0;
26
27 for (int i = 0; i < ac; ++i) {
28 if (quoted_string_index[quoted_count] == i)
29 strcat(str, " ");
30 strcat(str, av[i]);
31 if (quoted_string_index[quoted_count] == i) {
32 strcat(str, " ");
33 ++quoted_count;
34 }
35 }
36}
37
38char *array_to_str(int ac, const char **av)
39{
40 char *str;
41 size_t quoted_string = 0;
42 size_t len = 0;
43 int quoted_string_index[ac];
44
45 memset(quoted_string_index, -1, sizeof(int) * 4);
46 for (int i = 0; i < ac; ++i) {
47 if (get_nb_word((char *)av[i]) > 1) {
48 quoted_string_index[quoted_string] = i;
49 ++quoted_string;
50 }
51 len += strlen(av[i]);
52 }
53 len += quoted_string * 2;
54 str = malloc(sizeof(char) * (len + 1));
55 if (str == NULL)
56 return (NULL);
57 str[0] = '\0';
58 fill_str(ac, av, str, quoted_string_index);
59 return (str);
60}
char * array_to_str(int ac, const char **av)
Turn an array of string into a simple string.