求助怎么输出指定长度
#include <stdio.h>#define N 15
#define M ((int)(sizeof(b)/sizeof(b[0])))
int main(void)
{
char a[N],ch,b[10]={0};
int i,j=0,k;
printf("Enter a first and last name:");
k=0;
do { //读取名
i =getchar();
a[k] =i;
k++;
}while(i!= ' ');
j=0;
while((ch=getchar())!='\n') //读取姓氏
{
b[j]=ch;
j++;
}
printf("You enered the name:");
for(j=0;j<M;j++) //输出姓氏,此处的M我想指定循环的次数,但是不能实现,请请教。
{
printf("%c",b[j]);
}
printf(",%c.",a[0]);
printf("%d",M);
return 0;
}
输出示例:
Enter a first and last name:Lloyd Fosdick
You enterd the name: Fosdick,L.