Remember that in C an array is simply a list of characters with a \0 at the end (['1', 'a', '\0'] for the first string).
Your program should print out just the 0th, 2nd and 5th strings.
Exercise: Write a program that fills the array with scanf. Here we suppose that the array has 7 elements, and each element is string of 2 characters taken from stdin.
e.g. the input 1a2b3c4d5e6f7g will produce the array of strings
["1a", "2b", "3c", "4d", "5e", "6f", "7g"]
Reversing words in C
void name_reversal(char *rev, char *src){
//Get the length of the words
int len=strlen(src);
for(int i=0;i<len;i++){
//Traverse through the source character backwards
rev[i]=src[len-i-1];
}
//End of the reverse character
rev[len] = '\0';
}
times= int(input("How many rows you want? "))
for i in range(times+1):
k=times-i
#Print out space
for j in range(k):
print(end=" ")
#Print out the stars
for c in range(1,2*i):
print(end="*")
print("\n")
Object Oriented Programming principles:
Abstraction- Create new data types that are suitable for problems as needed.
Polymorphism- Ability to process objects differently depending on their type of data or class.
Inheritance- A form of abstraction that permits “generalisation” of similar attributes/methods of classes.
Encapsulation(low lvl) - Wrap the data (attributes) and code acting on data(methods) together as a single unit(class).
Interface segregation principle- No client should be forced to depend on methods it doesn’t used.
e.g. 3D printer doesn’t need to scan or fax, it only needs to print.
· Breaks interface down into multiple smaller and specific ones.
Liskov substitution principle- Child class should be used where a parent class is expected.
e.g. Child class can’t work or drive like parent class hence there should be extended from a human class.
· Prevents introduction of bugs
Solutions:
· Use Decorator pattern
- Adding functionality either before or after existing code.
- Creates a new class that implements the same interface
- Can use dependency injection in run time to determine if new class is use.
Open closed principle- Code should be extension but closed to be modification.
· If you need to add some additional functionality to an application you shouldn’t be editing existing classes or methods to do that.
-Source code of a method shouldn’t be altered, but extended