strcpy?

how do i copy a string for my code.
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
48
49
50
51
52
53
54
55
56
57
58
59
#include <stdlib.h>
#include <strings.h>
#define initial_size 5

void reverseChar(char* str);
char* IncreaseArray(char *oldArray, int OldSize, int NewSize);


int main(int argc, const char * argv[]) {
    
    char *str1 = NULL;
    char *str2 = str1;  //my attempt
    char c;
    int CurSize = 0;
    int ArraySize = 0;
    printf("Please Enter a String: ");
    do{
        scanf("%c", &c);
        if (c != 10 && c != 13){
            if (CurSize >= ArraySize){
                str1 = IncreaseArray(str1, ArraySize, ArraySize + initial_size);
                ArraySize = ArraySize + initial_size;
            }
            str1[CurSize] = c;
        }
        else
            str1[CurSize] = '\0';
        CurSize++;
        
    } while (c != 10 && c != 13);
    
    reverseChar(str1);
    printf("string Entered: %s\n", str2);  //my attempt
    printf("Reversed String: %s\n", str1);
    
    
    
    
    return 0;
}

void reverseChar(char* str)
{
    for (int i = 0; i<strlen(str) / 2; i++)
    {
        char temp = str[i];
        str[i] = str[strlen(str)-i - 1];
        str[strlen(str)-i - 1] = temp;
    }
}

 char* IncreaseArray(char *oldArray, int OldSize, int NewSize){
    char *NewArray = NULL;
    NewArray = (char*)malloc(NewSize * sizeof(char)); //allocates memory
    for (int i = 0; i < OldSize; i++) //copies old array
        NewArray[i] = oldArray[i];
    free(oldArray); //releases old array's memory
    return NewArray; //return new array
}
Last edited on
Topic archived. No new replies allowed.