K & R C Programs Exercise 5-4.

K and R C, Solution to Exercise 5-4:
K and R C Programs Exercises provides the solution to all the exercises in the C Programming Language (2nd Edition). You can learn and solve K&R C Programs Exercise.
C Program, that returns 1 if the string t occurs at the end of the string s, and zero otherwise. Read more about C Programming Language .

/***********************************************************
* You can use all the programs on www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact [email protected]
* To find more C programs, do visit www.c-program-example.com
* and browse!
*
* Happy Coding
***********************************************************/


#include <stdio.h>

//finds the string length, standard "strlen" function
int str_len(char *s)
{
int n;

for(n = 0; *s != ''; s++)
{
n++;
}
return n;
}

int str_cmp(char *s, char *t)
{
for(;*s == *t; s++, t++)
if(*s == '')
return 0;
return *s - *t;
}


int str_end(char *s, char *t)
{
int Result = 0;
int s_length = 0;
int t_length = 0;

/* get the lengths of the strings */
s_length = str_len(s);
t_length = str_len(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 == str_cmp(s, t))
{
Result = 1;
}
}

return Result;
}
int main(void)
{
char Str1[8192] ;
char Str2[8192] ;
char Str3[8192] ;
printf("n Enter the first string: n");
scanf("%s",Str1);
printf("n Enter the second string: n");
scanf("%s",Str2);
printf("n Enter the third string: n");
scanf("%s",Str3);
printf("String one is (%s)n", Str1);
printf("String two is (%s)n", Str2);
printf("String two is (%s)n", Str3);

if(str_end(Str1, Str2))
{
printf("The string (%s) has (%s) at the end.n", Str1, Str2);
}
else
{
printf("The string (%s) doesn't have (%s) at the end.n", Str1, Str2);
}
if(str_end(Str1, Str3))
{
printf("The string (%s) has (%s) at the end.n", Str1, Str3);
}
else
{
printf("The string (%s) doesn't have (%s) at the end.n", Str1, Str3);
}



return 0;
}


C BasicC StringsK and R C Programs ExerciseYou can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can Follow @c_program

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it

(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!

To browse more C Programs visit this link
(c) www.c-program-example.com

Leave a Reply