void main (void) { pdata char buff[MAXLINE]; /*Required for Serial Interface*/ SCON = 0x52; /*Serial port configuration*/ TMOD = 0x20; TCON = 0x40; TH1 = 0xf3; /*2403 baudrate @12mhz*/ while(TRUE){ memset(buff, 0, sizeof(buff)); prtline("Enter Line: "); getline(buff); PROMPT; puts(buff); /*Reverse the characters in the line*/ /*Reverse the words in the line*/ rev_word(buff, buff + strlen(buff) - 2); reverse_words(buff); PROMPT; puts(buff); /*Convert the line to its original form*/ rev_word(buff, buff + strlen(buff) - 2); reverse_words(buff); /*Sort the characters in the words in alphabetical order*/ alph_sort_words(buff); PROMPT; puts(buff); } }
// //Finds words in the char *string. When a word is found, //reverse_words() will keep track of the starting and ending //position. We apply rev_word(), which //reverses a word // void reverse_words(char *str) { char *itor = str; char *start; bool start_set = FALSE; /*Has char *start been set*/ bool prev = FALSE; //Loop entire char *str, until we find the null plug. //When a word is found apply rev_word() while (*itor ) { /*New word found*/ if (!isspace(*itor)) { prev = TRUE; if(start_set == FALSE){ start = itor; start_set = TRUE; } ++itor; /*Skip whitespace */ }else if (isspace(*itor) && prev == FALSE) { ++itor; continue; /*We are at the end of a word. Apply operation*/ }else if (isspace(*itor) && prev == TRUE) { rev_word(start, --itor); ++itor; prev = FALSE; start_set = FALSE; } } }
/* prints out info table as a final step of the program */ void print_table(char table[][21], int nwords, int numb[]) { int a; char srev[21]; printf(" \n\n Here is list of SORTED and then REVERSED words : \n\n"); printf("****************************************************\n"); printf("* %-3s * %-21s * %-8s * %-7s *\n","#","Reversed Word","#Ocured","Length"); printf("****************************************************\n"); for (a=0;a<nwords;a++) printf ("* %-3d * %-21s * %-8d * %-7d *\n",a+1,rev_word(table[a],srev),numb[a],strlen(table[a])); printf("****************************************************\n"); }
void rev_text_2(char *text) { char tmp; unsigned i, j, len; len = strlen(text); for(i=0; i < (len/2); i++) { tmp = text[i]; text[i] = text[len-i-1]; text[len-i-1] = tmp; } j = 0; for(i=0; i < len; i++) { if( text[i] == ' ') { rev_word(text, j, i-1); print_word(text,j, i-1); j = i+1; } } print_word("\n", 0, 1); }