Re: String Comparision
- From: Aditya <adityagupta.18@xxxxxxxxx>
- Date: 30 Apr 2007 00:58:55 -0700
On Apr 29, 9:53 pm, user34 <use...@xxxxxxxxxxx> wrote:
(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;
}- Hide quoted text -
- Show quoted text -
HI
I tried this code, it works fine!!, no idea what error u are getting..
and even the concept seems to be ok
Aditya
.
- Follow-Ups:
- Re: String Comparision
- From: Barry Schwarz
- Re: String Comparision
- References:
- String Comparision
- From: user34
- String Comparision
- Prev by Date: Re: gets() is dead
- Next by Date: Re: gets() is dead
- Previous by thread: Re: String Comparision
- Next by thread: Re: String Comparision
- Index(es):
Relevant Pages
|
|