-
Notifications
You must be signed in to change notification settings - Fork 1
/
append.c
47 lines (44 loc) · 1.03 KB
/
append.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
45
46
47
/* CITS2002 Project 2018
* Names: Bruce How, Vincent Tian
* Student numbers: 22242664, 22262122
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "append.h"
/**
* Dynamically allocates memory to word to accomodate for an additional
* character and null byte symbol to be added
*/
char *append(char *word, char ch) {
if(word == NULL) {
word = malloc(2 * sizeof(char));
if(word == NULL) {
perror(__func__);
exit(EXIT_FAILURE);
}
word[0] = ch;
word[1] = '\0';
} else {
int len = strlen(word);
word = realloc(word, (len+2) * sizeof(char));
if(word == NULL) {
perror(__func__);
exit(EXIT_FAILURE);
}
word[len] = ch;
word[len+1] = '\0';
}
return word;
}
/**
* Utilises append() to add each character from add to word
*/
char *appendStr(char *word, char *add) {
while(*add != '\0') {
word = append(word, *add);
add++;
}
return word;
}