- #include<stdio.h>
- #include <STRING.H>
- #include<math.h>
- void main()
- {
- int num,swap,countg=0,countr=0,count1=0,count2=0;
- char string[100];
- while(1)
- {
- countg=0;
- count1=0;
- printf("please input the string:\\n");
- gets(string);
- num=strlen(string);
- for(swap=num-1;swap>=0;swap--)
- {
- if(string[swap]=='g')
- {
- countg++;
- }
- else
- {
- break;
- }
- }
- for(swap=0;swap<num-countg;swap++)
- {
- if (string[swap]=='r')
- {
- countr++;
- }
- else
- {
- break;
- }
- }
- count1=0;
- count2=0;
- for(swap=countr;swap<num-countg;swap++)
- {
- if(string[swap]=='g')
- count1++;
- if(string[swap]=='r')
- count2++;
- }
- num=count1>count2?count2:count1;
- printf("%d\\n",num);
- }
- }
- //该片段来自于http://www.codesnippet.cn/detail/041120136909.html
来源: http://www.codesnippet.cn/detail/041120136909.html