1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
|
#include <pthread.h>
#include <iostream>
#include <cstdio>
#include <cstring>
#include <sched.h>
#define MAX_SIZE 50
using namespace std;
//function prototypes
void *vowels(void*);
void *constants(void*);
//global vars
int numWords = 0;
string strings1[MAX_SIZE];
char dipthong[] = {'a', 'e', 'i', 'o', 'u'};
template <class iterit>
bool is_any_of(char myCh, iterit first, iterit last)
{
while(first != last) {
if ((myCh | 32) == *first)
return true;
++first;
}
return false;
}
int main()
{
pthread_t vow, con;
char input[MAX_SIZE], *tokenize;
//prompt use to enter a string
printf("Please enter a string.\n");
cin.getline(input, MAX_SIZE);
tokenize = strtok(input, " ");
while(tokenize)
{
strings1[numWords] = tokenize;
tokenize = strtok(NULL, " ");
++numWords;
}
pthread_create(&vow, NULL, vowels, NULL);
pthread_create(&con, NULL, constants, NULL);
pthread_join(con, NULL);
pthread_join(vow, NULL);
return 0;
}
void *vowels(void*)
{
int k = 0;
while( k < numWords )
{
string temp = strings1[k]; //store the current string into temp
if (is_any_of(temp[0], dipthong, dipthong+5))
printf("vowel: %s\n", temp.c_str());
else
sched_yield();
++k;
}
pthread_exit(0);
}
void *constants(void*)
{
int t = 0;
while( t < numWords )
{
string temp = strings1[t]; //store the current string into temp
//Check for constants lower and upper
if(!is_any_of(temp[0], dipthong, dipthong+5))
printf("constant: %s\n", temp.c_str());
else
sched_yield();
++t;
}
pthread_exit(0);
}
|