-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
96 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,45 +1,52 @@ | ||
#include <stdlib.h> | ||
#include "main.h" | ||
#include <stdlib.h> | ||
|
||
/** | ||
* *argstostr - concatenates all the arguments of the program | ||
* @ac: number of arguments | ||
* @av: array of arguments | ||
* argstostr - concatenates all the arguments of a program. | ||
* @ac: argument count. | ||
* @av: argument vector. | ||
* | ||
* Return: Pointer to the new string (Success), NULL (Error) | ||
* Return: pointer of an array of char | ||
*/ | ||
char *argstostr(int ac, char **av) | ||
{ | ||
int i, j, k, len; | ||
char *str; | ||
char *aout; | ||
int c, i, j, ia; | ||
|
||
if (ac == 0 || av == NULL) | ||
if (ac == 0) | ||
return (NULL); | ||
|
||
for (i = 0; i < ac; i++) | ||
for (c = i = 0; i < ac; i++) | ||
{ | ||
if (av[i] == NULL) | ||
return (NULL); | ||
|
||
for (j = 0; av[i][j] != '\0'; j++) | ||
len++; | ||
len++; | ||
c++; | ||
c++; | ||
} | ||
|
||
str = malloc(sizeof(char) * (len + 1)); | ||
aout = malloc((c + 1) * sizeof(char)); | ||
|
||
if (str == NULL) | ||
if (aout == NULL) | ||
{ | ||
free(aout); | ||
return (NULL); | ||
} | ||
|
||
k = 0; | ||
|
||
for (i = 0; i < ac; i++) | ||
for (i = j = ia = 0; ia < c; j++, ia++) | ||
{ | ||
for (j = 0; av[i][j] != '\0'; j++) | ||
if (av[i][j] == '\0') | ||
{ | ||
str[k] = av[i][j]; | ||
k++; | ||
aout[ia] = '\n'; | ||
i++; | ||
ia++; | ||
j = 0; | ||
} | ||
str[k] = '\n'; | ||
k++; | ||
if (ia < c - 1) | ||
aout[ia] = av[i][j]; | ||
} | ||
aout[ia] = '\0'; | ||
|
||
return (str); | ||
return (aout); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
#include "main.h" | ||
#include <stdlib.h> | ||
|
||
/** | ||
* ch_free_grid - frees a 2 dimensional array. | ||
* @grid: multidimensional array of char. | ||
* @height: height of the array. | ||
* | ||
* Return: no return | ||
*/ | ||
void ch_free_grid(char **grid, unsigned int height) | ||
{ | ||
if (grid != NULL && height != 0) | ||
{ | ||
for (; height > 0; height--) | ||
free(grid[height]); | ||
free(grid[height]); | ||
free(grid); | ||
} | ||
} | ||
|
||
/** | ||
* strtow - splits a string into words. | ||
* @str: string. | ||
* | ||
* Return: pointer of an array of integers | ||
*/ | ||
char **strtow(char *str) | ||
{ | ||
char **aout; | ||
unsigned int c, height, i, j, a1; | ||
|
||
if (str == NULL || *str == '\0') | ||
return (NULL); | ||
for (c = height = 0; str[c] != '\0'; c++) | ||
if (str[c] != ' ' && (str[c + 1] == ' ' || str[c + 1] == '\0')) | ||
height++; | ||
aout = malloc((height + 1) * sizeof(char *)); | ||
if (aout == NULL || height == 0) | ||
{ | ||
free(aout); | ||
return (NULL); | ||
} | ||
for (i = a1 = 0; i < height; i++) | ||
{ | ||
for (c = a1; str[c] != '\0'; c++) | ||
{ | ||
if (str[c] == ' ') | ||
a1++; | ||
if (str[c] != ' ' && (str[c + 1] == ' ' || str[c + 1] == '\0')) | ||
{ | ||
aout[i] = malloc((c - a1 + 2) * sizeof(char)); | ||
if (aout[i] == NULL) | ||
{ | ||
ch_free_grid(aout, i); | ||
return (NULL); | ||
} | ||
break; | ||
} | ||
} | ||
for (j = 0; a1 <= c; a1++, j++) | ||
aout[i][j] = str[a1]; | ||
aout[i][j] = '\0'; | ||
} | ||
aout[i] = NULL; | ||
return (aout); | ||
} |