Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Detection of word NANO from a string

c

This topic has been archived. This means that you cannot reply to this topic.
2 replies to this topic

#1 SaiGanesh

SaiGanesh

    CC Lurker

  • Just Joined
  • Pip
  • 2 posts

Posted 19 December 2015 - 10:09 PM

Why my code is not working perfectly ?

 

What ever the data it is just printing "nano is found".

 

But i want it as when "n" is detected it has to print n is found . untill the last stage it has to carry and print nano is found

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


#2 dargueta

dargueta

    I chown trolls.

  • Moderator
  • 4854 posts

Posted 21 December 2015 - 10:51 PM

That's because you're comparing the entire string, so none of the cases will match. You need to check one character at a time.


sudo rm -rf / && echo $'Sanitize your inputs!'


#3 AceInfinity

AceInfinity

    CC Addict

  • Senior Member
  • PipPipPipPipPip
  • 313 posts

Posted 10 January 2016 - 01:17 AM

Why can't you use strstr() from <string.h>?


Microsoft MVP (2012) - .NET Programming | ®Crestron DMC-T Certified Programmer
Posted Image