I am trying to write a program in C language but keep running into errors. Any help would be awesome. here is my code I have so far.
#include <stdio.h>
#include <conio.h>
#include <string.h>
int main(){
int lenght, i = 0, state = 0;
char line[100];
printf("enter the string for checking of comment
line: \n");
gets(line);
while(i < strline(line)){
switch(state){
case 0: if
(line[i] == '/'){
state = 1; i++;
}
else i++;
break;
case 1: if
(line[i] == '*'){
state = 2; i++;
}
else i++;
break;
case 2: if
(line[i] == '*'){
state = 3; i++;
}
else i++;
break;
case 3: if
(line[i] == '*'){
state = 3; i++;
}
else if (line[i]
== '/'){
state =2; i++;
}
break;
case 4:
i++;
break;
}
}
//Display output
if (state ==4)
printf("\n The string is recognized as a comment line");
else printf("\n The string is not recongnized as comment
line");
getch();
return (0);
}
In the while loop the function you used is the strline but it should be strlen.
Below is the corrected code and output attached:-
#include <stdio.h>
#include <conio.h>
#include <string.h>
int main(){
int lenght, i = 0, state = 0;
char line[100];
printf("enter the string for checking of comment line:
\n");
gets(line);
while(i < strlen(line)){
switch(state){
case 0: if (line[i] == '/'){
state = 1; i++;
}
else i++;
break;
case 1: if (line[i] == '*'){
state = 2; i++;
}
else i++;
break;
case 2: if (line[i] == '*'){
state = 3; i++;
}
else i++;
break;
case 3: if (line[i] == '*'){
state = 3; i++;
}
else if (line[i] == '/'){
state =4; i++;
}
break;
case 4: i++;
break;
}
}
//Display output
if (state ==4)
printf("\n The string is recognized as a comment line");
else printf("\n The string is not recongnized as comment
line");
getch();
return (0);
}
Output:-
Get Answers For Free
Most questions answered within 1 hours.