i tried to define string end function in K&R excercises
i tried to define string end function in K&R excercises
i tried to define strend(s,t)
function which returns 1 if string t at the end of s string and zero otherwise , this is my code .
strend(s,t)
typedef enum state
{
Not_occured ,
occured
}State;
char a="Hello world zone";
char b="ne";
int main(void)
{
int x = 0 ;
x = strend(a,b);
printf("%d",x);
return 0;
}
int strend(char *s, char *t)
{
while(*++s);
while(*++t);
while(*t-- == *s--)
if(!(*t))
return occured;
return Not_occured;
}
modified code
int strend(char *s, char *t)
{
char *ptr = s;
while(*++s);
while(*++t);
while(*t-- == *s--)
if(s == ptr)
return occured;
return Not_occured;
}
why prefix worked here while(*++s);
, while(*++t);
and postfix doesn't work?
while(*++s);
while(*++t);
t
Think about what
if (!(*t))
is supposed to be checking.– dbush
Jul 2 at 18:19
if (!(*t))
The prefix code
while(*++s);
doesn't actually work. It assumes that there's at least one non-NUL character in the string. If you define a string b = "";
and use the loop, the code is likely to crash, or at least behave badly. The correct way to write the loop is while(*s) s++;
– user3386109
Jul 2 at 18:43
while(*++s);
b = "";
while(*s) s++;
@mohamedtarek The code has undefined behavior. So even though it seems to work (for the example in the question), it doesn't actually work. That's a hard concept for new C programmers to grasp, but it's very important if you want to be successful as a C programmer. Just because your code passes one test doesn't mean it's right.
– user3386109
Jul 2 at 19:28
The bottom line is that both the prefix code
while(*++s);
and the postfix code while(*s++);
are wrong. The correct code is while(*s) s++;
– user3386109
Jul 2 at 19:30
while(*++s);
while(*s++);
while(*s) s++;
1 Answer
1
if(!(*t))
assumes there is a nul character before the first character in the string. Not only is this an incorrect assumption, but it also tries to access memory outside of the array bounds.
if(!(*t))
Also, while(*t-- == *s--)
...What happens when the 2 strings are identical or t
is longer than s
?
while(*t-- == *s--)
t
s
Here's a simple solution:
int strend(char *s, char *t) {
if (s == NULL || t == NULL) return Not_occured;
size_t s_len = strlen(s);
size_t t_len = strlen(t);
if (t_len <= s_len) {
return 0 == strcmp(&s[s_len - t_len], t) ? occured : Not_occured;
}
return Not_occured;
}
Since it's now clearer that the original question is about the pre- and post-fix operations, I've updated.
First, understand the difference between the two:
int x = 1;
printf("%d", x++);
// Prints 1 because x is evaluated before the inc
x = 1;
printf("%d", ++x);
// Prints 2 because x is evaluated after the inc
Now, some operator precedence:
postfix > dereference
prefix == dereference, right-to-left assoc.
What happens with *s++
:
*s++
s++
s
s0
s
s0
This is the same as:
char *s0 = s;
s += 1;
char c = *s0;
What happens with *++s
:
*++s
s
s
This is the same as:
s += 1;
char c = *s;
Hope that makes sense.
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Looks like you're probably dereferencing
t
after it's gone out of the bounds of the array.– Christian Gibbons
Jul 2 at 18:19