C语言小例子(一)

今天考科目三,但是也要抽空学习(手动狗头)

顺便提一下ctype里面的一些好用的东西

来源菜鸟教程

来源菜鸟教程
有了这些,又可以省一些力气

下面看一道简单的C语言编程题:

随机构造50个句子,使用4个名为article(冠词),noun(名词),verb(动词),preposition(介词)的指向字符串的指针数组。
数组article包含“the”,“a”,“one”,“some”和“any”;数组noun包含“boy”,“girl”,“dog”,“town”和“car”;数组verb包含“drove”,“jumped”,“ran”,“walked”和“skipped”;数组preposition包含“to”,“from”,“over”,“under”和“on“。
程序按如下顺序、随机地从每个数组中挑选一个单词来构造句子:article,noun,verb,preposition,article和noun。每个句子的第一个字母大写,句子以句号结束。

附Y4tacker大佬解答 如下:

#include<stdio.h>
#include<string.h>
#include<ctype.h>
#include<time.h>
int suiji(int *,int *,int *,int *);				//这个函数实现随机数的生成
int main()
{
	const char* artical[5] ={"the","a","one","some","any"};
	const char* noun[5]={"boy","girl","dog","town","car"};
	const char* verb[5]={"drove","jumped","ran","walked","skipped"};
	const char* preposition[5]={"to","from","over","under","on"};
	char final_sentence[50];
	int w,z,s,q;
	int i;
	srand(time(NULL));
	for(i=0;i<50;i++)
	{
		suiji(&w,&z,&s,&q);

		strcpy(final_sentence,artical[w]);
		strcat(final_sentence," ");
		strcat(final_sentence,noun[z]);
		strcat(final_sentence," ");
		strcat(final_sentence,verb[s]);
		strcat(final_sentence," ");
		strcat(final_sentence,preposition[q]);
		strcat(final_sentence," ");

		a = rand() % 5;
		n = rand() % 5;
		strcat(final_sentence,artical[w]);
		strcat(final_sentence," ");
		strcat(final_sentence,noun[z]);
		strcat(final_sentence,".");
		final_sentence[0] = toupper(final_sentence[0]);
		puts(final_sentence);

	
	}

}

int suiji(int *w,int *z,int *s,int *q)
{
		*w = rand() % 5;
		*z = rand() % 5;
		*s = rand() % 5;
		*q = rand() % 5;
}



 师傅们有什么高强本领可以在评论区指点哦

每天进步,北海虽赊,扶摇可接。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值