c语言,怎样讲整形的数字赋值给一个字符串数组?

来源:百度知道 编辑:UC知道 时间:2024/05/29 03:58:08
看看这个函数:
void toString(){
char chessBoard[200]=" 0 1 2 3 4 5 6 7";
int row;
int column;
for(row=0;row<8;row++){
strcat(chessBoard,"\n");
strcat(chessBoard,"?");
for(column=0;column<8;column++){
if(squares[row][column]){
strcat(chessBoard," #");
}else{
strcat(chessBoard," .");
}
}
}
puts(chessBoard);
}
我想做成这个样子:
0 1 2 3 4 5 6 7
0 . . . . . . . .
1 . . . . . . . .
2 . . . . . . . .
3 . . . . . . . .
4 . . . . . . . .
5 . . . . . . . .
6 . . . . . . . .
7 . . . . . . . .

#include<stdio.h>
#include<string.h>

void main()
{
char chessBoard[200]=" 0 1 2 3 4 5 6 7";
char squares[100][100]={0};
char a[2]={0};
int row,column;

for(row=0;row<8;row++)
{
strcat(chessBoard,"\n");
//strcat(chessBoard,"?");
a[0]='0'+row;
strcat(chessBoard,a);
for(column=0;column<8;column++)
{
if(squares[row][column])
{
strcat(chessBoard," #");
}
else
{
strcat(chessBoard," .");
}
}
}
puts(chessBoard);
}

输出:
. 0 1 2 3 4 5 6 7
0 . . . . . . . .
1 . . . . . . . .
2 . . . . . . . .
3 . . . . . . . .
4 . . . . . . . .
5 . . . . . . . .
6 . . . . . . . .
7 . . . . . . . .

可以用sprintf
示例:
char a[100];
int b =312348;<