-3

I wrote code that find all vowels which used in all words in the text. And I do not know how to transfer it. Do I need to rewrite all code? So, I need to have such results:

Text:

wwe w fa

Result:

o u i

#include <stdio.h>
#include <ctype.h>
#define vowel (1u<<('a'-'a') | 1u<<('e'-'a') | 1u<<('i'-'a') | 1u<<('o'-'a') | 1u<<('u'-'a'))
unsigned int char_to_set(char c) 
{
    c = tolower(c);
    if (c < 'a' || c > 'z') 
        return 0; else return 1u<<(c-'a'); 
}
int letter(int c) 
{
    return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z');
}
int sign(int c) 
{
    return c == ' ' || c == ',' || c == '\n' || c == '\t';
}
int main () 
{
    int c, flag=0;
    char alpha;
    unsigned int sl = 0, mn = vowel;
    FILE *pf;
    pf=fopen("l13.txt","r");
    printf ("Ishodnyi text:\n\n");
    while (!feof(pf)) 
    {
        c=getc(pf);
        printf("%c",c);
        switch (flag) 
        {
            case (0): 
            {
                if (letter(c)) 
                             { 
                               sl = sl | char_to_set(c); 
                               flag = 1;
                            } 
                if (sign(c)) flag = 0;
                break;
            }
            case (1): 
            {
                if (letter(c)) 
                              { 
                                sl = sl | char_to_set(c); 
                                flag = 1;
                              }
                if (sign(c)) 
                             { 
                              mn = mn & sl; 
                              sl = 0; 
                              flag = 0;
                             }
                break;
            }
        }
    }

    if (mn == 0) { printf ("\n\n no vowels are included in all word"); } else {                                                                         printf ("\n\n vowels are included in all word:\n");                                                                         for(alpha='a'; alpha <= 'z'; alpha++){                                                                                                                          if((mn & char_to_set(alpha)) != 0){                                                                                                                             printf("%c ", alpha);
    }  
}
} 
fclose(pf);
getchar();
return 0;
}
Weather Vane
  • 33,872
  • 7
  • 36
  • 56
Bran
  • 1
  • 3
  • 1
    What do you mean by "transfer it"? Are you talking about moving the source file to a different computer? – John Bollinger Dec 22 '15 at 21:08
  • `while (!feof(pf))` please see the FAQ [Why is “while ( !feof (file) )” always wrong?](http://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong). Better is the idiomatic `while ((c = getc(pf)) != EOF) {...}` – Weather Vane Dec 22 '15 at 21:16
  • the input and output do not match the description of your problem. – ForeverStudent Dec 22 '15 at 21:19
  • @ForeverStudent please explain why? (see title statement of problem) – Weather Vane Dec 22 '15 at 21:20
  • If you found all vowels used in the text - the others must be the unused ones. Or what are you looking for? – Bo Persson Dec 22 '15 at 21:20
  • No, if i have such text Input: awa a rea Output: o i u. Now my program finds all vowels which used in every word in the text. Ex: Input: awe aw. Output: a (only a). – Bran Dec 22 '15 at 21:22
  • @WeatherVane you forgot the word "never" in the description – ForeverStudent Dec 22 '15 at 21:24
  • @ForeverStudent **please** read the question title carefully. *"Find all vowels which **never** used in all words in the text"* – Weather Vane Dec 22 '15 at 21:25
  • Read the description – ForeverStudent Dec 22 '15 at 21:27
  • That's right , the description is correct, the code attempts to find all the vowels used, which is required to establish which are not used, as asked in the title, and illustrated in the example. Do you have anything useful to say? – Weather Vane Dec 22 '15 at 21:28

1 Answers1

1

There are many ways to do what you want. Below is one way. There may be better ways but hopefully it will give you some ideas to improve it.

If I understand your code correctly, mn contains the bit mask of vowels present in the text. So you can write a function to check all the vowel bits that are not set. The following code checks for a and e only but I think it should be clear how to extend it for the other other vowels.

#define A_MASK (1u<<('a'-'a'))
#define E_MASK (1u<<('e'-'a'))

/*
 * Convenience struct for associating masks with characters.
 * Could be done without this by deriving the character from the mask
 * but this (IMHO) makes the code simpler to understand.
 */
struct {
     unsigned int mask
     char c;
} masks[] =  { { A_MASK, 'a'} , { E_MASK, 'e'} };

void vowels_not_present (unsigned int vowels_mask)
{
    int ix;
    for (ix = 0; ix < sizeof(masks) / sizeof(masks[0]); ix++) {
        if (!(vowels_mask & masks[ix].mask)) {
            printf("vowel %c is not present\n", masks[ix].c);
        }
    }
}

Then in your main invoke the above function:

vowels_not_present(mn);
kaylum
  • 13,833
  • 2
  • 22
  • 31