我想知道在 c 中存储一系列句子的最佳方法是什么。在 python 中这很容易,但我在 C 中找不到类似的解决方案。
例如:
sent1="this is sentence 1"
sent2="this is sent 2"
列出 list
list = [sent1;sent2];
这个目标是打印一个随机句子
print list[random_number]
最佳答案
如果句子像您的示例中那样是静态的,您可以简单地执行此操作。
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main()
{
time_t t;
const char * sentences[] = {"first sentence", "second sentence"};
srand(time(&t));
printf("%s\n", sentences[rand() % 2]);
return 0;
}
马丛指的是 K&R 的《C 编程语言》:https://hassanolity.files.wordpress.com/2013/11/the_c_programming_language_2.pdf
关于c - 如何用 C 语言创建句子列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36488001/