蓝桥杯学习记录-21.3.20

蓝桥杯学习记录-21.3.20

字符串和日期基础3

字符串:是一种常用的数据类型。末尾以\0结束的字符型数组称为字符串,否则只能称为字符型数组,在C中用“%s"输出;在C++中用string类型对字符串进行操作。

字符串复制:char *strcpy(char *destin,char *source)

#include <stdio.h>
#include <string.h>
int main()
{
	char name[10];
	char *str ="www";
	strcpy(name,str);
	printf("%s\n",name);
	return 0;
}

字符串连接:char*strcat(char *dest,char *source)

#include <string.h>
#include <stdio.h>
int main()
{
	char dest[10];
	char *str1 ="hello",*str2 =" ",*str3 ="wwww";
	strcat(dest,str1);
	strcat(dest,str2);
	strcat(dest,str3);
	printf("%s\n",dest);
	return 0;
}

字符串比较strcmp

#include <string.h>
#include <stdio.h>
int main()
{
	char *str1 = "cd",*str2="abc";
	int res=strcmp(str1,str2);
	if(res>0)
	{
		printf("%s>%s\n",str1,str2);
	}
	else if(res==0)
	{
		printf("%s=%s\n",str1,str2);
	}
	else
	{
		printf("%s<%s\n",str1,str2);
	}
	return 0;
}

字符串长度strlen

对称字符串

1、问题描述

问题描述

输入一个正整数N(N<=20)

样例输入

1

样例输出

A

样例输入

2

样例输出

ABA

样例输入

3

样例输出

ABACABA

样例输入

4

样例输出

ABACABADABACABA

2、解题思路

(1)注意res数组定义在main函数外面

3、代码
#include <string.h>
#include <stdio.h>
char res[5000000];
int main()
{	
	int n;
	scanf("%d",&n);
	int len=0;
	int i;
	for(i=1;i<=n;++i)
	{
		strcat(res+len+1,res);
		res[len]='A'+i-1;
		len=strlen(res);
	}
	printf("%s\n",res);
	return 0;
}

二、查找字符串

1、题目描述

样例输入

i miss you!

you

样例输出

1

样例输入

ossosso

osso

样例输出

2

2、解析

(1)注意fgets取得字符串后求长度时要减1

3、代码
#include <cstring>
#include <cstdio>
char s1[1005],s2[1005];
int main()
{	
	fgets(s1,1004,stdin);
	fgets(s2,1004,stdin);
	int len1=strlen(s1)-1,len2=strlen(s2)-1;
	int ans=0;
	for(int i=0;i<len1-len2+1;++i)
	{
		bool matched=true;
		for(int j=0;j<len2;j++)
		{
			if(s1[i+j]!=s2[j])
			{
				matched=false;
				break;
			}
		}
		if(matched)
		{
			
			ans++;
		}
	}
	printf("%d\n",ans);
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值