Re: String Comparision
- From: user34 <user34@xxxxxxxxxxx>
- Date: Sun, 29 Apr 2007 22:59:15 +0530
Flash Gordon wrote:
user34 wrote, On 29/04/07 17:53:(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?
First off, did you copy and paste or retype? I suspect you retyped and in the process typed correctly what is wrong in the copy you ran.
I did not retype the code !
Here it is once again (without any of the above suggested improvements)and it still shows me the same 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: Default User
- Re: String Comparision
- From: Flash Gordon
- Re: String Comparision
- References:
- String Comparision
- From: user34
- Re: String Comparision
- From: Flash Gordon
- String Comparision
- Prev by Date: Re: String Comparision
- Next by Date: Re: String Comparision
- Previous by thread: Re: String Comparision
- Next by thread: Re: String Comparision
- Index(es):
Relevant Pages
|
|