Day 12 算法笔记之入门模拟3.6 字符串处理

目录

1.回文串

2.说反话

3.字符串连接

4.首字母大写

5.字符串的查找删除

6.单词替换


1.回文串

首先,如果要求数组对称的那个元素,如果前面的是i,则后面的是len-1-i

其次,这种情况如果用函数就会更简洁一下

#include <cstdio>
#include <cstring>
#include <math.h>
#include <algorithm>
using namespace std;

int main(){
	
	char martix[300];
	
	while(scanf("%s",martix)!=EOF){
		
		int flag = strlen(martix)/2;
		
		for(int i=0;i<strlen(martix)/2;i++){
			if(martix[i] != martix[strlen(martix)-1-i]){
				printf("NO\n");
				break;
			}else{
				flag--;
			}
		}
		
		if(flag==0){
			printf("YES\n");
		}
	}
	
	return 0;
}

2.说反话

最后一个如果还输出空格就错了,所以要额外判断一下

#include <cstdio>
#include <cstring>
#include <math.h>
#include <algorithm>
using namespace std;

int main(){
	
	char martix[100];
	
	gets(martix);
		
	int len=strlen(martix);
	
	char final[100][100];
	
	int row=0,col=0;
	
	for(int i=0;i<len;i++){
		if (martix[i]!=' '){
			final[row][col] = martix[i];
			col+=1;
		}else{
			final[row][col] = '\0';
			row+=1;
			col=0;
		}
	}
	
	for(int i=row;i>=0;i--){
		printf("%s",final[i]);
		if(i>0){
			printf(" ");
		}
	}

	
	return 0;
}

3.字符串连接

用多个数组直接获取多个字符串

'\0'是数组的结束位置

是strcat的原理实现

#include <cstdio>
#include <cstring>
#include <math.h>
#include <algorithm>
using namespace std;

int main(){
	
	char a[100],b[100];
	
	while(scanf("%s %s",a,b)!=EOF){
		
		int la=0,lb=0;
		
		while(a[la]!='\0'){
			la++;
		}
		
		while(b[lb]!='\0'){
			lb++;
		}
		
		for(int i=la;i<la+lb;i++){
			a[i] = b[i-la];
		}
		
		for(int i =0;i<la+lb;i++){
			printf("%c",a[i]);
		}
		
		printf("\n");
	}

	
	return 0;
}

4.首字母大写

首先,scanf和fgets好像有些区别,如果是有空格的字符串输入,最好是用fgets

其次,能不拆最好别拆成一个个单词处理,会很麻烦,直接加入一个特征值判断一下是单词还是特殊符号最好

#include <cstdio>
#include <cstring>
#include <math.h>
#include <algorithm>
using namespace std;

int main(){
	
	char martix[200];
	
	while(fgets(martix,199,stdin)){
		
		int flag=0;
		
		for(int i=0;i<strlen(martix);i++){
			char x = martix[i];
			if(x==' '||x=='\t'||x=='\r'||x=='\n'){
				flag=0;
			}else{
				if(flag==0&&martix[i]>=97&&martix[i]<=122){
					martix[i] -= 32;
				}
				flag = 1;
			}
		}
		
		printf("%s",martix);
		
		
		
	}
	

	
	return 0;
}

5.字符串的查找删除

我的答案对样例是对的,但是显示答案错误,不想仔细看了。。。

这一题首先要引入一个<cctype>的头文件,用tolower函数给所有字母降为小写

#include <cstdio>
#include <cctype>
#include <cstring>
#include <math.h>
#include <algorithm>
using namespace std;

bool judge(char a,char b){
	return tolower(a)==tolower(b)?1:0;
}

int main(){
	
	char shorts[20];
	scanf("%s",shorts);
	
	getchar();//吸收回车键
	
	char str[200];
	
	while(fgets(str,199,stdin)){
		
		int len_short = strlen(shorts);
		int len_str = strlen(str)-1;
		
		
		int flag=0;
		
		int ju[200] = {0};
		
		for(int i=0;i<len_str-len_short+1;i++){
			
			for(int j=0;j<len_short;j++){
				if(judge(str[i+j],shorts[j])==1){
					flag = 1;
				}else{
					flag = 0;
					break;
				}
			}
			
			if(flag==1){
				
				for(int j=i;j<i+len_short;j++){
					ju[j] = 1;
				}
				
			}
			
		}
		
		for(int i=0;i<len_str;i++){
			
			if(str[i]==' '){
				ju[i] = 1;
			}
		}
		
		char final[200];
		
		int pos=0;
		
		for(int i=0;i<strlen(str);i++){
			if(ju[i]!=1){
				final[pos++] = str[i];
			}
		}
		
		
		for(int i=0;i<strlen(final);i++){
			printf("%c",final[i]);
		}
		
		memset(final,'\0',sizeof(final));
	}
	
	memset(str,'\0',sizeof(str));
	

	return 0;
}

