c - 如何用 C 语言创建句子列表

标签 c

我想知道在 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/

相关文章:

c - 诡异的 。 C 中的表示法?这叫什么,有人可以解释一下吗?

C++:extern "C"和类成员之间的命名空间冲突

c - 快速排序算法给出段错误

c - 打印到屏幕 n 个字符串

c - 使用unput错误

c - malloc() 如何知道堆从哪里开始?

对 getchar() 和 fflush(stdin) 行为感到困惑

c - 在 C 中对链表进行排序

c - 将 Emacs 缓冲区传递给 C

c - 在某个重复字符后从字符串中删除文本