1、
提示并输入一个字符串,统计该字符串中字母、数字、空格以及其他字符的个数
#include <stdio.h>
#include <string.h>
int main(int argc, const char *argv[])
{
char c = 0;
int big = 0,small = 0,num = 0,other = 0,space = 0;
printf("输入一个字符串:");
while((c=getchar()) !='\n')
{
//getchar()
//scanf("%c",&c);
//getchar();
if(c>='A' && c<='Z')
{
big++;
}
else if(c>='a' && c<='z')
{
small++;
}
else if(c>='0' && c<='9')
{
num++;
}
else if(c == ' ')
{
space++;
}
else
{
other++;
}
}
printf("big = %d small = %d num = %d space = %d other = %d\n",big,small,num,space,other);
return 0;
}
2、提示并输入一个字符串,求出该字符串中所有数字的总和
3、定义一个4*3的二维整形数组,完成对二维数组的输入、输出。并将该二维数组中每一行的最值放入到一个一维数组中,并对该维数组进行升序排序后输出。
#include <stdio.h>
#include <string.h>
#define M 3
#define N 4
int main(int argc, const char *argv[])
{
int s[M][N] = {0};
int arr[M] = {0};
int max_hang = 0;
int max_lie = 0;
int i = 0,j = 0,temp = 0;
for (i = 0; i < M; i++)
{
for (j = 0; j < N; j++)
{
printf("输入数组[%d][%d]的值:", i, j);
scanf("%d", &s[i][j]);
}
}
for (i = 0; i < M; i++)
{
for (j = 0; j < N; j++)
{
printf("%d\t",s[i][j]);
}
putchar(10);
}
for (i = 0; i < M; i++)
{
max_hang = i;
max_lie = 0;
for (j = 0; j < N; j++)
{
if (s[i][j] > s[max_hang][max_lie])
{
max_hang = i;
max_lie = j;
}
}
printf("最大值%d,下标[%d][%d]\n", s[max_hang][max_lie], max_hang, max_lie);
arr[i] = s[max_hang][max_lie];
}
for(i=0;i<M-1;i++)
{
for(j=0;j<M-i-1;j++)
{
if(arr[j]>arr[j+1])
{
temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
for(i = 0; i < M;i++)
{
printf("%d\t",arr[i]);
}
putchar(10);
return 0;
}
4、提示并输入两个一维整形数组,求这两个数组的交集。
5、完成注册和登录功能:使用两个一维字符数组存储账户和密码注册:
完成对账号和密码的输入
登录:将登录账号和密码跟注册的账号和密码进行匹配,如果相等,则登录成功,否则,登录失败
#include <stdio.h>
#include <string.h>
#define LOG 1
#define REG 2
int main(int argc, const char *argv[])
{
puts("*********************Please chose************************");
puts("*************************0.QUIT**************************");
puts("************************1.Log On*************************");
puts("*********************2.Registration***********************");
puts("**********************************************************");
int ret, chose;
char RegAccount[10] = {0}, LogAccount[10] = {0};
char RegPassword[10] = {0}, LogPassword[10] = {0};
while (1)
{
printf("input chose ->");
ret = scanf("%d", &chose);
if (ret != 1)
{
printf("input error\n");
return -1;
}
while ((getchar() != '\n'))
;
if (chose == 0)
{
break;
}
switch (chose)
{
case LOG:
printf("输入登录账号:");
gets(LogAccount);
printf("输入登录密码:");
gets(LogPassword);
if (strcmp(RegAccount, LogAccount) == 0)
{
if (strcmp(RegPassword, LogPassword) == 0)
{
printf("登录成功\n");
}
else
{
printf("密码错误\n");
}
}
else
{
printf("用户账号未注册,输入2进行注册\n");
}
break;
case REG:
printf("输入注册账号:");
gets(RegAccount);
printf("输入注册密码:");
gets(RegPassword);
break;
default:
printf("输入错误\n");
break;
}
}
return 0;
}
思维导图