Adding spaces at null of char array

Hey everyone,
I have a char array of supposing 20.
like
char array [20];
When I enter "Animation is good", it has 17 letters of course. Extra 3 spaces are neglected by C++. I wonder if somehow these last 3 null arrays filled with spaces.


I hope you would understand my question.
Fill the array with spaces first.
std::fill(std::begin(array), std::end(array), ' ');
When I enter "Animation is good", it has 17 letters of course. Extra 3 spaces are neglected by C++. I wonder if somehow these last 3 null arrays filled with spaces.


You using scanf or cin to read input? Answer depends on which one.
@Hassibayub has confused other posters by using the word “spaces”.

As far as C is concerned, a string ends when the character value is zero. It doesn’t actually care how big the array is.

Character values after the terminating null (zero) character value are unimportant, and often wind up being rather random.

In your case, your array looks like:

 
  char array[20] = { 'A','n','i','m','a','t','i','o','n',' ','i','s',' ','g','o','o','d', 0, ?, ? };

The 18th character (at array[17]) is a zero, meaning “end of string”. The remaining two characters can have any value.

Hope this helps.
Last edited on
@mbozzi can you elaborate your code?

@elohssa I'm using gets() for inputting an char array..

@Duthomhas You answer as far as I understand is add spaces to your char array by yourself.
But I want that last 3 character is somehow
like for Animation is good

char array[20] = { 'A','n','i','m','a','t','i','o','n',' ','i','s',' ','g','o','o','d', 0, ?, ? };
last 3 should be filled with 3 spaces..
And if user enters any string like Animation
char array should be
char array[20] = { 'A','n','i','m','a','t','i','o','n',' ',' ',' ',' ',' ',' ',' ',' ',' ' ,' ' , ' ' };
last null indexes should filled spaces automatically...
I believe you mean
char array[20] = { 'A','n','i','m','a','t','i','o','n',' ',' ',' ',' ',' ',' ',' ',' ',' ' ,' ' , 0 };
or, if you want 20 actual, printable characters, it would need to be
char array[21] = { 'A','n','i','m','a','t','i','o','n',' ',' ',' ',' ',' ',' ',' ',' ',' ' ,' ' ,' ', 0 };

Regardless... why do you want to do this? There might be a better solution.

If you need a constant minimum of 20 characters of spacing, check out formatting functionality like std::left, std::right std::setw.
http://en.cppreference.com/w/cpp/io/manip/left
http://www.cplusplus.com/reference/iomanip/setw/

___________________________________________

Here's something you could use,
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
int main()
{
    const int size = 20;
    char array[size] = {}; // Initialize all characters to null

    // get input (gets is not safe, so I don't suggest using it)
    std::cin.getline(array, size);

    // Changes all remaining null characters (except for the last one) into spaces
    for (int i = 0; i < size-1; i++)
    {
       if (array[i] == '\0')
            array[i] = ' ';
    }
    
    std::cout << "-->" << array << "<--" << std::endl;
}

Last edited on
An array of characters is not necessarily a C-string. It can also be a fixed-size buffer, containing data that is not null-terminated.

1
2
3
4
5
6
7
char array[20];
// entirely fill array with spaces
std::fill(std::begin(array), std::end(array), ' '); 

// partially fill array with some data, overwriting the first size spaces
std::ifstream str{"/dev/urandom", std::ios::bin};
str.read(array, size);


After the snippet, assuming success, the contents of array is the following:

{ x, x, x, x, x, x, x, x, x, x, ' ', ' ', ' ', ' ', ' ' ,' ', ' ', ' ', ' ', ' ', }

Consider std::array instead.
Last edited on
Hey @Ganado,
Actually, I want to format my output look aligned like MS excel sheet do.
for example:

ID                 Name              Department                    Issue date
0013               Haseeb             Avionics Engg.                 22|12|2018              
0014               Zain               Computer Sc.                   02|04|2018
003                Usama              Business                        09|08|2018


Hope You'll get my point...
Last edited on
This could work. There is probably a way to condense it more, but I wasn't sure.
I took the liberty of assuming the IDs and dates are ints internally.
If they are simply strings, ex "003", "09|08|2018", then it's even easier, just follow the pattern I did for "ID Name Department issue date"

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
60
61
62
63
64
65
66
#include <iostream>
#include <string>
#include <iomanip>

struct Badge {
    int id;
    std::string name;
    std::string department;
    int issue_date_day;
    int issue_date_month;
    int issue_date_year;
};

int main()
{
    Badge badges[3] = {
        {13, "Haseeb", "Avionics Engg.", 22, 12, 2018},
        {14, "Zain",   "Computer Sc.",    2,  4, 2018},
        {3,  "Usama",  "Business",        9,  8, 2018}
    };
    
    const int id_width = 4;
    const int cell_width = 20;
    
    // print titles (Doing this just with strings is easy,
    // it gets a bit harder if you need leading zeroes + spacing)
    std::cout << std::left << std::setw(cell_width) <<
      "ID" << std::setw(cell_width) <<
      "Name" << std::setw(cell_width) <<
      "Department" << std::setw(cell_width) <<
      "Issue date" << std::endl;
    
    for (int i = 0; i < 3; i++)
    {
        // format for id with leading zeroes
        std::cout << std::right << std::setw(id_width) << std::setfill('0'); 
        std::cout << badges[i].id;
        
        // fill the remaining spaces before name
        std::cout << std::setw(cell_width-id_width) << std::setfill(' ');
        std::cout << " ";
        
        // format for name and department
        std::cout << std::left;
        std::cout << std::setw(cell_width) << badges[i].name;
        std::cout << std::setw(cell_width) << badges[i].department;

        //
        // format for date XX|YY|ZZ with leading zeroes
        //
        std::cout << std::right << std::setw(2) << std::setfill('0');
        std::cout << badges[i].issue_date_day;
        
        std::cout << std::setw(1) << "|";
        
        std::cout << std::right << std::setw(2);
        std::cout << badges[i].issue_date_month;
        
        std::cout << std::setw(1) << "|";
        
        std::cout << std::right << std::setw(2);
        std::cout << badges[i].issue_date_year;
        
        std::cout << std::endl;
    }
}


1
2
3
4
ID                  Name                Department          Issue date          
0013                Haseeb              Avionics Engg.      22|12|2018
0014                Zain                Computer Sc.        02|04|2018
0003                Usama               Business            09|08|2018
Last edited on
Topic archived. No new replies allowed.