Click to See Complete Forum and Search --> : comparing two strings in c
kasturiaparna
October 25th, 2006, 02:36 AM
hi
I wrote a program for comparing two strings in C. It works well.
But it is not working for, strings as "hello", "helloo"
It is displaying hello = helloo
How to modify the code, so that it displays hello<helloo?
please help me
main()
{
char src[]="bhelloo";
char dest[]="ahelloo";
int i;
i=compare(src,dest);
if(i==0)
printf("%s > %s", src,dest);
if(i==1)
printf("%s < %s", src,dest);
if(i==-1)
printf("%s = %s", src,dest);
}
int compare(char src[], char dest[])
{
int i;
for(i=0; i<strlen(src)&&i<strlen(dest);i++)
{
if(src[i]>dest[i])
return 0;
else if(src[i]<dest[i])
return 1;
else
return -1;
}
}
humptydumpty
October 25th, 2006, 02:51 AM
Few points.
Before Entering in your loop Check if lenght of Both String same Then Only move ahead.instead of Directly entering in a Loop and start Checking whether they are equal or not.
Thanx
kasturiaparna
October 25th, 2006, 03:06 AM
I want to compare unequal strings.
company and companies
development and develop
And unable to do that using above code.
humptydumpty
October 25th, 2006, 03:10 AM
didn't you get my above point.i mean to say Before Performing your Operation in Loop
you can Check the length of both the string and can Compare the length if both are the same length then go ahead else Simply return 1 or -1 according to result.
Thanx
wildfrog
October 25th, 2006, 04:42 AM
How to modify the code, so that it displays hello<helloo?By adding a small check on the strings lengths. If they're not the same length, then they're not equal.
- petter
humptydumpty
October 25th, 2006, 04:55 AM
hi
I wrote a program for comparing two strings in C. It works well.
But it is not working for, strings as "hello", "helloo"
It is displaying hello = helloo
How to modify the code, so that it displays hello<helloo?
please help me
putting same Question in Multiple thread Doesn't create any Sense.here is your thread.
http://www.codeguru.com/forum/showthread.php?t=403913
Thanx
Yves M
October 25th, 2006, 06:49 AM
[ Merged threads ]
rbofnjtu
October 29th, 2006, 01:07 AM
hi
I wrote a program for comparing two strings in C. It works well.
But it is not working for, strings as "hello", "helloo"
It is displaying hello = helloo
How to modify the code, so that it displays hello<helloo?
please help me
main()
{
char src[]="bhelloo";
char dest[]="ahelloo";
int i;
i=compare(src,dest);
if(i==0)
printf("%s > %s", src,dest);
if(i==1)
printf("%s < %s", src,dest);
if(i==-1)
printf("%s = %s", src,dest);
}
int compare(char src[], char dest[])
{
int i;
for(i=0; i<strlen(src)&&i<strlen(dest);i++)
{
if(src[i]>dest[i])
return 0;
else if(src[i]<dest[i])
return 1;
else
return -1;
}
}
You'd better use 0, -1, 1 to express being equal, smaller, and larger, respectively. Just modify your compare function like this:
main()
{
char src[]="bhelloo";
char dest[]="ahelloo";
int i;
i=compare(src,dest);
if(i==1)
printf("%s > %s", src,dest);
if(i==-1)
printf("%s < %s", src,dest);
if(i==0)
printf("%s = %s", src,dest);
}
int compare(char src[], char dest[])
{
int i;
assert(src != NULL && dest != NULL);
for(i=0; i<(strlen(src)+1) && i<(strlen(dest)+1);i++)
{
if(src[i] == dest[i])
continue;
else if(src[i]<dest[i])
return -1;
else
return 1;
}
return 0;
}
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.