Given a string S and a character C, write a program to count the number of substrings of S that contains only the character C.
Sample Input0110111
1
Sample Output
9
#include<stdio.h>
int main() {
char str[30];
int i,ch,c=0,SubstrCount=0;
fgets(str,30,stdin);
scanf("%c",&ch);
for(i=0;str[i]!='\0';i++){
if(str[i]==ch){
c++;
}else{
SubstrCount += (c*(c+1))/2;
c=0;
}
}
SubstrCount += (c*(c+1))/2;
printf("%d",SubstrCount);
return 0;
}
int main() {
char str[30];
int i,ch,c=0,SubstrCount=0;
fgets(str,30,stdin);
scanf("%c",&ch);
for(i=0;str[i]!='\0';i++){
if(str[i]==ch){
c++;
}else{
SubstrCount += (c*(c+1))/2;
c=0;
}
}
SubstrCount += (c*(c+1))/2;
printf("%d",SubstrCount);
return 0;
}