Re: K&R2, exercise 5.4



On Wed, 09 Apr 2008 16:44:13 +0500, arnuld wrote:

I have come up with following and it runs fine. Any of you finds a
run-time bug in it ?

that has a bug :( , this is the newer version free of bugs. Do you have
any advice in this program:


#include <stdio.h>
#include <stdlib.h>

int strend( char*, char* );



int main()
{
char s[] = "Love";
char t[] = "Loved";


printf("\n%d\n", strend(s,t));


return EXIT_SUCCESS;
}


/* I am using pointers because, arrays are never passed to functions and I
* don't want to FAKE the array call
*
* outer for loop checks for each element of 1st array.
* inner for loop compares elements of both arrays.
* if condition checks whether we have compared the 2nd array till end.
*/
int strend( char* s, char* t )
{
char *pj;

for( ; *s != '\0'; *s++ )
{
for( pj = s; *t == *pj; t++, pj++ )
{
;
}

if( *t == '\0' )
{
return 1;
}
}

return 0;
}



--
http://lispmachine.wordpress.com/

Please remove capital 'V's when you reply to me via e-mail.

.



Relevant Pages

  • K&R2, exercise 5.4
    ... int strend(char*, char*); ... 2nd array, ... so that we can compare from there till end. ... int strend(char* s, char* t) ...
    (comp.lang.c)
  • Re: K&R2, exercise 5.4
    ... int strend(char*, char*); ... outer for loop checks for each element of 1st array. ... inner for loop compares elements of both arrays. ... int strend(char* s, char* t) ...
    (comp.lang.c)
  • Re: <ctype.h> toLower()
    ... Jumbo wrote: ... >> How do I apply this to an array of characters? ... > But this crashes therefore it is a bug. ... "HI" is not an array of char. ...
    (alt.comp.lang.learn.c-cpp)
  • Re: Window Management
    ... (* prompt is a string resource identifier specifying the string ... PROCEDURE YesNoCancel(prompt: ARRAY OF CHAR; ... VAR INOUT response: ARRAY OF CHAR): BOOLEAN; ...
    (comp.lang.ada)
  • Window Management
    ... (* prompt is a string resource identifier specifying the string ... PROCEDURE YesNoCancel(prompt: ARRAY OF CHAR; ... VAR INOUT response: ARRAY OF CHAR): BOOLEAN; ...
    (comp.lang.ada)