Re: String Comparision



osmium wrote, On 29/04/07 19:03:
"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);
}

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

The first problem I note is that the terminating condition on the for loop should be an '\0', signifying end of string. This suggests the middle expression should be something like

*s!='\0' && *t'='\0'

But this doesn't work because we don't know if both strings are the same length. So now we precede your code with something to find the length of the strings.

Actually, you don't have to worry about whether the strings are the same length, of even check for the end of both strings. After all, is the strings are of different lengths then when you get to the end of the shorter string you will find that '\0'!=character.

> But that uses stuff in <string.h>, the very thing you are
trying to avoid! But then note that you are barely using a for loop, it is a force fit. I suggest you rewrite using a while loop. I don't like suggesting new and different ways to attack a problem, but even more, I don't like the code I foresee if the for loop approach is continued.

I consider the primary good attribute of a for loop the index variable. But you don't *use* an index variable.

The approach used by the OP is not bad, it just has an error in it. Using an index variable is also perfectly acceptable IMHO since a decent optimising compiler will happily convert from one to the other depending on what is more efficient for the processor.
--
Flash Gordon
.



Relevant Pages

  • Re: C# coding guidelines: use "this." or not when referring to member fields/properties within the
    ... Alphabetic for strings isn't quite so ... One example of where people go wrong is when they want to optimise loop ... implementation so that each iteration takes 10% less time will only ...
    (microsoft.public.dotnet.languages.csharp)
  • RE: Error handling in a Do Loop
    ... I made the changes you suggested to my strings - blanking them correctly now. ... have any further suggestion or pointers, ... > When loop is able to ping a computer and able to pull the information, ... when the loop does not ping a computer/unable to ...
    (microsoft.public.windows.server.scripting)
  • Re: Stupid Newbie Needs Help
    ... Without the loop the program works fine with the ... with 0-terminated strings, that way you can take advantage of C's ... hold up to 10 tokens, where each token may be up to 80 characters ... should give a clue of how the variable/constant/function/macro is ...
    (comp.lang.c)
  • Re: timeit module: am I missing something obvious?
    ... Why am I passing strings around when functions are ... best of 3 trials: 0.792 usec per loop ... quickly choosing number of iterations. ... - The result from the final pass through the convergence loop ...
    (comp.lang.python)
  • Re: HELP ME on Sorting Characters.
    ... >> 2) overwrite the original array, using a loop for each letter, and NOT ... strings like this, but likely it would be for large strings. ... int main ...
    (comp.lang.c)