Unless the code must run endlessly, such as a program that operates a gas pump, an endless loop isn’t something you want. From last week’s Lesson, I crafted an endless loop to accept single-word input from the scanf() function to build a string. But no string is output because the loop never ends! It’s time to address this situation.
I often craft endless loops when the terminating condition is just too ugly to put into a while or for statement. For building a string, I could set an input size limit. After the limit is reached, the loop exits and the string is output. However, such a size limit lacks flexibility. So instead, I added two statements to the existing code to terminate the loop:
if( strcmp(b,"END")==0 )
break;
These lines are set immediately after the scanf() statement. The string input is compared with the text END
. When a match is found, the loop breaks and the string is output. The word “END” isn’t appended to the string (which is why input is checked immediately). Here is the full code:
2023_07_22-Lesson.c
#include <stdio.h> #include <stdlib.h> #include <string.h> #define SIZE 16 int main() { char *b,*s; /* allocate/initialize buffers */ b = malloc( SIZE * sizeof(char) ); /* input */ s = malloc( sizeof(char) ); /* string */ if( b==NULL || s==NULL ) { fprintf(stderr,"Memory allocation error\n"); exit(1); } /* initialize string storage */ *b = *s = '\0'; while(1) { /* fetch input */ printf("Word: "); scanf("%s",b); if( strcmp(b,"END")==0 ) break; /* copy the word */ /* add two: space and null char */ s = realloc(s,strlen(s) + strlen(b) + 2); if( s==NULL ) { fprintf(stderr,"Reallocation error\n"); exit(1); } strcat(s,b); strcat(s," "); } /* output results */ puts(s); return(0); }
Now the program runs — and terminates! — providing you type the word END (all caps) after the string of text:
Word: Hello
Word: there,
Word: blue
Word: ball!
Word: END
Hello there, blue ball!
Because the scanf() function devours stream input, you can type everything on a single line:
Word: Hello there, blue ball! END
Word: Word: Word: Word: Hello there, blue ball!
Alas, the program outputs the input prompt Word:
for each whitespace character input. Still, the string is properly built.
To fix the multiple prompts, I moved the prompting printf() statement before the endless while loop, and changed it to read "Type some text:"
. This update is found on GitHub. Here is the output from the updated program:
Type some text: This is some text END
This is some text
Another flaw I pointed out last week is that each word in the string is suffixed with a space character. This assumption means that the string is always terminated with a space and that spaces sandwich each word. A possible solution is to create a series of “secret” words to type that help format the text.
For example, in addition to END
, the user could type SP
to add a space or NL
for a newline. These words can be scanned for similar to END
in the current program. A better solution is to write a tokenizing function that translates special words into tokens or characters. I cover this update in next week’s Lesson.