Re: String Comparision
- From: user34 <user34@xxxxxxxxxxx>
- Date: Mon, 30 Apr 2007 20:37:34 +0530
user34 wrote:
Hi all. I am trying to learn C.As a simple exercise i tried to write a code which would compare two strings using pointers.
However i am not getting the correct result.
On tracing the program i noticed that the strings "s" and "t" get modified somehow in the comp function and as a result i am getting incorrect results.
Can anyone please point out the error?
....Code snipped....
Thank you all for your help.I have tried to modify the code and here it is .This does show me the correct output but are there any bugs still lurking around which can cause it to fail?
#include <stdio.h>
int comp(const char *s,const char *t)
{
for( ; *s==*t ; s++,t++)
{
if(*s=='\0' || *t=='\0')
break;
}
if(*s=='\0')
return 0;
else
return 1;
}
int main(void)
{
char str[]="Hello";
char str2[]="Hello";
if(comp(str,str2))
printf("strings are different");
else
printf("strings are same");
return 0;
}
.
- References:
- String Comparision
- From: user34
- String Comparision
- Prev by Date: Re: enum variables and extern
- Next by Date: Re: enum variables and extern
- Previous by thread: Re: String Comparision
- Next by thread: Ahead of "main"?
- Index(es):
Relevant Pages
|