So for a lab at uni… Ive been challenged to find all words in the usr/share/dict/linux.words
file using fopen, fgets etc with every vowel only once, in order.
i.e. facetious
So far I have the following code… but its flawed somewhere…
int all_vowels( char *s )
{
const unsigned char *p = (const unsigned char *)s;
char *v = malloc(sizeof(char *));
char *vowel = v;
if(*p == '\0') return -1;
while( *p != '\0' )
{
if( *p == 'a' || *p =='e' || *p =='i'|| *p =='o' || *p =='u' )
{
*v = *p;
v++;
}
p++;
}
if ( *vowel == 'a' && (*vowel + 1) == 'e' && (*vowel + 2) == 'i' && (*vowel + 3) == 'o' && (*vowel + 4) == 'u' )
{
return 1;
}
return -1;
}
int main (int argc, char *argv[])
{
FILE *file;
char line[BUFSIZ];
if (( file = fopen("/usr/share/dict/words", "r") ) == NULL)
{
fprintf(stderr, "cannot open %s\n", "/usr/share/dict/words");
exit(1);
}
while ( !feof(file) )
{
fgets(line, sizeof(line), file);
if ( all_vowels(line) == 1 )
{
printf("%s\n", line);
}
}
fclose(file);
return 0;
}
Any tips would be great!!!
Im really confused at the moment…
You are accessing
vas if it were pointing to a location holding a number of characters, when indeed you only reserve space for one singlechar *(usually 4 byte on a 32 bit machine and 8 byte on a 64 bit machine):That might or might not be enough for what you are trying to store in it; in your case, the number of vowels, in any given word.
Whenever possible, you should avoid dynamic allocations; in your case, you don’t need them, you can declare an array of fixed size instead of a char*:
In addition to that, you have to check if you have read 5 vowels already, so that you don’t exceed the array size; if, after 5 vowels, you encounter another one, you can stop the check anyway; the currently encountered one has to be a duplicate vowel, the word will therefore not qualify.
The way you address characters is also a problem. Check again what
*does: it dereferences the expression immediately to the right. In your case, it will always dereference v, then add something to it (which is also legal, since the result of dereferencing is a char). So if the first character where v points to is an a, the second an e, then*vwill yield'a',(*v + 1)will yield'b',(*v +2)will yield'c'and so on – you see, the result is an addition to the letter a by the given number; it doesn’t matter at all what comes after the first character because you never access the value there. To achieve what you want with pointer arithmetic, you’d have to use parenthesis:*(v+1)– i.e., add 1 to the pointerv, then dereference it. This would yield the second character in the c string starting atv, i.e.'e'.Note that with v declared as above you can simply write
v[0],v[1],v[2]and so on to address each character.Aside from that, check the last comparison in your if condition, you had an ‘e’ instead of an ‘u’ there.
By the way, as a side note, and something to think about: There is a solution to your problem which does not require the
v/vowelvariables at all… only a single integer variable!