Question

I am trying to write a program in C language but keep running into errors. Any...

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);
}

Homework Answers

Answer #1

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:-

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
I'm trying to find a code to check the occurrences of the word or string I...
I'm trying to find a code to check the occurrences of the word or string I wrote my own code but it's not working can you please help and fix my code #include <stdio.h> #include <string.h> #define MAX_SIZE 100 // Maximum string size int main() { char str[MAX_SIZE]; char tosearch[MAX_SIZE]; printf("Enter any string: "); gets(str); printf("Enter word to search occurrences: "); gets(tosearch); int cursor = 0; int i = 0; int stringLen = 0; int searchLen = 0; int count1;...
The code is in C programming language pls convert it into python. Thanks. Program --> #include...
The code is in C programming language pls convert it into python. Thanks. Program --> #include <stdio.h> #include <stdlib.h> void main() { //declare variables FILE *fileptr; char filename[15]; char charRead; char filedata[200],searchString[50]; int i=0,j=0,countNoOfWord=0,count=0; //enter the filename to be opened printf("Enter the filename to be opened \n"); scanf("%s", filename); /* open the file for reading */ fileptr = fopen(filename, "r"); //check file exit if (fileptr == NULL) { printf("Cannot open file \n"); exit(0); } charRead = fgetc(fileptr); //read the string...
C Program Write a program to count the frequency of each alphabet letter (A-Z a-z, total...
C Program Write a program to count the frequency of each alphabet letter (A-Z a-z, total 52 case sensitive) and five special characters (‘.’, ‘,’, ‘:’, ‘;’ and ‘!’) in all the .txt files under a given directory. The program should include a header count.h, alphabetcount.c to count the frequency of alphabet letters; and specialcharcount.c to count the frequency of special characters. Please only add code to where it says //ADDCODEHERE and keep function names the same. I have also...
LANGUAGE C The codes below reads another .c or .txt file. Then, it will read the...
LANGUAGE C The codes below reads another .c or .txt file. Then, it will read the contents of every line and store the contents into a string array. Example: .c file that is to be read: #include <stdio.h> int main(){    printf("Hello World");    return ; } _______________(END OF THE FILE)_______________ Therefore, the code will read the contents above and store the contents in every line into an array, such that.... array[0] = "#include <stdio.h>\n" array[1] = "\n" array[2] ="int...
C CODE PLZ! Need all TO DO sections finished thanks #include <stdio.h> int main(int argc, char...
C CODE PLZ! Need all TO DO sections finished thanks #include <stdio.h> int main(int argc, char **argv) { const int BUF_LEN = 128; char str[BUF_LEN]; int i; char c; int is_binary; int d, n; /* Get the user to enter a string */ printf("Please enter a string made of 0s and 1s, finishing the entry by pressing Enter.\n"); for (i=0; i<BUF_LEN-1; i++) { scanf("%c", &c); if (c == '\n') { break; } str[i] = c; } str[i] = '\0'; /*...
I am attempting to cause a simple stack buffer overflow in the C language. This is...
I am attempting to cause a simple stack buffer overflow in the C language. This is the code I have: #include <stdio.h> #include <string.h> int main(int argc, char *argv[]){ char buffer[10]; strcpy(buffer,argv[1]); }; When I run this code I use the input "0123456789" a total of 10 bytes and I recieve an error that the core was dumped. But I thought since I defined a buffer of size 10 any input greater than 10 would deliver that error, inputs less...
Construct a flowchart based on this code and write its equivalent algorithms. #include <stdio.h> int main()...
Construct a flowchart based on this code and write its equivalent algorithms. #include <stdio.h> int main() { int x,y; float result; char ch; //to store operator choice printf("Enter first number: "); scanf("%d",&x); printf("Enter second number: "); scanf("%d",&y); printf("Choose operation to perform (+,-,*,/): "); scanf(" %c",&ch); result=0; switch(ch) { case '+': result=x+y; break; case '-': result=x-y; break; case '*': result=x*y; break; case '/': result=(float)x/(float)y; break; case '%': result=x%y; break; default: printf("Invalid operation.\n"); } printf("Result: %d %c %d = %.2f\n",x,ch,y,result); // Directly...
Question Rewrite the following C program using switch-case statement. and give me an output please use...
Question Rewrite the following C program using switch-case statement. and give me an output please use printf and scanf #include<stdio.h> int main(void) {      int semester;           printf("What is your Studying Level (1-8)?> ");      scanf("%d" , &semester);      if(semester == 1 || semester == 2)           printf("Your are a freshman!\n ");      else if(semester == 3 || semester == 4)           printf("Your are sophomore!\n ");      else if(semester == 5 || semester == 6)           printf("Your are...
1 #include <stdio.h> 2 #include <stdlib.h> 3 4 extern char **environ;    5 void output(char *a[], char...
1 #include <stdio.h> 2 #include <stdlib.h> 3 4 extern char **environ;    5 void output(char *a[], char *b[]) { 6 int c = atoi(a[0]); 7 for (int i = 0; i < c && b[i]; ++i) { 8 printf("%s", b[i]+2); 9 } 10 } 11 12 void main(int argc, char *argv[]) { 13      14 switch (argc) { 15 case 1: 16 for (int i = 0; environ[i]; ++i) {    17 printf("%s\n", environ[i]); 18 } 19 break; 20 default: 21 output(argv +...
use c language only to solve this porblem -------------------------- Have the function StringChallenge(str) take the str...
use c language only to solve this porblem -------------------------- Have the function StringChallenge(str) take the str string parameter being passed and return the number of words the string contains (e.g. "Never eat shredded wheat or cake" would return 6). Words will be separated by single spaces. Examples Input: "Hello World" Output: 2 Input: "one 22 three" Output: 3 you have the following code what edits can you do #include <stdio.h> #include <string.h> void StringChallenge(char str[]) {      // code goes...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT