题目: 输入一个字符串,同时输入帧头和帧尾(可以是多个字符),将该字符串中合法的帧识别出来.
提示:帧头和帧尾分别是head和tail 字符串”asdheadhauboisoktail”中headhauboisoktail是合法帧
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int main()
{
char *str = (char *)malloc(sizeof(char)*32);
char *ptr = (char *)malloc(sizeof(char)*32);
char *dtr = (char *)malloc(sizeof(char)*32);
printf("输入字符串 帧头 帧尾\n");
scanf("%s%s%s",str,ptr,dtr);
int len_str = strlen(str);
int len_ptr = strlen(ptr);
int len_dtr = strlen(dtr);
if(strncmp(str,ptr,len_ptr) == 0||strncmp(str + len_str - len_dtr,dtr,len_dtr) == 0)
{
printf("合法\n");
}
else
{
printf("不合法\n");
}
return 0;
}
简单通讯录
#include<stdio.h>
#include<string.h>
#define N 50
#define M 50
struct User
{
int id;
char name[N];
};
struct User addlist[M];
void firstscreen()
{
printf("********************欢迎进入通讯录系统**********************\n");
printf("*******************1.添加新用户 **********************\n");
printf("*******************2.删除用户 **********************\n");
printf("请输入选择:\n");
}
void adduser()
{
printf("please input id and name:\n");
static int i;
for (i = 0; i < 50; i++)
{
scanf("%d%s", &addlist[i].id ,addlist[i].name );
break;
}
// for(i = 0;i < 2; i++)
// {
// printf("id : %d name : %s\n",addlist[i].id,addlist[i].name);
// }
}
void deleteuser()
{
static int j = 0;
char name[30];
printf("请输入想要删除通讯录的名字:");
scanf("%s",name);
for(j = 0; j < 50 ;j++)
{
if(strcmp(addlist[j].name,name) == 0)
{
break;
}
}
if(j == 49)
{
addlist[j].id = 0;
strcpy(addlist[j].name,'\0');
}
for(;j < 49 ;j++)
{
addlist[j].id=addlist[j + 1].id;
strcpy(addlist[j].name,addlist[j + 1].name);
}
}
int main()
{
int a;
while(1)
{
firstscreen();
scanf("%d",&a);
switch(a)
{
case 1:
adduser();
break;
case 2:
deleteuser();
break;
deafult:
printf("input error");
}
}
return 0;
}