CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

Problem in comparing two strings Strcmp()

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
syeda amna



Joined: 28 Dec 2012
Posts: 21

View user's profile Send private message

Problem in comparing two strings Strcmp()
PostPosted: Mon Mar 18, 2013 4:09 am     Reply with quote

why the code does not compare the two strings correctly??

Code:


#include <18F452.h>
#fuses HS,NOWDT,NOLVP,PUT,NOPROTECT,BROWNOUT
#use delay(clock=16M)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)
#include <string.h>


void main()
{
int8 i=0;
int8 x,y;
char password[2];
char line[80] = {"0123456789/0123456789"};  //sample string
char sep[] = {"/"};
char *data;


//=============Use strtok() function to to match/change password========//
   
   data = strtok(line, sep);   //It separates this "/"
 
   while (data!=NULL)
    {
       password[i++]=data;
       data = strtok(0, sep); 
     }
     
     
    for(x=0;x<2;x++){
     puts(password[x]);
     }
     
     
     y = strcmp (password[0] , password[1]) ;
     
     if ( y == 0 )
       puts("correct");
     
     
    else
       puts("incorrect");
         
}
Mike Walne



Joined: 19 Feb 2004
Posts: 1785
Location: Boston Spa UK

View user's profile Send private message

PostPosted: Mon Mar 18, 2013 4:49 am     Reply with quote

Try printing out the two passwords to see what you are actually comparing.

OR debug to examine RAM contents.

Mike
syeda amna



Joined: 28 Dec 2012
Posts: 21

View user's profile Send private message

PostPosted: Mon Mar 18, 2013 5:38 am     Reply with quote

they are same "0123456789". I use putc to see the contents. But it gives Incorrect . why? the same program works in standard C.
jeremiah



Joined: 20 Jul 2010
Posts: 1322

View user's profile Send private message

PostPosted: Mon Mar 18, 2013 6:41 am     Reply with quote

You should update your previous post (not start a new thread):
http://www.ccsinfo.com/forum/viewtopic.php?t=50065

It makes it easier for everyone to help.
Ttelmah



Joined: 11 Mar 2010
Posts: 19333

View user's profile Send private message

PostPosted: Mon Mar 18, 2013 6:50 am     Reply with quote

Don't launch new threads asking the same question.
The version I posted in your previous thread, works.
If it doesn't for you, then the question 'compiler version' applies.

Best Wishes
syeda amna



Joined: 28 Dec 2012
Posts: 21

View user's profile Send private message

PostPosted: Mon Mar 18, 2013 6:57 am     Reply with quote

Sorry for posting new thread. I changed the program a little bit. Just because strcmp() doesn't work. this command still does not give the correct answer. I The same code is working in standard C.
Mike Walne



Joined: 19 Feb 2004
Posts: 1785
Location: Boston Spa UK

View user's profile Send private message

PostPosted: Mon Mar 18, 2013 7:16 am     Reply with quote

Sorry. I didn't read your code carefully enough first time round.

I have just tried your code on a PIC18F458 (Just happened to be in my PICDEM 2 PLUS board).
I did it by cut and paste.

I can confirm the code works as expected.

Prints "correct" as is.
Prints "incorrect" when two halves of line[80] are not same.

Mike
Ttelmah



Joined: 11 Mar 2010
Posts: 19333

View user's profile Send private message

PostPosted: Mon Mar 18, 2013 8:03 am     Reply with quote

I have a 'nasty suspicion', we may be dealing with one of the 'ripped off' early V4 versions, where some things like this did have problems.....

Best Wishes
FvM



Joined: 27 Aug 2008
Posts: 2337
Location: Germany

View user's profile Send private message

PostPosted: Mon Mar 18, 2013 8:27 am     Reply with quote

char password[2]; means you can either store two characters, or a string comprised of one character and a terminating null.

strcmp compares strings, not characters, thus this function call makes no sense at all:
Code:

strcmp (password[0] , password[1]);
syeda amna



Joined: 28 Dec 2012
Posts: 21

View user's profile Send private message

PostPosted: Wed Mar 20, 2013 2:15 am     Reply with quote

Thanks FvM for mentioning the mistake.
Quote:
char password[2]; means you can either store two characters, or a string comprised of one character and a terminating null.


Now i declare it as char *password[2];
Now the program is working well with the sample string.
Another problem arises Sad
When i use gets() function (for inputting the string instead of the sample string), it is not working. the same program is working in standard C. plzzz help.



Code:



#include <18F452.h>
#fuses HS,NOWDT,NOLVP,PUT,NOPROTECT,BROWNOUT
#use delay(clock=16M)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)
#include <string.h>


void main()
{
int8 i=0;
int8 x,y;
char *password[2];
char line[80] = {"0123456789/0123456789"};  //sample string
char sep[] = {"/"};
char *data;
char pass[30] = {""};

//========Inputting String from User Separated by "/" =======//

gets(pass);

//=====Use strtok() function to to match/change password=======//
   

   data = strtok(pass, sep);   //It separates this "/"
 
   while (data!=NULL)
    {
       password[i++]=data;
       data = strtok(NULL, sep);
     }
     
     
    for(x=0;x<2;++x){
     puts(password[x]);
     }
     
     y = strcmp (password[0] , password[1]) ;
     
     if ( y == 0 )
       puts("correct");
     
     
    else
       puts("incorrect");
         
}
Ttelmah



Joined: 11 Mar 2010
Posts: 19333

View user's profile Send private message

PostPosted: Wed Mar 20, 2013 2:38 am     Reply with quote

Classic problem here is what you are using to send the string at the PC end.
Look carefully at what it is setup to send when you type 'return'.
Will your string then match?.

Best Wishes
syeda amna



Joined: 28 Dec 2012
Posts: 21

View user's profile Send private message

PostPosted: Wed Mar 20, 2013 3:02 am     Reply with quote

no effect Confused
Ttelmah



Joined: 11 Mar 2010
Posts: 19333

View user's profile Send private message

PostPosted: Wed Mar 20, 2013 3:45 am     Reply with quote

What do you mean 'no effect'. Have you actually looked at what it sends?....

Most terminal programs send either LF on it's own (which is not character 13), or CR/LF.

If the first case you won't get the CR you want, in the second, you will get an _extra_ LF in the string, which will mean they won't match....

Use get_string in input.c, instead of gets (better anyway since you can specify the maximum line length). Edit it's penultimate line to:
Code:

   } while(c!=13 && c!=10);


set it so it won't overrun your string buffer (remember to allow for the space needed for the terminator), and make sure your terminal program is set to send just LF.

Think.
syeda amna



Joined: 28 Dec 2012
Posts: 21

View user's profile Send private message

PostPosted: Thu Mar 21, 2013 5:06 am     Reply with quote

Finally solved. the code is working with get_string().
thanks ttelmah and everybody.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group