String Comparision
- From: user34 <user34@xxxxxxxxxxx>
- Date: Sun, 29 Apr 2007 22:23:05 +0530
(Sorry if this gets posted twice)
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);
}
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;
}
.
- Follow-Ups:
- Re: String Comparision
- From: user34
- Re: String Comparision
- From: Aditya
- Re: String Comparision
- From: Keith Thompson
- Re: String Comparision
- From: jg
- Re: String Comparision
- From: Flash Gordon
- Re: String Comparision
- Prev by Date: Re: programming-challenges
- Next by Date: Re: Info about the GNU C Compiler...? (Then what about linux?)
- Previous by thread: the mystery of <ctrl-d>
- Next by thread: Re: String Comparision
- Index(es):
Relevant Pages
|
|