Re: String Comparision
- From: Barry Schwarz <schwarzb@xxxxxxxxx>
- Date: Sun, 29 Apr 2007 15:20:00 -0700
On Sun, 29 Apr 2007 10:05:20 -0700, "osmium" <r124c4u102@xxxxxxxxxxx>
wrote:
"user34" writes:
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?
#include<stdio.h>
int comp(char *s,char *t)
{
for(;*s==*t;s++,t++);
if(*s=='\0')
return 0;
else
return (*s -*t);
You return after examining the first char, no matter how it turns out.
I think you missed the ; at the end of the for statement.
To the original poster, this is one reason why many recommend
for ( )
;
Rethink your logic.
}
int main(void)
{
char str[]="Hello";
char str2[]="Hello";
if(comp(str,str2)!=0)
printf("strings are different");
else
printf("strings are same");
return 0;
}
Remove del for email
.
- Follow-Ups:
- Re: String Comparision
- From: CBFalconer
- Re: String Comparision
- References:
- String Comparision
- From: user34
- String Comparision
- Prev by Date: Re: Initialization of an array and symbolic names for index
- Next by Date: Re: Ahead of "main"?
- Previous by thread: Re: String Comparision
- Next by thread: Re: String Comparision
- Index(es):
Relevant Pages
|