Chapter 5 - Pointers and Arrays 4
Write the function strend(s,t) , which returns 1 if the string t occurs at the end of the string s , and zero otherwise.
Solution by Bryan Williams
/*
Exercise 5-4. Write the function strend(s,t), which returns 1 if the string t
occurs at the end of the string s, and zero otherwise.
Author : Bryan Williams
*/
int strlen(char *s) /* added by RJH; source: K&R p99 */
{
int n;
for(n = 0; *s != '\0'; s++)
{
n++;
}
return n;
}
int strcmp(char *s, char *t) /* added by RJH; source: K&R p106 */
{
for(;*s == *t; s++, t++)
if(*s == '\0')
return 0;
return *s - *t;
}
int strend(char *s, char *t)
{
int Result = 0;
int s_length = 0;
int t_length = 0;
/* get the lengths of the strings */
s_length = strlen(s);
t_length = strlen(t);
/* check if the lengths mean that the string t could fit at the string s */
if(t_length <= s_length)
{
/* advance the s pointer to where the string t would have to start in string s */
s += s_length - t_length;
/* and make the compare using strcmp */
if(0 == strcmp(s, t))
{
Result = 1;
}
}
return Result;
}
#include <stdio.h>
int main(void)
{
char *s1 = "some really long string.";
char *s2 = "ng.";
char *s3 = "ng";
if(strend(s1, s2))
{
printf("The string (%s) has (%s) at the end.\n", s1, s2);
}
else
{
printf("The string (%s) doesn't have (%s) at the end.\n", s1, s2);
}
if(strend(s1, s3))
{
printf("The string (%s) has (%s) at the end.\n", s1, s3);
}
else
{
printf("The string (%s) doesn't have (%s) at the end.\n", s1, s3);
}
return 0;
}
Cleaner category 0 solution, by Nicholas Smillie.
#include <string.h>
int strend(char *s, char *t);
int main()
{
char *s = "the world is over";
char *t = "over";
if (strend(s, t))
printf("The text '%s' occurs at the end of the string '%s'", t, s);
else
printf("Test failed.");
return 0;
}
int strend(char *s, char *t)
{
s += (strlen(s) - strlen(t)); //increments to the point of comparison
while (*s++ == *t++) //tests for equality
if (*s == '\0') //checks for null character while lines are equal
return 1;
return 0;
}
Category 0 Solution by Jesus Alvarez (The solution above is the correct way, this solution reverses the strings and then compares. Reversing is not necessary.)
#include <stdio.h>
#include <string.h>
#define STR_BUFFER 10000
int strend(char *, char *);
void strrev(char *, char *);
int main(int argc, char *argv[])
{
char string1[STR_BUFFER] = "ole.";
char string2[STR_BUFFER] = "hole. ";
printf ("String 1: %s\n", string1);
printf ("String 2: %s\n", string2);
if (strend(string1, string2)) {
printf ("String 2 occurs at the end of string 1!\n");
} else {
printf ("String 2 does not occur at the end of string 1.\n");
}
return 0;
}
int strend(char *s, char *t)
{
char *s_pt, *t_pt;
char s_rev[STR_BUFFER];
char t_rev[STR_BUFFER];
s_pt = s_rev;
t_pt = t_rev;
/* Reverse the strings to make the matching more fun, but not very
* efficient. :) */
strrev(s, s_rev);
strrev(t, t_rev);
for ( ; *s_pt == *t_pt; s_pt++, t_pt++) {
if (*s_pt == ' ' || *s_pt == '\t' || *s_pt == '\0') {
return 1; /* The ends of the strings match. */
} else {
return 0;
}
}
return 0;
}
/*
* strrev()
* Reverses a string.
* @param: str Pointer to the string to be reversed.
* @param: dest Pointer where the result will be stored.
*/
void strrev(char *str, char *dest)
{
int i = strlen(str)-1; /* -1 to skip the '\0' */
for (str += i; i >=0; dest++, str--, i--) {
/* Increment backwards over str copying to dest. */
*dest = *str;
}
dest++;
dest = '\0';
}
Category 0 Solution by Jose G. López
#include <stdio.h>
int strend(char *s, char *t);
int main(void)
{
char *s = "hello, hello";
char *t = "llo";
if (strend(s, t))
printf("'%s' occurs at the end of '%s'\n", t, s);
else
printf("no occurences at the end of '%s' from '%s'\n", s, t);
return 0;
}
int strend(char *s, char *t)
{
int i = 0;
int ind;
while (*s) {
for (ind = 0; *(s + ind) == *(t + ind)
&& *(s + ind) != '\0' && *(t + ind) != '\0'; ind++)
;
if(*(s + ind) == '\0' && *(t + ind) == '\0')
return 1;
s++;
}
return i;
}
Category 0 solution by Prabhat Pal
#include<stdio.h>
int strend(char *s, char *t)
{
char *p = t;
while(*s)
{
p = t;
while(*s++ != *p); /* new s value is one ahead of matched *p value */
++p; /*Lock step*/
while(*s++ == *p++)
if(*(s-1)=='\0' && *(p-1)=='\0')
return 1; /*Both matched till the end, boundary condition*/
}
return 0;
}
int main(void)
{
char s[] = "The cat zzy!! is going crazyy!!";
char t[] = "zyy!!";
printf("%s\n",(strend(s,t))?"Matched":"Not Matched");
return 0;
}
menonsahab's
#include <stdio.h>
#include <string.h>
int strend(char *s, char *t)
{
int slen = strlen(s), tlen = strlen(t);
if (slen < tlen)
return 0;
else
{
s = s + slen - tlen;
return strcmp(s, t) ? 0 : 1;
}
}
int main()
{
char s[] = "Daiki", t1[] = "iki", t2[] = "Midorima";
printf("strend(%s, %s) = %d\n", s, t1, strend(s, t1));
printf("strend(%s, %s) = %d\n", s, t2, strend(s, t2));
return 0;
}
seankndy's
#include <stdio.h>
int strend(char *s, char *t);
int main(void)
{
printf("first strend returned %d\n", strend("hello, world!", ", world!"));
printf("second strend returned %d\n", strend("hello, world!", "hello"));
printf("third strend returned %d\n", strend("hello, world!", "xorld!"));
printf("fourth strend returned %d\n", strend("hello, world!", "!"));
printf("fifth strend returned %d\n", strend("hello, world!", "hello, world! hello, world!"));
return 0;
}
int strend(char *s, char *t)
{
char *sc = s;
char *tc = t;
while (*s != '\0')
s++;
while (*t != '\0')
t++;
while (s > sc && t > tc && *s-- == *t--)
;
return (t == tc && *s == *t ? 1 : 0);
}