密码 猜数字游戏 二分法找下标 字符反转

本文深入讲解C语言编程的四个核心应用:密码验证、猜数字游戏、二分查找算法及字符大小写转换。通过实例演示如何使用C语言进行逻辑控制、随机数生成、数组操作及字符处理。

#include<stdio.h>
#include<stdlib.h>
#include<string.h>//密码
int main(){
int i = 0;
for (; i < 3; i++)
{
printf(“请输入密码:”);
char passwd[1024] = { 0 };
scanf("%s", passwd);
if (strcmp(passwd, “888888”) == 0){
printf(“登录成功!!\n”);
break;
}
else
{
printf(“你的密码输入有误!\n”);
}
}
if (i == 3){
printf(“登陆失败!\n”);
}
system(“pause”);
}

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>//猜数字游戏
#include<stdlib.h>
#include<time.h>
int main()
{
int a,b=0;
srand((unsigned)(time(0)));
int number = rand()%100+1;

	while (1)
	{
		printf("输入你要猜的数");
		scanf("%d", &a);
	if (a > number)
	{
		printf("数字大了\n");
		continue;
	}
	else if (a < number){
	printf("数字小了\n");	
	continue;
	}
	else{
		printf("猜对了\n");
		break;
	}
}

system(“pause”);
return 0;
}

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>//二分法找下标
int main()
{
int arr[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
int num ;
printf(“请输入一个数组中的数”);
scanf("%d", &num);
int left = 0;
int right = sizeof(arr) / sizeof(int)-1;
//[left,right]待查找区间
//如果待查找区间不为空,继续找
while (left <= right){
int mid = (left + right) / 2;
if (arr[mid] < num)
{
left = mid + 1;
}
else if (arr[mid] > num){
right = mid - 1;
}
else{
printf(“下标为%d\n”, mid);
break;
}

}

system(“pause”);
return 0;
}

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>/如果是小写字符就输出对应的大写字符,
如果接收的是大写字符,就输出对应的小写字符,
如果是数字不输出。
/
#include<stdlib.h>
int main()
{
while (1)
{
char str;
//scanf("%c", &str);
str = getchar();
if (str>=‘a’&&str<=‘z’)
{
int a = str - 32;
printf("%c", a);
continue;
}
else if (str>64 && str < 91)
{
int a = str + 32;
printf("%c", a);
continue;
}
else
continue;
return 0;
}
system(“pause”);
return 0;
}

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值