Help in char array

Why the length of array y isn't 27?

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
#include <iostream>
#include "util.h" // display()
#include "str.h" // strFill(), strLength

using namespace std;

void letterToAlpha(char* letter, char* alphabet, char* voidalphabet){
    while(*alphabet){
        if(*letter == *alphabet){
            *voidalphabet = *alphabet;
        }
        alphabet++;
        voidalphabet++;
    }
}

int main(int argc, char *argv[]) {

    char x[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    char y[strLength(x)];
    char c = 'C';

    y[strLength(x) - 1] = '\0';

    strFill(y, '_');

    letterToAlpha(&c, x, y);

    display("\nFull alpha: ");
    display(x);
    display("\n\nVoid alpha: ");
    display(y);
    display("\n\nFull alpha length: ");
    display(strLength(x));
    display("\n\nVoid alpha length: ");
    display(strLength(y));

    return 0;
}
Last edited on
closed account (EwCjE3v7)
The size of array can't be changed, its 25 because there are 25 letters that you have put into x.
I'm not trying to change the size of array y, x is 27 chars long, I'm just declaring y with the size of x (27). The output of my program give me this:

Full alpha: ABCDEFGHIJKLMNOPQRSTUVWXYZ

Void alpha: __C____

Full alpha length: 27

Void alpha length: 8

Why is this happening? Why the lenth of y is 8 and not 27?
Last edited on
strFill probably detects a NULL halfway and stops filling out '_" at that location . Use memset instead or another method that ignores inadvertent NULLs
The error is most likely in the strFill() file.
without seeing the code i can't help.
Yes, maybe YokoTsuno is right, so I've changed my code to this and now it works fine. Thanks a lot =D

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
#include <iostream>
#include "util.h" // display()
#include "str.h" // strFill(), strLength

using namespace std;

void letterToAlpha(char* letter, char* alphabet, char* voidalphabet){
    while(*alphabet){
        if(*letter == *alphabet){
            *voidalphabet = *alphabet;
        }
        alphabet++;
        voidalphabet++;
    }
}

int main(int argc, char *argv[]) {

    char x[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    char y[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    char c = 'C';

    strFill(y, '_');

    letterToAlpha(&c, x, y);

    display("\nFull alpha: ");
    display(x);
    display("\n\nVoid alpha: ");
    display(y);
    display("\n\nFull alpha length: ");
    display(strLength(x));
    display("\n\nVoid alpha length: ");
    display(strLength(y));

    return 0;
}
Topic archived. No new replies allowed.