/**************************************************************/
/* bobo */
/* 20230212 */
/* English Passage Section Inverter */
/**************************************************************/
/* Includes */
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
/* Defines */
#define SIZE_PASSAGE 1000
#define SIZE_WORD 20
#define NUM_WORD (SIZE_PASSAGE / SIZE_WORD)
#define FLAG_INCOMPLETE 0
#define FLAG_COMPLETE 1
#define FLAG_ERROR 2
#define FLAG_START_NEG 0
#define FLAG_START_POS 1
/* Structures */
typedef struct {
int order;
char word[SIZE_WORD];
int start;
int end;
} passage_word_t;
/* Prototypes */
int passage_word_init(passage_word_t*, int);
int passage_word_divide(passage_word_t*, int, char*, int);
int passage_word_generate(char*, char*, passage_word_t*, int, int, int);
/* Globals */
/* Main Function*/
int main() {
/* Exploere the C World */
/* Variables */
char in_char_passage[SIZE_PASSAGE] = { 0 }, out_char_passage[SIZE_PASSAGE] = { 0 };
int in_int_invert_stt = 0, in_int_invert_stp = 0, indx_word = 0;
passage_word_t inter_struct_words[NUM_WORD] = { 0 };
/* Step 01. Input */
/* Step 01.01. Fetch Input */
fgets(in_char_passage, SIZE_PASSAGE, stdin);
scanf_s("%d", &in_int_invert_stt);
scanf_s("%d", &in_int_invert_stp);
/* Step 02. Process */
/* Step 02.01. Divide input string to words*/
passage_word_divide(inter_struct_words, NUM_WORD, in_char_passage, SIZE_PASSAGE);
/* Step 02.02. Determine word order */
/* Step 02.03. Gnerate new sentence */
//strcpy_s(out_char_passage, sizeof(out_char_passage), in_char_passage);
passage_word_generate(out_char_passage, in_char_passage, inter_struct_words, \
NUM_WORD, in_int_invert_stt, in_int_invert_stp);
/* Step 03. Output */
/*
* fputs(out_char_passage, stdout);
*/
printf("%s", out_char_passage);
/*
* printf("%d \n", in_int_invert_stt);
* printf("%d \n", in_int_invert_stp);
*/
/*
for (indx_word = 0; indx_word < NUM_WORD; indx_word++) {
if (inter_struct_words[indx_word].start != inter_struct_words[indx_word].end) {
printf("%s \n", inter_struct_words[indx_word].word);
}
else {
}
}
*/
/* Step 03.01. Output */
/* Return */
return 0;
}
/* Function Implementation */
/* Initialize */
int
passage_word_init(passage_word_t* in_arr_word, int in_num_word) {
/* Variables */
int index_word = 0;
int index_element = 0;
/* Initialize */
for (index_word = 0; index_word < in_num_word; index_word++) {
in_arr_word->order = index_word;
in_arr_word->start = 0;
in_arr_word->end = 0;
for (index_element = 0; index_element < SIZE_WORD; index_element++) {
in_arr_word->word[index_element] = '\0';
}
}
/* Return */
return 0;
}
/* Divide */
int
passage_word_divide(passage_word_t* out_arr_word, int in_size_arr, char* in_str_passage, \
int in_num_char)
{
/* Variables */
/* psg = passage, wd = word */
int index_psg_char = 0, index_wd = 0, index_wd_char = 0, num_wd_char = 0, sts_return = 0;
bool flag_stt = false;
/* Check character */
for (index_psg_char = 0; index_psg_char < in_num_char; index_psg_char++) {
if (in_str_passage[index_psg_char] != ' ' && in_str_passage[index_psg_char] != '\0' \
&& in_str_passage[index_psg_char] != '\n') {
/* Not space, analyze */
if (flag_stt == true) {
continue;
}
else {
out_arr_word[index_wd].order = index_wd;
out_arr_word[index_wd].start = index_psg_char;
flag_stt = true;
}
}
else {
/* Space, analyse */
if (flag_stt == false) {
continue;
}
else {
/* End - start = 1 if a word is 1-character-long */
out_arr_word[index_wd].end = index_psg_char;
num_wd_char = out_arr_word[index_wd].end - out_arr_word[index_wd].start;
if (num_wd_char > SIZE_WORD) {
/* Word length out of range */
sts_return = FLAG_ERROR;
break;
}
else {
/* Do nothing */
}
for (index_wd_char = 0; index_wd_char < num_wd_char; index_wd_char++) {
out_arr_word[index_wd].word[index_wd_char] = in_str_passage[\
out_arr_word[index_wd].start + index_wd_char];
}
index_wd++;
flag_stt = false;
}
}
}
return sts_return;
}
/* Generate */
int passage_word_generate(char* out_passage, char* in_passage, passage_word_t* in_word, \
int in_word_num, int in_word_order_stt, int in_word_order_stp) {
/* Variables */
/* psg = passage, wd = word */
int index_wd = 0, index_word_taken = 0, num_wd_valid = 0, index_wd_char = 0, size_wd = 0;
int index_psg_wd_out = 0, index_psg_wd_in = 0;
int index_wd_stt = 0, index_wd_stp = 0, index_wd_swap = 0;
/* Process */
/* Determine the number of words */
for (index_wd = 0; index_wd < in_word_num; index_wd++) {
if (in_word[index_wd].start != in_word[index_wd].end) {
num_wd_valid++;
}
else {
/* Do nothing */
}
}
index_wd_stt = in_word_order_stt;
index_wd_stp = in_word_order_stp;
/* Error process */
if (index_wd_stt > num_wd_valid - 1) {
/* Error */
index_wd_stt = num_wd_valid - 1;
}
else {
}
if (index_wd_stp > num_wd_valid - 1) {
/* Error */
index_wd_stp = num_wd_valid - 1;
}
else {
}
if (index_wd_stt > index_wd_stp) {
index_wd_swap = index_wd_stt;
index_wd_stt = index_wd_stp;
index_wd_stp = index_wd_swap;
}
else {
}
/* Determine order */
for (index_wd = 0; index_wd < num_wd_valid; index_wd++) {
if (index_wd < index_wd_stt || index_wd > index_wd_stp) {
/* Normal output */
index_word_taken = index_wd;
size_wd = in_word[index_word_taken].end - in_word[index_word_taken].start;
index_psg_wd_in = in_word[index_word_taken].start;
for (index_wd_char = 0; index_wd_char < size_wd; index_wd_char++) {
out_passage[index_psg_wd_out] = in_passage[index_psg_wd_in];
index_psg_wd_out++;
index_psg_wd_in++;
}
}
else {
/* Invert order */
index_word_taken = index_wd_stt + index_wd_stp - index_wd;
size_wd = in_word[index_word_taken].end - in_word[index_word_taken].start;
index_psg_wd_in = in_word[index_word_taken].start;
for (index_wd_char = 0; index_wd_char < size_wd; index_wd_char++) {
out_passage[index_psg_wd_out] = in_passage[index_psg_wd_in];
index_psg_wd_out++;
index_psg_wd_in++;
}
}
if (index_wd == num_wd_valid - 1) {
/* Do not add space here */
}
else {
/* Add space here */
out_passage[index_psg_wd_out] = ' ';
index_psg_wd_out++;
}
}
/* Return */
return 0;
}
/* End of File */