//#include <cstdio>
//#include <cctype>
//#include <cstring>
//const int maxn=400;
//bool Judge(char a,char b)
//{
//	return tolower(a)==tolower(b)?1:0;
//}
//void Predispose(char a[])
//{
//	char temp[maxn]={'\0'};
//	int len=0;
//	for(int i=0;a[i]!='\0';i++)
//		if(a[i]!=' ')
//			temp[len++]=a[i];
//	for(int i=0;i<strlen(a);i++)
//		a[i]=temp[i];
//}
//void SearchAndDelete(char a[],char n[])
//{
//	int flag=0;
//	for(int i=0;i<strlen(a);i++)
//	{
//		if(Judge(a[i],n[0])==1)
//		{
//			for(int j=0;j<strlen(n);j++)
//			{
//				if(Judge(a[i+j],n[j])==1)
//					flag=1;
//				else
//				{
//					flag=0;
//					break;
//				}
//			}
//		}
//		if(flag==1)
//		{
//			int len1=strlen(n);
//			int len2=strlen(a)-len1;
//			for(int j=i;j<len2;j++)
//				a[j]=a[j+len1];
//			a[len2]='\0';
//			i--;
//			flag=0;
//		}	
//	}
//}
//int main()
//{
//	char str[maxn],norm[maxn];
//	scanf("%s",norm);
//	getchar();
//	while(gets(str))
//	{
//		Predispose(str);
//		SearchAndDelete(str,norm);
//		printf("%s\n",str);
//	}
//	return 0;
//}

6.单词替换

思路对了,答案对了,无法通过测试,崩溃的一晚上

如果用scanf输入好像有时候会有坑,要在最后面加上一个getchar(),有时间好好研究下这个

数组一定要记得清空

#include <cstdio>
#include <cctype>
#include <cstring>
#include <math.h>
#include <algorithm>
using namespace std;

int main(){
	
	char martix[102];
	int n=0;
	
	while(fgets(martix,101,stdin)){
		
		char a[100],b[100];
		scanf("%s",a);
		scanf("%s",b);
		
		char sen[102][102];
		int len=strlen(martix);
		int row=0,col=0;
		
		for(int i=0;i<len;i++){
			if(martix[i]!=' '){
				sen[row][col++] = martix[i];
			}else{
				row+=1;
				col=0;
			}
		}
		
		for(int i=0;i<=row;i++){
			if(strcmp(sen[i],a)==0){
				printf("%s",b);
			}else{
				printf("%s",sen[i]);
			}
			
			if(i!=row){
				printf(" ");
			}
			
		}
		
		memset(sen,'\0',sizeof(sen));
		memset(martix,'\0',sizeof(martix));
		memset(a,'\0',sizeof(a));
		memset(b,'\0',sizeof(b));
		
		getchar();
	}
	

	return 0;
}
//#include <cstdio>
//#include <cstring>
//const int maxn=400;
//void Replace(char a[][maxn],char b[],char c[],int r)
//{
//	int len=strlen(c);
//	for(int i=0;i<=r;i++)
//	{
//		if(strcmp(a[i],b)==0)
//		{
//			for(int j=0;j<len;j++)
//				a[i][j]=c[j];
//			a[i][len]='\0';
//		}
//	}
//}
//int main()
//{
//	char a[maxn],b[maxn],c[maxn],ans[maxn][maxn];
//	while(gets(a))
//	{
//		scanf("%s",b);
//		scanf("%s",c);
//		int len=strlen(a),r=0,h=0;
//		for(int i=0;i<len;i++)
//		{
//			if(a[i]!=' ') 
//				ans[r][h++]=a[i];
//			else
//			{
//				ans[r][h]='\0';
//				r++;
//				h=0;
//			}
//		}
//		ans[r][h]='\0';
//		Replace(ans,b,c,r);
//		for(int i=0;i<=r;i++)
//		{
//			printf("%s",ans[i]);
//			if(i<r) printf(" ");
//		}
//		printf("\n");
//		memset(a,'\0',sizeof(a));
//		memset(b,'\0',sizeof(b));
//		memset(c,'\0',sizeof(c));
//		getchar();
//	}
//	return 0;
//} 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值