String Comparision



(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;
}
.



Relevant Pages

  • String1 <, <=, >, >=, String2
    ... maximum value at (int from_index = 2). ... I can do it by comparing charAtof all the three strings ... write a loop to compare the two strings char by char. ...
    (comp.lang.java.help)
  • Re: String Comparision
    ... code which would compare two strings using pointers. ... int comp ...
    (comp.lang.c)
  • Re: String Comparision
    ... I am trying to learn C.As a simple exercise i tried to write a 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. ... did you copy and paste or retype? ...
    (comp.lang.c)
  • Re: string-question
    ... You can compare strings using compareTo, which returns an int. ... value of 0 indicates equality. ...
    (comp.lang.java.programmer)
  • Re: Boost process and C
    ... of int for sizes, rather than size_t. ... read-only and constant strings and detecting errors efficiently ... where Bstrlib fails to be able to deal with them due to its design ... If the DOS port hadn't been dropped then depending on the compiler we ...
    (comp.lang.c)