-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcharToString.c
42 lines (37 loc) · 1022 Bytes
/
charToString.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
42
#include <stdio.h>
#include <stdlib.h>
#include "main.h"
/**
* charToString - Converts a character to a
* dynamically allocated string.
*
* @character: The character to be converted to a string.
*
* Description:
* This function takes a single character and
* converts it into a dynamically
* allocated string. The resulting string
* contains the character followed by
* a null-terminator.
*
* The function allocates memory for the
* character and the null-terminator,
* and sets the character at the first
* position in the string.
*
* If memory allocation fails, the function returns NULL.
*
* Return: A dynamically allocated string
* containing the character, or NULL on failure.
*/
char *charToString(char character)
{
char *string_pionter = (char *)malloc(2);
if (string_pionter == NULL)
{
return (NULL); /* Memory allocation failed */
}
string_pionter[0] = character; /* Set the character */
string_pionter[1] = '\0'; /* Null-terminate the string */
return (string_pionter);
}