简易用户登录
#include<stdio.h>
#include<string.h>
int compare(char *input,char *record);
int main()
{
int i;
//定义已有的用户名和密码
char *trueName = "柒";
char *trueCipher = "2004Xw";
//循环3次,意味着用户有3次错误的计划
for(i=2;i>=0;i--)
{
//定义name和cipher来接收用户键盘录入的数据
char name[10];
char cipher[20];
printf("请输入你的用户名:");
scanf("%s",name);
printf("请输入你的密码:");
scanf("%s",cipher);
//判断是否存在这个用户名
if(compare(name,trueName) == 0)
{
//存在就继续对比密码
if(compare(cipher,trueCipher) == 0)
{
//都存在就提示登录成功
printf("恭喜你,登录成功!\n");
//成功后就结束循环
break;
}
//密码错误则提示密码错误,同时提示剩余机会
else
{
printf("密码错误,你还有%d次机会\n",i);
}
}
//用户名错误则提示用户名错误,同时提示剩余机会
else
{
printf("用户名错误,你还有%d次机会\n",i);
}
}
return 0;
}
//定义一个函数对比键盘录入的数据和已有的数据,相同返回0,不同返回非0
int compare(char *input,char *record)
{
int test = strcmp(input,record);
return test;
}
录入字符串判断各种字符数量
#include<stdio.h>
#include<string.h>
int main()
{
char str[100];
int i;
int bigCount = 0;
int smallCount = 0;
int numberCount = 0;
printf("请输入一条字符串(长度在100内):");
scanf("%s",str);
for(i=0;i<strlen(str);i++)
{
if(str[i]>='a' && str[i]<='z')
{
smallCount++;
}
else if(str[i]>='A' && str[i]<='Z')
{
bigCount++;
}
else if(str[i]>='0' && str[i]<='9')
{
numberCount++;
}
}
printf("你输入的字符串中:\n大写字符有%d个;\n小写字符有%d个;\n数字字符有%d个;\n",bigCount,smallCount,numberCount);
return 0;
}