首页 > 代码库 > C语言 aabbcc、abc、fabc、aabc
C语言 aabbcc、abc、fabc、aabc
输入一个字符串,匹配字符串中连续出现的字符串、并且连续个数相等
如输入 aabbcc、abc、fabc、aabc、aabbc
分别输出yes还是no
#include<stdio.h>
#include<string.h>
void main()
{
/*
思路:输入一个字符串数组a,定义一个数组b[3]={1,0,0}(其用意是默认第一个是不需要匹配的)
*/
char a[]={‘f‘,‘a‘,‘a‘,‘a‘,‘b‘,‘d‘,‘c‘};
int b[3]={1,0,0};
int i = 0;
int f = 0;
int g = 0;
int h = 1;
printf("a===%d\n",sizeof(a));
for( i = 1 ; i< sizeof(a);i++)
{
printf("i====%d\n",i);
//如果a[i-1] == a[i],默认从a[0] == a[1]开始,相等就加一,相当于a[0]=‘a‘ == a[1]=‘a‘
if(a[i-1] == a[i]){
b[g] = ++h;
printf("b[%d]=%d\n",g,b[g]);
}
//此处只需要判断不相等
if((a[i-1] != a[i]) && (a[i-1]-a[i] == -1)){//&& a[i]-a[i+1] = 1
if( a[i-1]-a[i] == -1){
h = 1;
g = g+1;
b[g] = h;
}else{
if(g==0){
b[0] = 1;
b[1] = 0;
b[2] = 0;
}
if(g == 1){
b[0] = b[1];
b[1] = 0;
b[2] = 0;
}
if(g == 2){
b[0] = b[2];
b[1] = 0;
b[2] = 0;
}
h = 1;
g = 0;
}
}
printf("%d\n",b[0]);
printf("%d\n",b[1]);
printf("%d\n",b[2]);
printf("-----------------------------\n");
if(b[0] >= b[1] && b[1]== b[2] && b[2]>=1){
printf("yes\n");
break;
}
}
}
C语言 aabbcc、abc、fabc、aabc