-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrevString.c
44 lines (39 loc) · 1.16 KB
/
revString.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
43
44
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "main.h"
/**
* reverseString - Adds "0x" prefix to a given hexadecimal string.
*
* @str: The input hexadecimal string to which the "0x" prefix will be added.
*
* Description:
* This function takes an input hexadecimal string and returns a new string with "0x" added as a
* prefix to the original string. The resulting string is null-terminated. The function allocates
* memory for the new string and ensures it can accommodate the original string, "0x" prefix, and
* a null terminator.
*
* Return: A dynamically allocated string containing the original hexadecimal string with the "0x"
* prefix added, or NULL if the input string is NULL or on memory allocation failure.
*/
char *reverseString(const char *str)
{
int length = strlen(str);
int start;
int end;
char *reversed = (char *)malloc(length + 1);
if (reversed == NULL)
{
return (NULL);
}
start = 0;
end = length - 1;
while (start < length)
{
reversed[start] = str[end];
start++;
end--;
}
reversed[length] = '\0';
return (reversed);
}