• C

Read text file and output words

Hey experts !

      I'm rusty with file streams, could someone write me a small C program that simply reads a text file and outputs only the words that contain 4 or more vowels? ('a' 'i' 'e' 'o' 'u' 'y').
LVL 1
DiscomonkeyAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Kent OlsenData Warehouse Architect / DBACommented:
Hi Discomonkey,

We probably could.  But this reeks of homework, and the rules of the board prohibit us from "academic fraud", which is a blunt way of saying that we can't do your homework for you.

Give it a try and post your code.  We'll be glad to step in and help.


Good Luck!
Kent
0
Jaime OlivaresSoftware ArchitectCommented:
Since you are working with C language, I think you must use fopen() like functions.
So a typical skeleton to read any text file is:

/* you must specify maximum line size here */
#define LINESIZE 1000
char buffer[LINESIZE];  
FILE *fp;
fp = fopen("yourfile.txt", "r");
if (fp) {   /* file opened succefully */
    while (!feof(fp)) {
        fgets(buffer, LINESIZE-1, fp);
        ProcessLine(buffer);
    }
    fclose(fp);
} else {
    printf("Error opening file\n\r");
}

You will find something like this in any book. But here the hard job start, I have just isolated it.

You have to implement a 'ProcessLine' function, where you will do the 'parsing' process:

ProcessLine(char *buffer)
{
     /* Process a line at a time */

}

Inside your function, you need to create some 'word buffer' to store every word from line. Then, analyze word to find those that match your search criteria and output to screen.

Now you have enough information to write your first test code.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Kent OlsenData Warehouse Architect / DBACommented:

Hi Jaime,

You're more tolerant than I am.  If the poster won't even respond to the "standard disclaimer" about academic honesty, he gets nothing more from me!


Kent
0
Jaime OlivaresSoftware ArchitectCommented:
I have not written a full code and leave the "heart" of problem to author. Just put him in the right way, from my point of view. I agree with you, I am more tolerant, but still author have a big job to do.
0
DiscomonkeyAuthor Commented:
thanks for the replies, I  wrote a solution like this:


#include<iostream>
#include<stdio.h>

int main(){
      FILE *file;
      char c;
      int vowelcount = 0, i = 0;
      char vowelword[20];
        file = fopen("Elites.text", "r");
      if(file==NULL) {
            printf("Error: can't open file.\n");
            return 0;
      }
      else {
            while(true) {  
                  
                  c = fgetc(file);
                  if(c != EOF) {
                        if(c == ' ' || c=='.' || c==',' || c=='-' || c=='\n' || c==':' || c=='`' || c=='/') {
                              if(vowelcount >= 4) {
                                    for(int x = 0; x<i; x++){printf("%c", vowelword[x]);}
                                    printf(" ");
                              }
                              vowelcount = 0;
                              i = 0;
                        }
                        else{
                              if(c == 'a' || c == 'e' || c=='i' || c=='o' || c=='u' || c=='y'){
                                    vowelcount++;
                                    vowelword[i] = c;
                                    i++;
                              }
                              else{
                                    vowelword[i] = c;
                                    i++;
                              }
                        }                  
                  }
                  else {
                        break;    
                  }
            }
            fclose(file);
                        
      }
      
return 0;
}


I came up wtih it while waiting for replies, gonna give the points to jaime.  
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.