int main(int argC, char *argV[]) { /* Allocate memory and check if okay */ char *instring = (char*) malloc (MAX_STRING_SIZE); if (instring == NULL) { printf ("No memory\n"); return 1; } /* Ask user for instring */ printf("Enter string to be disemvoweled: "); /* Get the instring, with size limit */ fgets (instring, MAX_STRING_SIZE, stdin); /* Remove trailing newline, if there */ if ((strlen(instring)>0) && (instring[strlen (instring) - 1] == '\n')) instring[strlen (instring) - 1] = '\0'; /* Call disemvowel function */ disemvowel(instring); /* Free memory and exit */ free (instring); return 0; }
int main() try { cout << "Enter input file name: "; string iname; cin >> iname; ifstream ifs(iname.c_str()); if (!ifs) error("can't open input file ",iname); cout << "Enter output file name: "; string oname; cin >> oname; ofstream ofs(oname.c_str()); if (!ofs) error("can't open output file ",oname); char ch; string s; while (ifs.get(ch)) { if (isgraph(ch)) { ifs.unget(); ifs >> s; if (allvowels(s)) ifs.get(ch); // don't print s, skip next space else { disemvowel(s); ofs << s; } } else ofs << ch; } } catch (exception& e) { cerr << "exception: " << e.what() << endl; } catch (...) { cerr << "exception\n"; }
int main(int argc, char *argv[]) { FILE *inputFile; FILE *outputFile; // FILE *file1 = fopen( argv[1], "r" ); if (argc == 1){ //stdin to stdout case inputFile = stdin; outputFile = stdout; } else if(argc == 2){ //file to stdout case inputFile = fopen( argv[1], "r" ); outputFile = stdout; } else if(argc == 3){ //file to file case inputFile = fopen( argv[1], "r" ); outputFile = fopen( argv[2], "w" ); } else { printf("More than two arguments were present."); exit(1); } disemvowel(inputFile, outputFile); return 0; }
int main(int argc, char *argv[]) { FILE *inputFile; FILE *outputFile; if(argc == 1) { disemvowel(stdin, stdout); } else if (argc == 2){ inputFile = fopen(argv[1], "r"); disemvowel(inputFile, stdout); } else if (argc == 3){ inputFile = fopen(argv[1], "r"); outputFile = fopen(argv[2], "w"); disemvowel(inputFile, outputFile); } return 0; }
int main(int argc, char* argv[]) { char* line; int size; size = 100; line = (char*) malloc (size + 1); while (getline(&line, &size, stdin) > 0) { printf("%s\n", disemvowel(line)); } free(line); }
int main(int argc, char *argv[]){ FILE *inputFile; FILE *outputFile; int num_cons; if(argc == 1){ //grabs from command line inputFile = fopen("tempFile", "w"); inputFile = stdin; outputFile = fopen("tempOut", "w+"); } else if(argc == 2){ //reads from file to stdout inputFile = fopen(argv[1], "r"); outputFile = fopen("tempOut", "w+"); } else if(argc == 3){ //reads to file, writes to file inputFile = fopen(argv[1], "r"); outputFile = fopen(argv[2], "w"); } else { printf("Incorrect number arguments. You used %d and must have 2 or less", argc - 1); return 1; } num_cons = disemvowel(inputFile, outputFile); if(argc == 2 || argc == 1){ //remove files and prints to standard out char* paragraph = malloc(BUF_SIZE * sizeof(char)); fseek(outputFile, 0, SEEK_SET); fread(paragraph, sizeof(char), num_cons - 1, outputFile); printf("%s\n", paragraph); remove("tempFile"); remove("tempOut"); free(paragraph); } fclose(inputFile); fclose(outputFile); return 0; }
int main(int argc, char **argv) { FILE *inputFile; FILE *outputFile; /* Code that processes the command line arguments and sets up inputFile and outputFile */ if(argc == 1) { inputFile = stdin; outputFile = stdout; } else if (argc == 2) { inputFile = fopen(argv[1], "r"); outputFile = stdout; } else if (argc == 3) { inputFile = fopen(argv[1], "r"); outputFile = fopen(argv[2], "w+"); } disemvowel(inputFile, outputFile); return 0; }
void urduanalyse(token *word, unichar tagset[TAGSETSIZE][TAGLENGTH]) { int i; unichar tempword[WORDLENGTH]; unichar respA30[USERLENGTH] = { 0x0041, 0x0033, 0x0030, 0x0000 }; unichar respA70[USERLENGTH] = { 0x0041, 0x0037, 0x0030, 0x0000 }; ustrcpy(tempword, word->wordform); disemvowel(tempword); for ( i = 0 ; tempword[i+1] ; i++ ) /* scroll [i] to point at final char in string (prior to NULL) */ ; switch (tempword[i]) { case 0x0627: switch (tempword[i-1]) { case 0x062a: /* -tA : VVTM1N */ ustrcpy(word->tag[0], tagset[VVTM1N]); break; case 0x0646: /* -nA : VVNM1N */ ustrcpy(word->tag[0], tagset[VVNM1N]); break; case 0x067e: /* -pA : NNMM1N */ ustrcpy(word->tag[0], tagset[NNMM1N]); break; case 0x06cc: /* -YA : NNMF1N, NNMF1O, NNMF1V, VVYM1N */ ustrcpy(word->tag[0], tagset[NNMF1N]); ustrcpy(word->tag[1], tagset[NNMF1O]); ustrcpy(word->tag[2], tagset[NNMF1V]); ustrcpy(word->tag[3], tagset[VVYM1N]); break; default: /* -A with no other clues: NNMM1N, JJM1N, VVYM1N */ ustrcpy(word->tag[0], tagset[NNMM1N]); ustrcpy(word->tag[1], tagset[JJM1N]); ustrcpy(word->tag[2], tagset[VVYM1N]); break; } break; case 0x062a: if (tempword[i-1] == 0x06cc) { /* -Yt: apply NNUF1N, NNUF1O, NNUF1V */ ustrcpy(word->tag[0], tagset[NNUF1N]); ustrcpy(word->tag[1], tagset[NNUF1O]); ustrcpy(word->tag[2], tagset[NNUF1V]); } break; case 0x0646: if((tempword[i-1] == 0x0627 && tempword[i-2] == 0x062a && tempword[i-3] == 0x0633 ) || (tempword[i-1] == 0x067e ) ) { /* -stAn or -pn: apply NNUM1N, NNUM1O, NNUM1V, NNUM2N */ ustrcpy(word->tag[0], tagset[NNUM1N]); ustrcpy(word->tag[1], tagset[NNUM1O]); ustrcpy(word->tag[2], tagset[NNUM1V]); ustrcpy(word->tag[3], tagset[NNUM2N]); } break; case 0x0648: if (tempword[i-1] == 0x062a && tempword[i-2] == 0x06cc ) { /* -YtV : NNUF2V */ ustrcpy(word->tag[0], tagset[NNUF2V]); break; } if((tempword[i-1] == 0x0646 && tempword[i-2] == 0x0627 && tempword[i-3] == 0x062a && tempword[i-4] == 0x0633 ) || (tempword[i-1] == 0x0646 && tempword[i-2] == 0x067e ) ) { /* -stAnV or -pnV: apply NNUM2V */ ustrcpy(word->tag[0], tagset[NNUM2V]); break; } if((tempword[i-1] == 0x0679 && tempword[i-2] == 0x0648 && tempword[i-3] == 0x0627 ) || (tempword[i-1] == 0x0679 && tempword[i-2] == 0x06c1 && tempword[i-3] == 0x0627 ) || (tempword[i-1] == 0x06c1 && tempword[i-2] == 0x0627 && tempword[i-3] == 0x06af ) ) { /* -AVTV, -AhTV, or -gAHV : NNUF2V */ ustrcpy(word->tag[0], tagset[NNUF2V]); break; } /* "else": just -V : therefore assign NNMM2V, NNMF2V, NNUM2V, NNUF2V, VVST2, VVIT2 */ ustrcpy(word->tag[0], tagset[NNMM2V]); ustrcpy(word->tag[1], tagset[NNMF2V]); ustrcpy(word->tag[2], tagset[NNUM2V]); ustrcpy(word->tag[3], tagset[NNUF2V]); ustrcpy(word->tag[4], tagset[VVST2]); ustrcpy(word->tag[5], tagset[VVIT2]); break; case 0x064b: /* word ends in tanvYn, therefore assign RR */ ustrcpy(word->tag[0], tagset[RR]); break; case 0x0679: if((tempword[i-1] == 0x0648 && tempword[i-2] == 0x0627 ) || (tempword[i-1] == 0x06c1 && tempword[i-2] == 0x0627 ) ) { /* -AVT or -AhT : NNUF1N, NNUF1O, NNUF1V */ ustrcpy(word->tag[0], tagset[NNUF1N]); ustrcpy(word->tag[1], tagset[NNUF1O]); ustrcpy(word->tag[2], tagset[NNUF1V]); } break; /**************************************************************************************/ /* BEGIN THE ONES WITH ~ AS THEIR LAST CHAR */ case 0x06ba: switch (tempword[i-1]) { case 0x0627: /* -A~ */ if (tempword[i-2] == 0x0648) { /* -VA~ : assign JDNM1N */ ustrcpy(word->tag[0], tagset[JDNM1N]); } else if (tempword[i-2] == 0x06cc) { /* -YA~ : assign NNMF2N */ ustrcpy(word->tag[0], tagset[NNMF2N]); } break; case 0x0648: /* -V~ */ if((tempword[i-2] == 0x0646 && tempword[i-3] == 0x0627 && tempword[i-4] == 0x062a && tempword[i-5] == 0x0633 ) || (tempword[i-2] == 0x0646 && tempword[i-3] == 0x067e ) ) { /* -stAnV~ or -pnV~ : assign NNUM2O */ ustrcpy(word->tag[0], tagset[NNUM2O]); break; } if((tempword[i-2] == 0x062a && tempword[i-3] == 0x06cc ) || (tempword[i-2] == 0x0679 && tempword[i-3] == 0x0648 && tempword[i-4] == 0x0627 ) || (tempword[i-2] == 0x0679 && tempword[i-3] == 0x06c1 && tempword[i-4] == 0x0627 ) || (tempword[i-2] == 0x06c1 && tempword[i-3] == 0x0627 && tempword[i-4] == 0x06af ) ) { /* -YtV~, -AVTV~, -AhTV~ or -gAhV~ : assign NNUF2O */ ustrcpy(word->tag[0], tagset[NNUF2O]); break; } if (tempword[i-2] == 0x06cc) { /* -YV~ : assign NNMF2O */ ustrcpy(word->tag[0], tagset[NNMF2O]); break; } /* "else" it's just -V~ : assign NNMM2O, NNUM2O, NNUF2O, VVSM1 */ ustrcpy(word->tag[0], tagset[NNMM2O]); ustrcpy(word->tag[1], tagset[NNUM2O]); ustrcpy(word->tag[2], tagset[NNUF2O]); ustrcpy(word->tag[3], tagset[VVSM1]); break; case 0x06cc: /* -Y~ */ if (tempword[i-2] == 0x062a && tempword[i-3] == 0x06cc ) { /* -YtY~ : assign NNUF2N */ ustrcpy(word->tag[0], tagset[NNUF2N]); break; } if (tempword[i-2] == 0x062a) { /* -tY~ : assign VVTF2N */ ustrcpy(word->tag[0], tagset[VVTF2N]); break; } if (tempword[i-2] == 0x0648) { /* -VY~ : assign JDNM1O JDNM2N JDNM2O JDNF1N JDNF1O JDNF2N JDNF2O */ ustrcpy(word->tag[0], tagset[JDNM1O]); ustrcpy(word->tag[1], tagset[JDNM2N]); ustrcpy(word->tag[2], tagset[JDNM2O]); ustrcpy(word->tag[3], tagset[JDNF1N]); ustrcpy(word->tag[4], tagset[JDNF1O]); ustrcpy(word->tag[5], tagset[JDNF2N]); ustrcpy(word->tag[6], tagset[JDNF2O]); break; } if((tempword[i-2] == 0x0679 && tempword[i-3] == 0x0648 && tempword[i-4] == 0x0627 ) || (tempword[i-2] == 0x0679 && tempword[i-3] == 0x06c1 && tempword[i-4] == 0x0627 ) || (tempword[i-2] == 0x06c1 && tempword[i-3] == 0x0627 && tempword[i-4] == 0x06af ) ) { /* -AVTY~, -AhTY~ or -gAhY~: assign NNUF2N */ ustrcpy(word->tag[0], tagset[NNUF2N]); break; } /* it's just -Y~, so assign: NNUF2N, VVSM2, VVSV2, VVYF2N */ ustrcpy(word->tag[0], tagset[NNUF2N]); ustrcpy(word->tag[1], tagset[VVSM2]); ustrcpy(word->tag[2], tagset[VVSV2]); ustrcpy(word->tag[3], tagset[VVYF2N]); break; default: /* no pattern for words just randomly ending in nVni GVnA */ break; } break; /* END THE ONES WITH ~ AS THEIR LAST CHAR */ /**************************************************************************************/ case 0x06c1: if (tempword[i-1] == 0x0627 && tempword[i-2] == 0x06af ) { /* -gAh : assign NNUF1N, NNUF1O, NNUF1V */ ustrcpy(word->tag[0], tagset[NNUF1N]); ustrcpy(word->tag[1], tagset[NNUF1O]); ustrcpy(word->tag[2], tagset[NNUF1V]); } else /* theoretically check for -Yh, but this is masc marked, just like -h. */ /* therefore, have found -h or -Yh : assign NNMM1N */ ustrcpy(word->tag[0], tagset[NNMM1N]); break; case 0x06cc: if (tempword[i-1] == 0x062a) { /* -tY : assign VVTF1N, VVTF1O, VVTF2N, VVTF2O */ ustrcpy(word->tag[0], tagset[VVTF1N]); ustrcpy(word->tag[1], tagset[VVTF1O]); ustrcpy(word->tag[2], tagset[VVTF2N]); ustrcpy(word->tag[3], tagset[VVTF2O]); break; } if (tempword[i-1] == 0x0646) { /* -nY : assign VVNF1, VVNF2 */ ustrcpy(word->tag[0], tagset[VVNF1]); ustrcpy(word->tag[1], tagset[VVNF2]); break; } /* "else": just -Y : therefore assign NNMF1N, NNMF1O, NNMF1V, JJF1N, JJF1O, JJF2N, */ /* JJF2O, VVYF1N, VVYF1O, VVYF2N, VVYF2O */ ustrcpy(word->tag[0], tagset[NNMF1N]); ustrcpy(word->tag[1], tagset[NNMF1O]); ustrcpy(word->tag[2], tagset[NNMF1V]); ustrcpy(word->tag[3], tagset[JJF1N]); ustrcpy(word->tag[4], tagset[JJF1O]); ustrcpy(word->tag[5], tagset[JJF2N]); ustrcpy(word->tag[6], tagset[JJF2O]); ustrcpy(word->tag[7], tagset[VVYF1N]); ustrcpy(word->tag[8], tagset[VVYF1O]); ustrcpy(word->tag[9], tagset[VVYF2N]); /* F2N without ~ ??????? */ ustrcpy(word->tag[10], tagset[VVYF2O]); break; case 0x06d2: switch (tempword[i-1]) { case 0x0626: if (tempword[i-2] == 0x0627) { /* -A'E : assign NNUF1N, NNUF1O, NNUF1V */ ustrcpy(word->tag[0], tagset[NNUF1N]); ustrcpy(word->tag[1], tagset[NNUF1O]); ustrcpy(word->tag[2], tagset[NNUF1V]); } else { /* -'E : assign VVIA, NNMM1O, NNMM1V, NNMM2N */ ustrcpy(word->tag[0], tagset[VVIA]); ustrcpy(word->tag[1], tagset[NNMM1O]); ustrcpy(word->tag[2], tagset[NNMM1V]); ustrcpy(word->tag[3], tagset[NNMM2N]); } break; case 0x062a: /* -tE : assign VVTM1O, VVTM2N, VVTM2O */ ustrcpy(word->tag[0], tagset[VVTM1O]); ustrcpy(word->tag[1], tagset[VVTM2N]); ustrcpy(word->tag[2], tagset[VVTM2O]); break; case 0x0646: /* -nE : assign VVNM1O, VVNM2 */ ustrcpy(word->tag[0], tagset[VVNM1O]); ustrcpy(word->tag[1], tagset[VVNM2]); break; case 0x06cc: if (tempword[i-2] == 0x0626) { /* -'YE : assign VVIA */ ustrcpy(word->tag[0], tagset[VVIA]); break; } else ; /* drop-thru here purposeful so the ELSE of the previous IF is "GOTO DEFAULT" */ default: /* it's just -E : assign NNMM1O, NNMM1V, NNMM2N, JJM1O, JJM2N, JJM2O, */ /* VVYM1O, VVYM2N, VVYM2O, VVST1, VVSV1, RRJ */ ustrcpy(word->tag[0], tagset[NNMM1O]); ustrcpy(word->tag[1], tagset[NNMM1V]); ustrcpy(word->tag[2], tagset[NNMM2N]); ustrcpy(word->tag[3], tagset[JJM1O]); ustrcpy(word->tag[4], tagset[JJM2N]); ustrcpy(word->tag[5], tagset[JJM2O]); ustrcpy(word->tag[6], tagset[VVYM1O]); ustrcpy(word->tag[7], tagset[VVYM2N]); ustrcpy(word->tag[8], tagset[VVYM2O]); ustrcpy(word->tag[9], tagset[VVST1]); ustrcpy(word->tag[10], tagset[VVSV1]); ustrcpy(word->tag[11], tagset[RRJ]); break; } break; default: break; } /* if a tag has been given, assign resptag A30 and return */ /* THIS MEANS ALL "AL-" WORDS THAT AREN'T AL/NNU... MUST BE IN LEXICON !!! */ if (word->tag[0][0]) { ustrcpy(word->resp, respA30); return; } /* check for "al" */ if ( word->wordform[0] == 0x0627 && word->wordform[1] == 0x0644 ) { /* assign SPAL, add a resptag (A70) and return */ ustrcpy(word->tag[0], tagset[SPAL]); ustrcpy(word->resp, respA70); return; } /* if even looking for "al" has failed, give up */ }
void urdutag(entry *lexicon, token *word) { int i, j; entry *rightentry; unichar tempword[WORDLENGTH]; unichar respA10[USERLENGTH] = { 0x0041, 0x0031, 0x0030, 0x0000 }; unichar respA50[USERLENGTH] = { 0x0041, 0x0035, 0x0030, 0x0000 }; unichar respA90[USERLENGTH] = { 0x0041, 0x0039, 0x0030, 0x0000 }; static char before = FALSE; static char *c_pointers_tagset[TAGSETSIZE]; static unichar tagset[TAGSETSIZE][TAGLENGTH]; if ( ! before ) { /* create the tagset strings in an array */ initialise_tagset_pointer_array(c_pointers_tagset); for ( i = 0 ; i < TAGSETSIZE ; i++ ) { for ( j = 0 ; j < TAGLENGTH ; j++ ) { tagset[i][j] = *(c_pointers_tagset[i]+j); if ( ! tagset[i][j] ) break; } } before = TRUE; } /* remove vowels from a temporary copy of the word for lookup */ ustrcpy(tempword, word->wordform); disemvowel(tempword); /* if the word has the special al-tag, do lexical lookup */ /* if no joy, assign the set of words that can follow al & return */ /* this is for when it is called by do_the_splits, rather than urdutag */ // do a concordance of AL and see what does follow it... improve this set if(ustrident(word->tag[0], tagset[SPAL])) { clear_tags(word); if (rightentry = find_entry(tempword, lexicon) ) assign_tags(word, rightentry); else { ustrcpy(word->tag[0], tagset[JJU]); ustrcpy(word->tag[1], tagset[NNUM1N]); ustrcpy(word->tag[2], tagset[NNUM1O]); ustrcpy(word->tag[3], tagset[NNUM2N]); ustrcpy(word->tag[4], tagset[NNUM2O]); ustrcpy(word->tag[5], tagset[NNUF1N]); ustrcpy(word->tag[6], tagset[NNUF1O]); ustrcpy(word->tag[7], tagset[NNUF2N]); ustrcpy(word->tag[8], tagset[NNUF2N]); } /* do not change the resptag */ return; } /* look it up in the lexicon - if found, assign code and return */ if (rightentry = find_entry(tempword, lexicon) ) { assign_tags(word, rightentry); /* What if a special-process tag is returned from lex? */ /* Do nothing: split signal is handled by calling function */ ustrcpy(word->resp, respA10); return; } /* analyse chartypes */ /* if all chars are numerals (Indo-Arabic, Arabic or Euro), JDNU. */ ustrcpy(word->tag[0], tagset[JDNU]); for ( i = 0 ; i < WORDLENGTH && word->wordform[i] ; i++ ) { if ( word->wordform[i] < 0x0030 || (word->wordform[i] > 0x0039 && word->wordform[i] < 0x0660) || (word->wordform[i] > 0x0669 && word->wordform[i] < 0x06f0) || word->wordform[i] > 0x06f9 ) word->tag[0][0] = 0; } /* if JDNU was not wiped, return with resp-tag A50 */ if (word->tag[0][0]) { ustrcpy(word->resp, respA50); return; } /* if any characters are non-Arabic, FX */ for ( i = 0 ; i < WORDLENGTH && word->wordform[i] ; i++ ) { if ( word->wordform[i] < 0x0600 || word->wordform[i] > 0x06ff ) ustrcpy(word->tag[0], tagset[FX]); } /* if a tag has been assigned, return with resp-tag A50 */ if (word->tag[0][0]) { ustrcpy(word->resp, respA50); return; } /* analyse morphology: separate func which operates directly on the token structure */ urduanalyse(word, tagset); if (word->tag[0][0]) /* i.e. if some tag or other has been assigned */ /* don't add a resp tag - urduanalyse does this */ return; /* if all else fails and no tag has been given, apply the default set of tags */ ustrcpy(word->tag[0], tagset[JJU]); ustrcpy(word->tag[1], tagset[NNUM1N]); ustrcpy(word->tag[2], tagset[NNUM1O]); ustrcpy(word->tag[3], tagset[NNUM1V]); ustrcpy(word->tag[4], tagset[NNUM2N]); ustrcpy(word->tag[5], tagset[NNUF1N]); ustrcpy(word->tag[6], tagset[NNUF1O]); ustrcpy(word->tag[7], tagset[NNUF1V]); ustrcpy(word->tag[8], tagset[RR]); ustrcpy(word->tag[9], tagset[VV0]); ustrcpy(word->tag[10], tagset[VVIT1]); /* assign the correct resptag */ ustrcpy(word->resp, respA90); }
int main(int argc, char *argv[]) { // Variables int sockfd; struct addrinfo hints, *servinfo, *p; int status; int numbytes; char *my_port; struct sockaddr_storage their_addr; char buf[MAX_PACKET_LEN]; // Make the buffer big enough for a full 1Kb message + 5 bytes for TML, etc. socklen_t addr_len; memset(&hints, 0, sizeof hints); hints.ai_family = AF_UNSPEC; // set to AF_INIT to force IPv4 hints.ai_socktype = SOCK_DGRAM; hints.ai_flags = AI_PASSIVE; // use my IP // Check to make sure the command line arguments are valid if (argc != 3) { fprintf(stderr, "Usage Error: Should be 2 arguments: 'client' and the port number.\n"); exit(1); } // Get the port number from the command line my_port = argv[2]; if (DEBUG) { printf("DEBUG: Port number: %s\n", my_port); } printf("Starting Server... to stop, press 'Ctrl + c'\n"); while(1) { // 1. getaddrinfo if ((status = getaddrinfo(NULL, my_port, &hints, // points to a struct with info we have already filled in &servinfo)) != 0) // servinfo: A linked list of results { fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(status)); return 1; } // Loop through all the results and bind to the first we can for (p = servinfo; p != NULL; p = p->ai_next) { // 2. socket if ((sockfd = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) == -1) { perror("Socket Error!"); continue; } // 3. bind if (bind(sockfd, p->ai_addr, p->ai_addrlen) == -1) { close(sockfd); perror("Bind Error!"); continue; } break; } if (p == NULL) { fprintf(stderr, "Failed to bind socket\n"); return 2; } freeaddrinfo(servinfo); // Call this when we are done with the struct "servinfo" if (DEBUG) { printf("Binding complete, waiting to recvfrom...\n"); } addr_len = sizeof their_addr; rx_packet packet_in; // 4. recvfrom // MAX_PACKET_LEN -1: To make room for '\0' if ((numbytes = recvfrom(sockfd, (char *) &packet_in, MAX_PACKET_LEN - 1, 0, (struct sockaddr *)&their_addr, &addr_len)) == -1) { perror("recvfrom"); exit(1); } printf("Packet Received!\n"); // Add the null to terminate the string packet_in.message[numbytes - 5] = '\0'; // numbytes - 5: To compensate for header if (DEBUG) { printf("struct.tml = %d\n", ntohs(packet_in.TML)); printf("struct.rid = %d\n", ntohs(packet_in.RID)); printf("struct.op = %d\n", packet_in.operation); printf("struct.message = %s\n", packet_in.message); } // Here we check what operation the client wanted. if (packet_in.operation == V_LENGTH) { if (DEBUG) { printf("Operation: vLength requested.\n"); printf("String to process: %s\n", packet_in.message); printf("The number of vowels in string '%s' is: %d\n", packet_in.message, count_vowels(packet_in.message)); } // Create packet that will be returned to the client tx_vLength packet_out_vLength; // Fill in the struct with TML, RID and vLength packet_out_vLength.TML = htons(((sizeof packet_out_vLength.TML) + (sizeof packet_out_vLength.RID) + (sizeof packet_out_vLength.vLength))); packet_out_vLength.RID = htons(ntohs(packet_in.RID)); packet_out_vLength.vLength = htons(count_vowels(packet_in.message)); if (DEBUG) { printf("packet_out_vLength.TML: %d\n", ntohs(packet_out_vLength.TML)); printf("packet_out_vLength.RID: %d\n", ntohs(packet_out_vLength.RID)); printf("packet_out_vLength.VLength: %d\n", ntohs(packet_out_vLength.vLength)); } if (DEBUG) { printf("Sending vLength packet to Client.\n"); } // 5. sendto if (sendto(sockfd, (char *)&packet_out_vLength, ntohs(packet_out_vLength.TML), 0, (struct sockaddr *)&their_addr, addr_len) == -1) { perror("sendto error"); exit(1); } } else if (packet_in.operation == DISEMVOWEL) { if (DEBUG) { printf("Operation: Disemvowelment requested.\n"); printf("String to process: %s\n", packet_in.message); printf("The string '%s' disemvowled is: %s\n", packet_in.message, disemvowel(packet_in.message)); } // Create packet that will be returned to the client tx_disVowel packet_out_disVowel; // Fill in the struct with TML, RID and disVowel string strcpy(packet_out_disVowel.message, disemvowel(packet_in.message)); // Need to do the strcpy first or the message size won't be correct. packet_out_disVowel.TML = htons((sizeof packet_out_disVowel.TML) + (sizeof packet_out_disVowel.RID) + (strlen(packet_out_disVowel.message))); packet_out_disVowel.RID = htons(ntohs(packet_in.RID)); if (DEBUG) { printf("packet_out_disVowel.TML: %d\n", ntohs(packet_out_disVowel.TML)); printf("packet_out_disVowel.RID: %d\n", ntohs(packet_out_disVowel.RID)); printf("packet_out_disVowel.message: %s\n", packet_out_disVowel.message); printf("strlen(packet_out_disVowel.message): %d\n", (int)strlen(packet_out_disVowel.message)); } if (DEBUG) { printf("Sending disVowel packet to Client.\n"); } // 5. sendto if (sendto(sockfd, (char *)&packet_out_disVowel, ntohs(packet_out_disVowel.TML), 0, (struct sockaddr *)&their_addr, addr_len) == -1) { perror("sendto error"); exit(1); } } // Error! else { fprintf(stderr, "Operation not reconized!.\n"); break; } printf("Responce Sent!\n"); if (DEBUG) { printf("Sending Echo...\n"); } close(sockfd); } return 0; }