Home > Back-end >  How can I remove the whitespace at the end of each line
How can I remove the whitespace at the end of each line

Time:09-26

void printSudoku9x9(int grid[9][9]) {
    for (int y = 0; y < 9; y  ){
        for (int x = 0; x < 9; x  )
            cout << grid[y][x] <<' ';
        cout << endl;
    }
}

CodePudding user response:

Instead of printing a space after each character and dealing with the end, print a space before each character printed except for when (x==0).

void printSudoku9x9(int grid[9][9]) {
    for (int y = 0; y < 9; y  ){
        for (int x = 0; x < 9; x  ) {
            char leading = (x == 0) ? "" : " ";
            cout << leading << grid[y][x];
        }
        cout << endl;
    }
}

CodePudding user response:

Just remove it with "\b":

void printSudoku9x9(int grid[9][9]) {
    for (int y = 0; y < 9; y  ){
        for (int x = 0; x < 9; x  )
            cout << grid[y][x] <<' ';
        cout << "\b" << endl;
    }
}
  •  Tags:  
  • c
  • Related