想实现输入多组带空格的字符串并将每组字符串存入二维数组的元素中
目前代码如下
#include <stdio.h>
#include <string.h>
int main()
{
int t;//接下来将要输入t组字符串
scanf("%d",&t);
int i,j;
char trans[1000];
char str[1000][1000];//定义一个二维字符数组
for(i = 0;i < t;i ++){
scanf("%[^\n]%*c",str[i]); //实现能输入空格?
}
for(j = 0;j < t;j ++){
int len = strlen(str[j]);
stpcpy(trans,str[j]);
printf("%d\n",trans[trans[len-1]+1]);
}
return 0;
}
但问题在于没办法输入,输入t之后,回车会直接结束,并输出三个0
这串读入的代码%[^\n]%*c
在实现对单一的一组字符串读入一个一维字符数组是有效的,这次是实验性质地运用在二维字符数组读入多组数据。请问是用法不对吗?有更好的解决方案吗?
gets()