# Massive Function Problem

• 11-18-2003
Marc Sharp
Massive Function Problem
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.

• 11-18-2003
Salem
First, edit your post so the code tags actually SURROUND your code, and are not merely just dumped in your message to make the "please use tags" prompt go away
• 11-18-2003
Marc Sharp
Sorry about that, it was a genuine mistake.
• 11-18-2003
Salem
Nope, still not right
Use preview to make sure you get it right
• 11-18-2003
Sayeh
Good heavens! All your trying to do is this:

Quote:

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.
Why do you need _so much code_?

All you need is a little loop and 5 counters. This whole thing can be done (comments and all) under about 25-30 lines of code.
• 11-18-2003
Marc Sharp
It may sound fairly easy for somebody who writes hundreds of lines of code on a daily basis but for a near beginner it is quite hard, especially as I have been told to use five different functions.

Any ideas then on how to finish it?
• 11-18-2003
Hammer
Code:

```#include <stdio.h> #include <ctype.h> int CountWhiteSpace(const char *s) {   int c;     for (c = 0; *s != '\0'; s++)   {     if (isspace(*s)) c++;   }     return c; } int main(void) {   printf ("Number of white spaces is %d\n", CountWhiteSpace(" blah blah blah"));   return(0); }```
• 11-18-2003
Sebastiani
You can use a single function to count everything. Here's the prototype:

int count_any(const char s1[], const char s2[]);

It works like this:

Set up a loop on 's1' and a nested loop on 's2'. If any two characters match, increment a counter. That way you can do:

int vowels = count_any(str, "aeiouAEIOU");
int spaces = count_any(str, " ");

...etc.
• 11-19-2003
Marc Sharp
So how would I put that as a function? Also where would I put that function in relation to the original code I posted?

Cheers for the help, it is becoming very handy indeed!
• 11-19-2003
WaltP
Quote:

Originally posted by Sebastiani
You can use a single function to count everything. Here's the prototype:

int count_any(const char s1[], const char s2[]);

It works like this:

Set up a loop on 's1' and a nested loop on 's2'. If any two characters match, increment a counter. That way you can do:

int vowels = count_any(str, "aeiouAEIOU");
int spaces = count_any(str, " ");

...etc.

So you're saying that the requirement stating
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.

should be ignored? Software specifications are written for a purpose and must be followed otherwise the boss gets mad.
• 11-19-2003
Sebastiani
Don't be rediculous. The requirements are in the interface, not the implementation.

Code:

``` int whitespace(const char str[]) {  return count_any(str, " "); } int vowels(const char str[]) {  return count_any(str, "aeiouAEIOU"); }```

Reusing code and isolating errors makes deadlines.

And that makes bosses happy. ;)