patterncMinor
Determining if one string occurs at the end of another
Viewed 0 times
determiningtheoneanotherendoccursstring
Problem
This is exercise 5-4 from K&R. I spent hours tweaking it but now it seems to work. I'm new to pointers and I'd welcome any comments about how to do it better.
Here is
```
/* Test the function strend(s, t), which returns 1 if the string t
occurs at the end of string s and zero otherwise /
#include "jim.h"
#include "subs.c"
char a[MAXLINE], b[MAXLINE];
int main (void)
{
printf("Return = %1d, Expect = 1\n", strend("12345", "45"));
printf("Return = %1d, Expect = 0\n", strend("12345", "35"));
printf("Return = %1d, Expect = 0\n", strend("45", "345"));
printf("Return = %1d, Expect = 1\n", strend("12345", "12345"));
printf("Return = %1d, Expect = 1\n", strend("12345", "5"));
printf("Return = %1d, Expect = 0\n", strend("12345", "4"));
printf("Return = %1d, Expect = 1\n", strend("12345", ""));
printf("Return = %1d, Expect = 0\n", strend("12345", "+"));
printf("Return = %1d, Expect = 0\n", strend("12345", "a2345"));
printf("Return = %1d, Expect = 1\n", strend("", ""));
printf("Return = %1d, Expect = 0\n", strend("", "Z"));
printf("Return = %1d, Expect = 1\n", strend("1", "1"));
printf("Return = %1d, Expect = 0\n", strend("1", "1A"));
printf("Return = %1
/* Function strend(s, t), which returns 1 if the string t
* occurs at the end of string s and zero otherwise */
int strend(const char *s, const char *t)
{
const char *s0, *t0;
if (s == 0) {
printf("s is NULL pointer\n");
return (-1);
}
if (t == 0) {
printf("t is NULL pointer\n");
return (-1);
}
s0 = s;
t0 = t;
while (*s++)
;
s -= 2; /* *s points to last real char in s */
while (*t++)
;
t -= 2; /* *t points to last real char in t */
if ((t-t0) > (s-s0))
return (0); /* t is longer than s */
while (t>=t0) {
if (*s-- != *t--)
return (0); /* Mismatch */
}
return (1); /* Match */
}Here is
main():```
/* Test the function strend(s, t), which returns 1 if the string t
occurs at the end of string s and zero otherwise /
#include "jim.h"
#include "subs.c"
char a[MAXLINE], b[MAXLINE];
int main (void)
{
printf("Return = %1d, Expect = 1\n", strend("12345", "45"));
printf("Return = %1d, Expect = 0\n", strend("12345", "35"));
printf("Return = %1d, Expect = 0\n", strend("45", "345"));
printf("Return = %1d, Expect = 1\n", strend("12345", "12345"));
printf("Return = %1d, Expect = 1\n", strend("12345", "5"));
printf("Return = %1d, Expect = 0\n", strend("12345", "4"));
printf("Return = %1d, Expect = 1\n", strend("12345", ""));
printf("Return = %1d, Expect = 0\n", strend("12345", "+"));
printf("Return = %1d, Expect = 0\n", strend("12345", "a2345"));
printf("Return = %1d, Expect = 1\n", strend("", ""));
printf("Return = %1d, Expect = 0\n", strend("", "Z"));
printf("Return = %1d, Expect = 1\n", strend("1", "1"));
printf("Return = %1d, Expect = 0\n", strend("1", "1A"));
printf("Return = %1
Solution
Const correct the pointers. The function is not changing anything in memory, no reason they shouldn't be
Check for NULL in the input pointers.
Use library string functions wherever possible.
Instead of doing:
call
Add
Unit-test to confirm correctness.
const. Same with variables you declare in the function; Make them constint strend(const char* s, const char* t)Check for NULL in the input pointers.
Use library string functions wherever possible.
Instead of doing:
while(*s++) ;
s-=2;call
strlen(s); and then substract 1 as needed in your algo. Since you are iterating over the string anyway replacing this with a function call has the same efficiency and makes it more readable.Add
{ around the while loop. It is future-proof.Unit-test to confirm correctness.
Code Snippets
int strend(const char* s, const char* t)while(*s++) ;
s-=2;Context
StackExchange Code Review Q#2489, answer score: 6
Revisions (0)
No revisions yet.