Skip to content

Commit

Permalink
Exerc 5_04.
Browse files Browse the repository at this point in the history
  • Loading branch information
rsm-lisper committed May 28, 2024
1 parent 6f90018 commit f08d9e7
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 0 deletions.
3 changes: 3 additions & 0 deletions chapter_5.pointers_arrays/5_04.strend/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
BINARY=strend

include ../../Makefile.exerc
30 changes: 30 additions & 0 deletions chapter_5.pointers_arrays/5_04.strend/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# include <stdio.h>
# include "strend.h"


void test (char *s, char *t)
{
printf("strend(\"%s\", \"%s\") => %d\n", s, t, strend(s, t));
}


int main ()
{
char s[] = "Write the function";

test(s, "");
test(s, "n");
test(s, "x");
test(s, "nn");
test(s, "ion");
test(s, "enction");
test(s, "Write");
test(s, "1. Write the function");
test(s, " Write the function");
test(s, "Write the function");
test(s, "function");
test(s, " function");
test(s, "ite the function");

return 0;
}
21 changes: 21 additions & 0 deletions chapter_5.pointers_arrays/5_04.strend/strend.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@

/*
strend:
returns 1 if the string t occurs at the end of the string s,
and zero otherwise
*/
int strend (char *s, char *t)
{
char *ts = s, *tt = t;
while (*ts++ != '\0')
;
ts--;
while (*tt++ != '\0')
;
tt--;
while (s <= ts && t <= tt && *ts == *tt) {
ts--;
tt--;
}
return tt < t;
}
11 changes: 11 additions & 0 deletions chapter_5.pointers_arrays/5_04.strend/strend.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# ifndef STREND_H
# define STREND_H

/*
strend:
returns 1 if the string t occurs at the end of the string s,
and zero otherwise
*/
int strend (char *s, char *t);

# endif
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
strend("Write the function", "") => 1
strend("Write the function", "n") => 1
strend("Write the function", "x") => 0
strend("Write the function", "nn") => 0
strend("Write the function", "ion") => 1
strend("Write the function", "enction") => 0
strend("Write the function", "Write") => 0
strend("Write the function", "1. Write the function") => 0
strend("Write the function", " Write the function") => 0
strend("Write the function", "Write the function") => 1
strend("Write the function", "function") => 1
strend("Write the function", " function") => 1
strend("Write the function", "ite the function") => 1

0 comments on commit f08d9e7

Please sign in to comment.