0

What function should i use for comparing the last 3 characters of strings?

Hi all I am writing code for printing the all students' names whose average mark is prime number and whose last name (l_name) ends with "yan" what should I do? strstr() function does not help as it works from the first character and it's not excluded that "yan" can appear not at the end of string? I should check the ending #include <stdio.h> #include <string.h> #include <stdlib.h> struct student { char f_name[50]; char l_name[70]; int avg_mark; } void main() { int i,j; int n; scanf("%d", &n); student *x=(student*)malloc(n*sizeof(student)) for (i=0; i<n; i++) scanf ("%s%s%d", x[i].f_name, x[i].l_name, &x[i].avg_mark ); for (i=0;i<n;i++) for(j=2;j<=x[i].avg_mark/2;j++) if (x[i].avg_mark/j!=0) printf("%f", x[i].avg_mark); } thanks in advance

18th Dec 2018, 5:24 PM
Lilit
Lilit - avatar
3 ответов
+ 2
Use strcmp like this: if (strcmp(&x[i].l_name[strlen(n)-3], "yan") == 0) you must make sure strlen is 3 or more to prevent index error. strcmp returns negative for less than, zero for equal, and positive for greater.
18th Dec 2018, 6:35 PM
John Wells
John Wells - avatar
+ 2
That test will only check the last three characters of the name.
18th Dec 2018, 7:06 PM
John Wells
John Wells - avatar
0
The problem is if someone's last name is agyanban, the program should not print it But if it's agbanyan the program should print the frist name and last name on the desktop. It ends with yan
18th Dec 2018, 7:04 PM
Lilit
Lilit - avatar