Help in char array

Nov 2, 2014 at 12:28am
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 Nov 2, 2014 at 4:12am
Nov 2, 2014 at 12:36am
closed account (EwCjE3v7)
The size of array can't be changed, its 25 because there are 25 letters that you have put into x.
Nov 2, 2014 at 12:45am
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 Nov 2, 2014 at 4:14am
Nov 2, 2014 at 5:26am
strFill probably detects a NULL halfway and stops filling out '_" at that location . Use memset instead or another method that ignores inadvertent NULLs
Nov 2, 2014 at 1:39pm
The error is most likely in the strFill() file.
without seeing the code i can't help.
Nov 2, 2014 at 3:53pm
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.