Assuming that a text file named FIRST.TXT contains some text written into it,
write a function named vowelwords( ), that reads the file FIRST.TXT and creates
a new file named SECOND.TXT, to contain only those words from the file
FIRST.TXT which start with a lowercase vowel (i.e. with ‘a’, ‘e’, ‘i’, ‘o’, ‘u’). For
example if the file FIRST.TXT contains
Carry umbrella and overcoat when it rains
Then the file SECOND.TXT shall contain
umbrella and overcoat it
A Function in Data File handling... C++?
I don't know how to inhibit the Yahoo software from
left justifying every line, so the lack of indentation
will make this hard to read. The code is really "C",
but it compiles and runs just fine under a C++ compiler.
Change the "fprintf(outfile" to "outfile%26lt;%26lt;" if you prefer.
In any case, "strtok" from the ANSI C library makes
this pretty straight-forward.
#include %26lt;stdio.h%26gt;
#include %26lt;string.h%26gt;
#include %26lt;stdlib.h%26gt;
int
main(int argc, char* argv[])
{
char buff[1000], *p, *t;
FILE *in = fopen("FIRST.TXT", "r");
FILE *out = fopen("SECOND.TXT", "w+");
if (!in || !out)
perror("fopen"), exit(-1);
while (fgets(buff, sizeof(buff), in)) {
p = buff;
while (t=strtok(p, " \t")) {
p = 0;
if (*t=='a' || *t=='e' || *t=='i' || *t=='o' || *t=='u')
fprintf(out, "%s ", t);
}
fprintf(out, "\n");
}
return 0;
}
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment