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

please do not use handwriting !!! Program 5 will prompt the user for one or two

ID: 3846220 • Letter: P

Question

please do not use handwriting !!!

Program 5 will prompt the user for one or two token, space delimited, inputs of at most 65 characters. If the user inputs too many characters or an incorrect number of tokens the appropriate error messages should be printed. The program should prompt the user until the single STR token "quit" is entered. The program will only accept two token inputs of the type: STR INT and single token inputs of the type: STR, which should result in the token types being printed as before (Program 4). All other inputs should be rejected with the appropriate (provided) error message. > 1 2 3 ERROR! Incorrect number of tokens found. > 1 2 ERROR! Expected STR INT. > l ERROR! Expected STR. > Stuff STR > tuff 1 STR INT

Explanation / Answer

Here is the code and sample output. Please don't forget to rate the answer if it helped.

I was not sure if strtok() function has been taught to you. It can be used to get a token using a delimiter. Since I was not sure, I wrote a getNextToken() function.

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

/*function to count the number of tokens in the string*/
int countTokens(char str[])
{
int i = 0, count = 0;

for(i = 0; str[i] != ''; ++i)
{
if(str[i] == ' ')
count++;
}

/*count the last token, it will not have a space after it but will just terminate with . For example if a single token is present,
there will not even be a space anywhere*/
if(i > 0 && str[i-1]!=' ')
count++;

return count;
}

/*copies the token starting from start into token and returns the index of delimiter/end of string. The return value can be
used for getting next token */
int getNextToken(char str[], int start,char delimiter, char token[])
{
int i, j;
/*copy the characters starting at specified start location until delimiter or end of the string is found*/
for(i = start, j = 0; str[i] != ''; ++i, ++j)
{
if(str[i] == delimiter)
{
break;
}
else
token[j] = str[i];
}

token[j] = ''; //terminate the token
return i;
}

/*returns token type. 1 is for STR and 2 for INT and 0 for other types */
int getTokenType(char token[])
{
int i=0;
int type = 1; /*assume token type to be STR*/


while(token[i] != '')
{
if(!isalpha(token[i])) /*any character is not alphabetic break out*/
{
type = 0; //not a valid STR type
break;
}
i++;
}

/*if type is still 1 , then its a valid STR type token*/
if(type == 1)
return type;

type = 2; /*assume type to be INT*/
i = 0;
while(token[i] != '')
{
if(!isdigit(token[i])) //is any character not a digit
{
type = 0;
break;
}
i++;
}

return type;
}

int main()
{
char str[100];
char token[66];
int count, type, start = 0, tokenNum;
int valid = 1;
int repeat = 1;
int len;

while(repeat)
{
printf(" > ");
fgets(str, 100, stdin);

len = strlen(str);
str[len-1]=''; /*fgets returns the at the end of string, remove the last character by replacing it with */

if(strlen(str) > 65)
{
printf("ERROR! Too many characters.");
}
else if ((count = countTokens(str)) > 2)
{
printf("Error! Incorrect number of tokens found.");
}
else
{
tokenNum = 1;
valid = 1;
start = 0;

while(valid)
{
//get next token and its type
start = getNextToken(str, start,' ', token);
type = getTokenType(token);

//depending on token number, see if the type is correct at that location
switch(tokenNum)
{
case 1:
if(type != 1)
valid = 0;
break;
case 2:
if(type != 2)
valid = 0;

break;
}

if(!valid)
{
if(count == 1) /*its not a valid token and there were 1 token in input*/
printf("Expected STR");
else
printf("Expected STR INT");

break;
}
else
{

if(count == 1) /*there was a single token in input*/
{
if(strcmp(token, "quit") == 0) /*was the token quit*/
{
repeat = 0;
break;
}
else
printf("STR"); /*display the token type*/
}
else if(tokenNum == count) /*all tokens valid, and count is more than 1*/
{
printf("STR INT");
}
}


start++; /*increment start to find next token to 1 character after delimiter*/
tokenNum++;
if(tokenNum > count)
break;

}


}

}

}

output

> 1
Expected STR
> 1 2
Expected STR INT
> 1 2 3
Error! Incorrect number of tokens found.
> stuff
STR
> stuff 1
STR INT
> quit