summaryrefslogtreecommitdiff
path: root/wk4/lect/copy.c
blob: fe532511cbcc66506f53fccc607c9317bf034fe7 (plain)
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
#include <cs50.h>
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(void)
{
    char *s = get_string("s: ");
    if (s == NULL)
    {
        return 1;
    }

    char *t = malloc(strlen(s) + 1); // allocates a piece of memory for the new string with the same length as s
    if (t == NULL)
    {
        return 1;
    }

    strcpy(t,s); // copies a string s to a new string t

    for (int i = 0, n = strlen(s); i <= n; i++)
    {
        t[i] = s[i];
    }

    if(strlen(t) > 0)
    {
        t[0] = toupper(t[0]);
    }

    printf("%p\n", s);
    printf("%p\n", t);

    free(t); // frees the memory allocated by malloc
    return 0;
}