没事儿写函数系列 str相关

总结:写代码时间短了真的bug特多

思路:定义一个方法  其实是我觉得可以定义好多方法!

       大体思路:先找str1 and str2相同的

                         找到一个就检查是否在str1 str2中都唯一

                         是就result++;

                                       然后:或者先把两个str唯一的char先选出来,选择先做,这样的话肯定会快一些!但是不想写了,而且相对也麻烦一些,然后再比较!

题目:

#include "stdio.h"



//
/fun 输入两字符串  
/		输出相同且唯一的字符串 
//

int char_same_one(char str1[], char str2[])
{
	int str1_num = 0;
	int str2_num = 0;
	char temp;
	int count1;
	int count2;
	int result = 0;
	
	for(int i = 0;; ++i)
	{
		if(str1[i] == '\0')	break;
		str1_num++;	
	}
	for(int i = 0;; ++i)
	{
		if(str2[i] == '\0')	break;
		str2_num++;	
	}

	printf("str1Long:%d  \nstr2Long:%d\n",str1_num,str2_num); 	//Test长度 
	
	
	
	
	for(int i = 0; i < str1_num; ++i)
	{
		if( str1[i] == ' ') continue;
		for(int j = 0; j < str2_num; ++j)
		{
			if(str1[i] == str2[j])
			{
				count1 = count2 =0;
//				printf("\nsss count1:	%d \n count2:	%d",count1,count2);		//Test	置0 
				temp = str1[i];
				for(int s1 = 0; s1 < str1_num; ++s1)
					{
						if(temp == str1[s1])	
						{
							count1++;	
						}									
					}
				for(int s2 = 0; s2 < str2_num; ++s2)
					{
						if(temp == str2[s2])	
						{
							count2++;	
						}									
					}
//				printf("\n count1:	%d \n count2:	%d",count1,count2);		//Test 同时候count  
				if( 1 == count1 && 1 == count2 )
				{
//					printf("\n %c \n\n",temp);
					result++;
				}else	continue;		
			}
			
			else continue;
		}
	}
	return result; 
	
}

int main(){
	int result;
	char a[80],b[80];
//	result	=	char_same_one("abc*defghijklmnos","a*scde");
//	printf("\n%dsss",result);
	while(1)
	{
	printf("Input1 ");
	scanf("%s",a);
	printf("Input2 ");
	scanf("%s",b);
	result = char_same_one(a,b);
	printf("\n----------满足条件num:%d---------------------\n",result);
	 } 

	return 0;
}


 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值