H
Hallvard B Furuseth
Philip said:Hallvard said:What gave you that idea? Yes it can. Try.
Let's give it a go:
char a[59] = "String 1";
(...)
Duh, of course. I was thinking "string literal", not "string".
Philip said:Hallvard said:What gave you that idea? Yes it can. Try.
Let's give it a go:
char a[59] = "String 1";
(...)
No. I dispute that "the empty string has size = 1", which is a different
statement entirely.
In two hours you published six versions of this program. This is the
latest according to my news server but I have no idea if it is the
latest you published. Keeping the title the same is good but maybe
you should put a version number in the code.
ok here is the final working version![]()
arnuld said:The only thing I did not understand in my final version is if I remove
these 2 pieces of code:
1.) *pt != '\0' from inner <for> loop
2.) *pt == '\0' && *ps == '\0' , from <if> condition
then the program gives strange results
removing (1) will force code to compare for '\0' in the inner for loop and
hence there is no need to compare for NULL in <if> condition. Then why
program gives wrong results ?
arnuld said:ok here is the final working version![]()
/* Exercise 5.4 from K&R2, page 107
*
* write the function strend(s, t) which returns 1 if the
* string t occurs at the end of string s and zero otherwise.
*
* Final-Version of code
*/
int strend( char* s, char* t, int n )
{
char *ps, *pt;
for( ; *s != '\0'; s++ )
{
for( ps = s, pt = t; *pt == *ps && *pt !='\0'; pt++, ps++ )
{
;
}
if( n > 1 && *pt == '\0' && *ps == '\0' )
You need to loose the n. The question as posed (see above) can be
answered knowing only s and t. No further data is needed, so any
other parameters you give must come from some misunderstanding or the
problem.
Even when you loose the n, this test is over complex. Write down what
you know when the loop above ends.
ok, I removed "n". I have to use my_strlen - to find the length of
the string:
<if> follows from the inner <for> loop:
for( ps = s, pt = t; *pt == *ps && *pt !='\0'; pt++, ps++ )
lets say s = "Love" and t = "ee"
then *pt == *ps will go false and hence the NULL checking in the if condition
is required otherwise it will always print 1. I am not able to find a
replacement for this.
arnuld said:int strend( char* s, char* t )
{
char *ps, *pt;
int len_of_t;
len_of_t = my_strlen( t );
for( ; *s != '\0'; s++ )
{
for( ps = s, pt = t; *pt == *ps && *pt !='\0'; pt++, ps++ )
{
;
}
if( len_of_t && *pt == '\0' && *ps == '\0' )
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.