![](/img/trans.png)
[英]How do I scan in an N number of strings (using structs) and then output that list of strings in reverse?
[英]How do I scan a sentence into an array of strings?
我有一个文本文件,其中出现两种类型的格式。 扫描完第一种格式类型并将其放入数组后,遇到第二种格式:
$$$$$$$
I have a big tree full of money
如何忽略$$$$$$$行并将句子扫描到一个新数组中,其中每个元素都是句子中的一个单词:
`char sentence[MAX_SENTENCE_LENGTH]`
我尝试使用:
for (i = 0; i < MAX_SENTENCE_LENGTH; i++){
if (scanf(" $$$$$$$ %s", sentence[i]) !=1){
break;
但这并不能说明$$$$$$$
仅在句子之前出现一次的事实。 它还返回格式化警告:
format specifies type 'char *' but the argument has type 'int' [-Wformat]
程序中的主要问题是您希望读取一个字符串数组,但是您的数据结构是单个字符串。 您的编译器实际上应该已经警告过您。 然后,用扫描第一元件" $$$$$$$ %s"
格式,是下述的与"%s"
只:
const int MAX_SENTENCE_LENGTH = 30;
const int MAX_NR_OF_SENTENCES = 30;
char sentence[MAX_NR_OF_SENTENCES][MAX_SENTENCE_LENGTH];
int i=0;
if (scanf(" $$$$$$$ %29s", sentence[i]) == 1) {
i++;
while (i < MAX_NR_OF_SENTENCES) {
if (scanf("%29s", sentence[i]) !=1){
break;
}
i++;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.