Program is to written in C programming language Program 2 should prompt the user
ID: 3870989 • Letter: P
Question
Program is to written in C programming language
Program 2 should prompt the user for input with the right carrot (">" ). When the user types in input, it should be treated as space delimited input and terminated by the newline character. The program should only take in user input of at most 65 characters (ignoring any more). The program will print either STR or INT for each input token. If the input token is an integer, it should output INT, otherwise it should output STR. The output should be on a single line and terminated by a newline character. > User 12 Input 3.14 Goes 12345 HereExplanation / Answer
#include <stdio.h>
#include <ctype.h>
#include <stdbool.h>
bool is_int(char * line, int s, int e){
// this function check if input is
// integer or not
//printf(" >> %d %d ", s, e);
for(int i = s ; i< e; i++){
if(!isdigit((int)line[i])){
return false;
}
}
return true;
}
int main() {
//code
int N = 66;
char lineContent[N];
printf("> ");
// take input
scanf("%65[^ ]", &lineContent);
getchar();
int ind = 0;
while(lineContent[ind] != ''){
// loop throught the input till end of line reached
int end = ind+1;
while(lineContent[end] != '' && lineContent[end] != ' '){
// find the end of a word
end +=1;
}
if(end > ind + 1){
// a non empty word is found
// ind:- start index
// end:- end index plus 1
if(is_int(lineContent, ind, end)){
// check if the word is number
printf("INT ");
}else{
printf("STR ");
}
}
if(lineContent[end] == ''){
break;
}
ind = end+1;
}
printf(" ");
// end with newline
return 0;
}
INPUT : - User 12 INPUT 3.13 Goes 12345 Here
OUTPUT:-
> STR INT STR STR STR INT STR
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.