Re: String Comparision



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
.



Relevant Pages

  • Re: A note on computing thugs and coding bums
    ... Here's my response including a bug fix. ... to make "modern strings" possible was designed and first implemented ... contents to strings, to compare them, and to duplicate them. ... while the Pike code will NEVER work...for international strings. ...
    (comp.programming)
  • Re: String Comparision
    ... code which would compare two strings using pointers. ... 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. ... He needs to test for at least one of the pointers being 0 in the loop ... And he should change the subtraction, if only for pedagogical reasons, ...
    (comp.lang.c)
  • Re: Ascan with a Substr
    ... the above suggestion: ... operator to compare the strings. ... code block you originally used was just right, ...
    (comp.lang.clipper)
  • Re: Ascan with a Substr
    ... The following solution work well ... operator to compare the strings. ... code block you originally used was just right, ...
    (comp.lang.clipper)
  • Re: Array comparison
    ... pointers are only going to compare their object pointers ... pointers and not the contents of the strings. ... things like array of Char, bytes, words, dword, int64's etc.. ...
    (alt.comp.lang.borland-delphi)