I am having a hard time converting this do while logic into a while loop. Can anyone help?
Here, I am just reading the 1st character and checking if it is a space or a EOF. If its a space you add a space. For EOF you do not need to add the space, but I did not want to handle it with another if condition.
If is not eof or a space you add the char in morse buffer and then read the char with a function to morse buffer and the function returns the char decoded which gets appended to word.
The problem is I do not want to have a if condition and then break the loop. I want while(some condition) not while(true)
#include "headers/main.h"
/* Global variables */
char morse_buff[MORSE_BUFFER_SIZE];
int morse_buff_idx = 0;
/* local functions */
static char read_morse_return_char(FILE *morse_file, struct hsearch_data *hashtable) {
int read_char;
do {
read_char = fgetc(morse_file);
if (read_char == ' ' || read_char == '\n') {
morse_buff[morse_buff_idx] = '\0';
morse_buff_idx = 0;
ENTRY item = {.key = morse_buff};
return morse_char_to_roman_char(hashtable,item);
} else {
morse_buff[morse_buff_idx ] = read_char;
}
}while(true);
}
/* Functions */
void read_morse_display_word(FILE *morse_file, struct hsearch_data *hashtable) {
/* Ensure that file pointer is not equal to NULL */
assert("NULL pointer passed as morse file" && morse_file != NULL);
char word[WORD_BUFFER_SIZE];
int word_idx =0, read_char;
// possible while
do {
read_char = fgetc(morse_file);
if (read_char == EOF || read_char == ' ') {
/* There will be an extra space in the word buffer after the the last char that occurs at EOF.
* Since the display is terminal it will not matter. Therefore, there is no
* extra handing for this case during EOF.
*/
word[word_idx ] = ' '; /* Append space */
word[word_idx] = '\0'; /* Append null just to be safe */
word_idx = 0; /* Reset index to zero */
printf("%s", word); /* Print the resultant word */
if(read_char == EOF) {
break; /* Break at end of file */
}
fgetc(morse_file); /* Read the next space and ignore it */
} else {
morse_buff[morse_buff_idx ] = read_char; /* append char in morse_buff */
word[word_idx ] = read_morse_return_char(morse_file,hashtable); /* append decoded char in word buff */
}
}while(true);
printf("\n");
}
Source Code: Nothing great. Link: https://gitlab.com/DirtyVoid/computer_systems/-/tree/main/A4:MorseCodeDecoding/MorseCodeDecoder/src
CodePudding user response:
It's "fiddley", trying to do different manipulations on-the-fly, but I believe this implements what the OP code does but without if/else and break confabulations.
This can only be theoretical as the code snippet lacks input, output and data definitions. Cannot test this.
static char read_morse_return_char(FILE *morse_file, struct hsearch_data *hashtable) {
int read_char;
while( ( read_char = fgetc( morse_file ) ) != ' ' && read_char != '\n' )
morse_buff[morse_buff_idx ] = read_char;
morse_buff[morse_buff_idx] = '\0';
morse_buff_idx = 0;
ENTRY item = {.key = morse_buff};
return morse_char_to_roman_char(hashtable,item);
}
void read_morse_display_word(FILE *morse_file, struct hsearch_data *hashtable) {
assert("NULL pointer passed as morse file" && morse_file != NULL);
char word[WORD_BUFFER_SIZE];
int word_idx =0, read_char;
do {
while( ( read_char = fgetc( morse_file ) ) != ' ' && read_char != EOF ) {
morse_buff[morse_buff_idx ] = read_char; /* append char in morse_buff */
word[word_idx ] = read_morse_return_char(morse_file,hashtable); /* append decoded char in word buff */
}
word[word_idx ] = ' '; /* Append space */
word[word_idx] = '\0'; /* Append null just to be safe */
word_idx = 0; /* Reset index to zero */
printf("%s", word); /* Print the resultant word */
} while( read_char != EOF && fgetc(morse_file) != EOF );
printf("\n");
}