Re: String Comparision
- From: Flash Gordon <spam@xxxxxxxxxxxxxxxxxx>
- Date: Sun, 29 Apr 2007 18:25:53 +0100
user34 wrote, On 29/04/07 18:15:
osmium 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. Rethink your logic.
Can you elaborate? The for loop should parse the string until the characters in the two strings differ ,is'nt it?
Also it won't stop at the end of the strings if they are the same.
--
Flash Gordon
.
- References:
- String Comparision
- From: user34
- Re: String Comparision
- From: user34
- String Comparision
- Prev by Date: Re: String Comparision
- Next by Date: Re: gets() is dead
- Previous by thread: Re: String Comparision
- Next by thread: Re: String Comparision
- Index(es):
Relevant Pages
|