View previous topic :: View next topic |
Author |
Message |
syeda amna
Joined: 28 Dec 2012 Posts: 21
|
Problem in comparing two strings Strcmp() |
Posted: Mon Mar 18, 2013 4:09 am |
|
|
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
|
|
Posted: Mon Mar 18, 2013 4:49 am |
|
|
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
|
|
Posted: Mon Mar 18, 2013 5:38 am |
|
|
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: 1354
|
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19538
|
|
Posted: Mon Mar 18, 2013 6:50 am |
|
|
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
|
|
Posted: Mon Mar 18, 2013 6:57 am |
|
|
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
|
|
Posted: Mon Mar 18, 2013 7:16 am |
|
|
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: 19538
|
|
Posted: Mon Mar 18, 2013 8:03 am |
|
|
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
|
|
Posted: Mon Mar 18, 2013 8:27 am |
|
|
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
|
|
Posted: Wed Mar 20, 2013 2:15 am |
|
|
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
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: 19538
|
|
Posted: Wed Mar 20, 2013 2:38 am |
|
|
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
|
|
Posted: Wed Mar 20, 2013 3:02 am |
|
|
no effect |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19538
|
|
Posted: Wed Mar 20, 2013 3:45 am |
|
|
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
|
|
Posted: Thu Mar 21, 2013 5:06 am |
|
|
Finally solved. the code is working with get_string().
thanks ttelmah and everybody. |
|
|
|