Detection of word NANO from a string using c language

Why my code is not perfectly working ?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include<stdio.h>
#include<conio.h>
#include<string.h>
enum my_state_t
{
    STATE_1=0,
    STATE_2=1,
    STATE_3=3,
    STATE_4=4
};
void main()
{
 enum my_state_t state=STATE_1;
 char str[20];
 int i;
  clrscr();
  for(i=0;i<10;i++)
  {
  printf("str[%d]: %s\n",i,str);
  scanf("%s",str);
switch(state)
{
  case STATE_1:
     if(strcmp(str,"n"))
      {
	printf("n is found");
	state = STATE_2;
      }
	break;

  case STATE_2:
      gets(str);
      if(strcmp(str,"a"))
       {
	printf(" a is found");
	state = STATE_3;
	}
      else
	state = STATE_1;
	break;
  case STATE_3:

      if(strcmp(str,"n"))
      {
	 printf("n is found");
	 state=STATE_4;
      }
      else if(strcmp(str,"a"))
	 state=STATE_2;
      else
	 state=STATE_1;
      break;
  case STATE_4:
      if(strcmp(str,"o"))
	    printf(" nano is found ");
	break;
	}
}
getch();
}  
A little better description of the problem would help us look in the right area.
Not sure why you need to jump from state to state like that? Is this some sort of finite automaton?

The function you are using to find something in another string is wrong. strcmp compares two strings for equality and returns a value depending on the comparison.
http://www.cplusplus.com/reference/cstring/strcmp/

What you want to use is strstr which is used to check if a string contains another (Note the difference)
http://www.cplusplus.com/reference/cstring/strstr/
Topic archived. No new replies allowed.