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 5 will prompt the user for one or tw

ID: 3870997 • Letter: P

Question

program to be written in C language

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 > 1 ERROR! Expected STR. > Stuff STR > tuff 1 STR INT

Explanation / Answer

#include<stdio.h>

#include<ctype.h>

#include<string.h>

int countTokens(char* str){

int count =1,i=0;

for(i=0;i<strlen(str)-2;i++){

if(str[i]==' '&& (str[i+1]!=' '||str[i-1]!=' '))

count++;

}

return count;

}

int checkCount1(char* str){

int isAlpha;

isAlpha =1;

int i;

for(i=0;i<strlen(str);i++){

if(!(isalpha(str[i]))){

isAlpha = 0;

break;

}

}

return isAlpha;

}

int checkCount2(char* str){

int isAlpha,isNum;

isAlpha =1;

isNum=1;

int i;

int space_index = 0;

for(i=0;i<strlen(str);i++){

if(str[i]==' ')

{

space_index = i ;

break;

}

}

for(i=0;i<space_index-1;i++){

if(!(isalpha(str[i]))){

isAlpha = 0;

break;

}

}

for(i=space_index+1;i<strlen(str);i++){

if(!(isdigit(str[i]))){

isNum = 0;

break;

}

}

if(isAlpha==1&&isNum==1)

return 1;

else return 0;

}

int main(){

char* str;

char* compare1="QUIT";

char* compare2= "quit";

printf(" enter string : ");

scanf("%s",str);

while(strcmp(str,compare1)!=0&&strcmp(str,compare2)!=0){

if(!(strlen(str)>65))

{

int count = countTokens(str);

if(count<=2){

if(count==1){

if(checkCount1(str)==1)

printf(" STR");

else

printf(" Expected STR");

}else if(count == 2){

if(checkCount2(str)==1)

printf(" STR INT");

else

printf(" Expected STR INT");

}

}else{

printf(" ERROR! Incorrect number of tokens found");

}

}

else{

printf(" String can have atmost 65 characters");

}

printf(" enter string : ");

scanf("%s",str);

}

return 0;

}