Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

program to be written in C language Program 4 will prompt the user for one or tw

ID: 3870993 • Letter: P

Question

program to be written in C language

Program 4 will prompt the user for one or two token, space delimited, inputs of at most 20 characters. If the user provides more than 20 characters before a newline is reached print the provided error message. If the number of tokens is incorrect, print the appropriate error message. If the input is correct, print the appropriate token types. Prompt the user for input (and provide output) until the user provides a single STR token "quit" (case insensitive). The program should quit immediately without output. 123 ERROR! Incorrect number of tokens found 1234567890123456789012345 ERROR! Input string too long. some 1 STR INT tuig STR

Explanation / Answer

#include<stdio.h>
#include<string.h>
#include<stdlib.h>

int main(){
    char *line;
    char word1[20];
    char word2[20];
    size_t len = 0;
    ssize_t read = 0;
    int i;
    int count = 0;
    while (1) {
      printf(" enter 1 or 2 tokens ");
      read = getline(&line, &len, stdin);
       
      if (strlen(line) > 20){
        printf("ERROR! Input string too long.");
       
      }
      else {
         count = 0;
         for (i=0; i<strlen(line); i++){
            if (line[i] == ' ')
               count++;
         }
         if (count == 0){
          line[strlen(line)-1] = '';
          if (strcmp(line,"Quit") == 0)
             return 0;
         }
         if (count > 1){
          printf("ERROR! Incorrect number of tokens.");
        
         }
         else {
            sscanf(line, "%s %s",word1,word2);
            if (atoi(word1)== 0)
              printf("%s ","STR");
            else
              printf("%s ","INT");
            if (count == 1) {
            if (atoi(word2)== 0)
                printf("%s ","STR");
            else
               printf("%s ","INT");
         }
       }
      }
    }  


}