0% found this document useful (0 votes)
30 views5 pages

Message

This C program takes a string as input, breaks it into 4-letter words, sorts the words alphabetically, and finds the most frequently occurring word(s). It defines functions to find the length of a string, copy strings, compare strings, and merge sort strings. It breaks the input string into 4-letter words, sorts the words, identifies the most frequent word(s) and their frequencies, and prints the most common word(s).

Uploaded by

Mansif Hossain
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views5 pages

Message

This C program takes a string as input, breaks it into 4-letter words, sorts the words alphabetically, and finds the most frequently occurring word(s). It defines functions to find the length of a string, copy strings, compare strings, and merge sort strings. It breaks the input string into 4-letter words, sorts the words, identifies the most frequent word(s) and their frequencies, and prints the most common word(s).

Uploaded by

Mansif Hossain
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 5

#include<stdio.

h>
#include<string.h>
#include<math.h>

int slength(char ara[])


{
int c=0;
for(int i=0;ara[i]!='\0';i++)
{
c++;
}
return c;
}

void scopy(char *ara1,char ara2[])


{
int i;
for(i=0;ara2[i]!='\0';i++)
{
*ara1=ara2[i];
ara1++;
}

*ara1='\0';

}
int scmp(char ara1[],char ara2[])
{
int j=0;
while(1)
{
if(ara1[j]=='\0' && ara2[j]=='\0')
{
return 1;
}

else if(ara1[j]=='\0')
{
return 3;
}

else if(ara2[j]=='\0')
{
return 2;
}

else if(ara1[j]>ara2[j])
{
return 2;
}

else if(ara1[j]<ara2[j])
{
return 3;
}

j++;
}
}
char str[1000001];
int serial[1000001];
char word[1000001][5];
char word2[1000001][5];

void sorting(int l,int m,int r,int i,int j,int c)


{

if(c==r+1)//Element is out of range


return;

if(i==m-l+1)//If the elements of the first half is finished with sorting


{

scopy(word[c],word2[m+j+1]);
c++;
j++;
}

else if(j==r-m)//if elements of the second half is finished with sorting


{

scopy(word[c],word2[l+i]);
c++;
i++;
}

else if( scmp(word2[l+i],word2[m+j+1])==3 || scmp(word2[l+i],word2[m+j+1])==1)


{

scopy(word[c],word2[l+i]);
c++;
i++;

else if(scmp(word2[l+i],word2[m+j+1])==2)
{

scopy(word[c],word2[m+j+1]);
c++;
j++;

}
sorting(l, m, r, i, j, c);

void merged(int l,int r)


{
if(l>=r)return;
int m=(l+r)/2;//middle point m
merged(l,m); //Sorting first half
merged(m+1,r);//Sorting second half

sorting(l,m,r,0,0,l);//Merge two sorted half


//Will Copy elements of word array to word2 array from l to r
for(int j=l;j<=r;j++)
{
strcpy(word2[j],word[j]);
}
}

int main()
{
scanf("%[^\n]s",str);
int c=0,i,j;
int n=strlen(str);

if(n>=4)
{
word[c][0]=word2[c][0]=str[0];
word[c][1]=word2[c][1]=str[1];
word[c][2]=word2[c][2]=str[2];
word[c][3]=word2[c][3]=str[3];
word[c][4]=word2[c][4]='\0';

c++;
}

for(i=4; i<n; i=i+1)


{
word[c][0]=word2[c][0]=word[c-1][1];
word[c][1]=word2[c][1]=word[c-1][2];
word[c][2]=word2[c][2]=word[c-1][3];
word[c][3]=word2[c][3]=str[i];
word[c][4]=word2[c][4]='\0';

c++;

int l=0;
int r=c-1;

merged(l,r);
//if length of word array is less than 4,c will be 0;
if(c==0)
{
printf("\n");
}

else
{
int maxc=1;
int cnt=1;
int f1=0,f2=0;//f1 is the frequency of any string
//f2 is the frequency of the string that occurs the highest
time

serial[f1]=0;//Serial is the which keeps the index of the strings which


occurs the highest

for(i=0;i<c-1;i=i+1)
{
if(scmp(word[i],word[i+1])==1)
{
cnt++;
}

else
{
if(cnt==maxc)
{
serial[f1]=i;
f1++;
cnt=1;
f2=f1;
}

else if(cnt>maxc)
{
maxc=cnt;
cnt=1;

f2=1;
f1=0;
serial[f1]=i;
f1++;
}
}

if(cnt==maxc)
{
serial[f1]=i;
f1++;
cnt=1;
f2=f1;
}

else if(cnt>maxc)
{
maxc=cnt;
cnt=1;
f2=1;
f1=0;
serial[f1]=i;
f1++;
}

for(i=0;i<f2;i=i+1)
{
printf("%s\n",word[serial[i]]);
}
}

return 0;
}

You might also like