formulla for calculate the occurance of a character in all substring of a given string .

Let’s the length of the string is ‘L’ and index ‘j’ which is start from 0. the formulla is –
                       
                    occurance=(j+1)*(L-j);

ex:-
        let’s string is “Rajnish” and we have to calculate occurancce of   ‘a’ in all substring found by this string.then the program is-

#include<stdio.h>
#include<string.h>
void main()
{
char s[10];
int length,j,sum=0,fre=0;
scanf(“%s”,s);
length=strlen(s);
for(j=0;j<length;j++)
{
if(s[j]==‘a’)
{
sum=sum+(j+1)*(lengthj);
fre++; // this is for how many times ‘a’ present in string because if ‘a’
//found two times then two substring are same and we have to considered only one
}
}
printf(“%d”,sumfre);
}


Leave a Reply

Your email address will not be published.