You will create a program that determines the language of a given input file bas
ID: 3533528 • Letter: Y
Question
You will create a program that determines the language of a given input file based on the words in that file. The structure of the input files will be one word per line (all lowercase letters). You should read these words in one LETTER at a time.
Each input file will contain 100000 letters, and be in a different language.
The way you are able to tell the languages apart is as follows:
English: The top letter frequencies are e, i, and a respectively.
Danish: The top letter frequencies are e, r, and n respectively.
Italian: The letters j, x, y do not exist in Italian (other than proper nouns, which are not present in the input files).
Your program MUST include 5 functions in addition to your main() function:
Explanation / Answer
#include <stdio.h>
#include <stdlib.h>
void fill(char a[], int b[])
{
int i;
for(i=0;i<26;i++)
b[i]=0;
for(i=0;i<10000;i++)
{
b[a[i]-'a']++;
}
}
void sort(char a[], int b[])
{
int i,j,temp;
char t;
for(i=0;i<25;i++)
{
for(j=i;j<26;j++)
{
if(b[j] < b[i])
{
temp = b[i];
b[i] = b[j];
b[j] = temp;
t = a[i];
a[i] = a[j];
a[j] = t;
}
}
}
}
double percent(char a[], int b[], char c)
{
int i;
for(i=0;i<26;i++)
{
if(a[i]==c)
return (((double)b[i])/100);
}
return 0;
}
void language(char a[], int b[], bool flag)
{
if(flag == true)
{
int i;
for(i=0;i<26;i++)
{
printf("Occurance of %c = %f ",a[i],(((double)b[i])/100));
}
}
if(percent(a,b,'j') == 0 && percent(a,b,'x') == 0 && percent(a,b,'y') == 0)
{
printf("Its Italian ");
return;
}
if(a[0] == 'e' && a[1] == 'r' && a[2] == 'n')
{
printf("Its Danish ");
return;
}
if(a[0] == 'e' && a[1] == 'i' && a[2] == 'a')
{
printf("Its English ");
return;
}
printf("Language cannot be determined ");
}
int main()
{
printf("Enter the filename ");
char fn[100];
scanf("%s",fn);
FILE* fp = fopen(fn,"r");
char c;
int i;
char* A = (char*)malloc(10001*sizeof(char));
for(i=0;i<10000;)
{
fscanf(fp,"%c",&c);
if(c == EOF)
{
printf("Cannot read 10000 characters ");
return 0;
}
if(c != ' ')
{
A[i]=c;
i++;
}
}
int b[26];
bool flag = true;
fill(A,b);
char a[26];
for(i=0;i<26;i++)
a[i]=('a'+i);
sort(a,b);
language(a,b,flag);
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.