Inpr Prelims Canvas
Inpr Prelims Canvas
Int array_name[][];
^ ^
Data type | name or identifier | and then 2 brackets
- The first bracket dictates the rows and the second bracket dictates the columns.
Visualization:
Int matrix[2][3] = {{1,2,3} , {3,4,5}};
Functions in C
- A function is a group of statement or a block of code that could perform a specific task.
- A function is sometimes referred as a method, a sub routine, or, a procedure
- Function allows programmers to divide complex programs into bits making the program
easy to understand, and allows reusability.
Types of Function
- Standard Library Function
o Are functions included in the programming language, in this case C.
o Examples: printf(); , scanf();,
- User defined function
o Function created by the user.
Declaring a Function
Int function_name (paramter1, parameter2, …){ //code block }
^ ^ ^
Return type | name or identifier | parameter list
Or
Int function_name(parameter1, parameter2, …){
//code block
}
Return type
- a function could return a value using “return;” at the end of the function
- The data type returned by the function depends on what is declared
- Common return type:
Void -> returns nothing
Int -> returns a whole number
Float & double -> returns decimal numbers
Boolean -> returns true or false
Parameter List
- Functions could take a value as it is being called. The values it can take depends on the
parameter list.
- A parameter is like a placeholder. It is like the x in a mathematical function ( F(x) ). In
mathematics, If we replace x with a value it will use the value according to the
expression.
- A parameter works similarly. It will take a value once it is used and depending on how
the function works it will process it.
- You first need to declare its data type and then name. You can use the parameter inside
the function by calling parameter_name.
Example of Functions
- Remember that functions are created OUTSIDE the “int main” function
- The addTwoValues creates a new variable called total, and then adds the 2 parameter
values.
- Then the addTwoValues function is called in the main function. As it being called it takes
the value of num1, and num2.
- Since the function returned a value, we can store function values inside a variable. In the
program above, it is stored in “result”.
- Then the variables are printed.
Output:
strcat(target, source)
Examples:
Output:
Example
Output
Strcopy()
- Copies string to the target
Strcopy(target, source)
Strlen()
- Gives the length of a given string and returns an integer
Strlen(string)
Strcmp()
- Compares two strings and return these values
Strcmp(string1, string2)
- -1 if the first string comes first alphabetically before the second string
- 0 if the second string comes first alphabetically before the first string.
- 1 if the string are equal
Strcmpi()
- Compares two strings but not case sensitive
Strcmpi(string1, string2)
Strchr()
- Returns pointer (in this case the index at which the character is found) to the first
occurrence of char in string
Strchr(string1, char)
Example
Char string1[20] = “hello word”
strchr(string1, e);
The code above will return number 1 since its found at 2nd place. (remember that strings
are arrays, therefore their index starts at 0)
Strrchar(string1, char);
Strstr()
- Returns the index of the last occurrence of given character in a string is found
Strstr(string1, string2)
- Example:
Char String1[20] = “I love you”
Char String2[20] = “love”
Strstr(String1, String2);
The strstr() will return the number 2 since the last “love” is in the 3rd character. (it
returns number 2 since index start at 0)
- Example 2:
Char String1[20] = “I love love you”
Char String2[20] = “love”
Strstr(String1, String2);
The strtstr() will return the number 7 since the last “love” character is found at the 8th
character.
Strdup()
- Duplicates string, which could be stored inside a pointer.
Strdup(string)
Strlwr()
- Converts all characters of a string to lowercase
Strlwr(string)
Strupr()
- Converts all characters of a string to uppercase
Strupr(string)
Strrev()
- Reverses the string
Strrev(string)
Strset()
- Sets all character in a string to a given character
Strset(string, char)
Strnset()
- Sets all character in a string to a given character and a given length
- Example:
Char string1[20] = “hello world”
Strnset(string1, “#”, 4);
Output: ###o world
Strtok()
- Tokenizes the given string using delimiter
Strtok(string, delimiter);
Example:
Strktok(string1, “ ”);
Output:
hello
world
- Strtok seperates the string hello world. It separates the word in newline by new spaces.