Hi All,
I am getting really stuck on wriiting some function codes in C. I have got a general understanding of it I just cannot quite get my program to work. I am also missing a couple of functions.
I will firstly tell you the problem, then I will show the code.
Write a program that analyzes a single sentence for grammatical statistics. The file should be read from a text file called 1.txt.
Your program should include 5 functions;
a)Number of printable characters.
b)Number of white spaces.
c)Number of vowels.
d)Number of consonants.
e)Frequency of each vowel.
Once all functions are assembled in your program, use the data to derive and print to screen the following statistics;
a)String length.
b)Ratio of printing to non-printing characters.
c)Ratio of printing to non-printing characters.
d)Ratio of vowels to consonants.
e)A list of the frequency with which each vowel appears in the pring as a % of the number of printing characters.
MY code thus far;
Code:
/*assignment 2*/
#include<stdio.h>
#include<string.h>
int frequency( char[]);
int lengthcount(char words[]);
int vowelfrequency(char words[]);
int main(void)
{
FILE *fp;
/**/
int frequency_of_each_vowel();
/*int i;*/
char ch;
char sentence[81];
int count = 0;
int llengthcount;
int pprintablecount;
int vowel_count = 0;
fp = fopen("1.txt","r"); /*Opens up the text file*/
if( fp == NULL ) /*If the file does not open a message appears*/
printf("File could not be opened\r\n");
else
{
printf("The file contains the following text:\n\n");
while ( fscanf(fp,"%c",&ch) != EOF)
{
sentence[count]=ch;
printf("%c",sentence[count]);
count++;
}
sentence[count]='\0';
}
frequency(sentence);
fclose(fp);
}
int lengthcount(char words[])
{
/* set local scope function variables */
int llengthcount = 0, i = 0;
/* while loop to check array for terminating characters */
while (words[i] != '\0')
{
if (words[i] != '\0')
llengthcount++;
{
i++;
}
return llengthcount;
}
printf("\n\nThe file has a string lenght of: llengthcount %d", llengthcount, words );
}
/*function to find consonants*/
//Marc Sharp//
int frequency( char letters[] )
{
int vowel_count = 0;
int count = 0;
int consonant_count = 0 ;
int sum = 0, index =0;
while( letters[index] != '\0')
{
if( letters[index] == 'b' || letters[index] == 'B' ||
letters[index] == 'c' || letters[index] == 'C' ||
letters[index] == 'd' || letters[index] == 'D' ||
letters[index] == 'f' || letters[index] == 'F' ||
letters[index] == 'g' || letters[index] == 'G' ||
letters[index] == 'h' || letters[index] == 'H' ||
letters[index] == 'j' || letters[index] == 'J' ||
letters[index] == 'k' || letters[index] == 'K' ||
letters[index] == 'l' || letters[index] == 'L' ||
letters[index] == 'm' || letters[index] == 'M' ||
letters[index] == 'n' || letters[index] == 'N' ||
letters[index] == 'p' || letters[index] == 'P' ||
letters[index] == 'q' || letters[index] == 'Q' ||
letters[index] == 'r' || letters[index] == 'R' ||
letters[index] == 's' || letters[index] == 'S' ||
letters[index] == 't' || letters[index] == 'T' ||
letters[index] == 'v' || letters[index] == 'V' ||
letters[index] == 'w' || letters[index] == 'W' ||
letters[index] == 'x' || letters[index] == 'X' ||
letters[index] == 'y' || letters[index] == 'Y' ||
letters[index] == 'z' || letters[index] == 'Z' )
{
consonant_count++;
}
index++;
}
vowel_count = 0;
count = 0;
while ( letters[count] !='\0' )
{
if (letters[count] == 'a' || letters[count] == 'A' ||
letters[count] == 'e' || letters[count] == 'E' ||
letters[count] == 'i' || letters[count] == 'I' ||
letters[count] == 'o' || letters[count] == 'O' ||
letters[count] == 'u' || letters[count] == 'U')
{
vowel_count++; /*Increases the count of vowels each time it passes the loop and finds one*/
}
count++;
}
printf("\n\nThe file has a ratio of: consonants %d : %d vowels.\n\n", consonant_count, vowel_count);
}
{
/*Vowel Frequency*/
int vowelfrequency(char words[]);
{
vowel_count = 0;
count = 0;
while ( letters[count] !='\0' )
{
if (letters[count] == 'a' || letters[count] == 'A' ||)
printf("\n\nThe file has %d Letter A's.\n",vowel_count);
else (letters[count] == 'e' || letters[count] == 'E' ||)
printf("\n\nThe file has %d Letter E's.\n",vowel_count);
else (letters[count] == 'i' || letters[count] == 'I' ||)
printf("\n\nThe file has %d Letter I's.\n",vowel_count);
else (letters[count] == 'o' || letters[count] == 'O' ||)
printf("\n\nThe file has %d Letter O's.\n",vowel_count);
else (letters[count] == 'u' || letters[count] == 'U')
printf("\n\nThe file has %d Letter U's.\n",vowel_count);
{
vowel_count++; /*Increases the count of vowels each time it passes the loop and finds one*/
}
count++;
return 0;
}
}
}
As you can see I have got quite far, I am just missing functions for Number of printable characters and the number of white spaces.
I am also slightly unsure on how or where to insert the printf statements.
Any help, no matter how big or how small would be brilliant.
Thank you in advance.