Structs & functions
#include <stdio.h>
#include <string.h>
int stringsTotalLength(char* firstString, char* secondString) {
int totalLength = strlen(firstString) + strlen(secondString);
return totalLength;
}
struct Person {
char firstName[100];
char lastName[100];
int age;
};
int personNameLength(struct Person person) {
return totalLengthOfStrings(person.firstName, person.lastName);
}
int didPersonOnlyProvideLastNameInitials(struct Person person) {
return (strlen(person.lastName) <= 3);
}
int main(void) {
// char firstName[] = "Suresh";
// char lastName[] = "Borra";
// int age = 31;
struct Person person1 = {
"Suresh",
"B",
31
};
// int nameLength = stringsTotalLength(person1.firstName, person1.lastName);
int nameLength = personNameLength(person1);
printf("Name length is %d\n", nameLength);
if (didPersonOnlyProvideLastNameInitials(person1)) {
printf("Person has only provided their last name initials");
}
return 0;
}
Classes, properties, functions and methods
class Person {
constructor(firstName, lastName, age, father) {
this.firstName = firstName;
this.lastName = lastName,
this.age = age;
this.father = father;
}
isMinor() {
return (this.age < 18);
}
}
function isPersonMinor(person) {
return (person.age < 18);
}
father = new Person("Venkata Raju", "Borra", "60", null);
son = new Person("Suresh", "Borra", 31, father);
console.log(son.firstName);
console.log(son.lastName);
console.log(son.father.firstName);