0% found this document useful (0 votes)
20 views11 pages

Inpr Prelims Canvas

Two-dimensional arrays are arrays that can store other arrays. They are defined using two sets of brackets and can be accessed using nested for loops. Common string functions in C include gets() and puts() to get and print strings, strcat() to concatenate strings, and strlen() to return the length of a string. Other useful string functions are strcpy(), strcmp(), strchr(), strrev(), and strtok().

Uploaded by

jayceelunaria7
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views11 pages

Inpr Prelims Canvas

Two-dimensional arrays are arrays that can store other arrays. They are defined using two sets of brackets and can be accessed using nested for loops. Common string functions in C include gets() and puts() to get and print strings, strcat() to concatenate strings, and strlen() to return the length of a string. Other useful string functions are strcpy(), strcmp(), strchr(), strrev(), and strtok().

Uploaded by

jayceelunaria7
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

INPR Prelims -> Summarized CANVAS PPT

Two – Dimensional Array


- Defined as an array inside an array.
- Initialized by:

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}};

➢ The array’s name is matrix. It spans 2 rows and 3 columns.


➢ We could also read it as an array that could store 2 arrays inside and those 2 arrays can
store 3 elements.
Table:

matrix[][0] matrix[][1] matrix[][2]


Matrix[0][] 1 2 3
Matrix[1][] 3 4 5

- two-dimensional arrays are typically accessed using a nested for loop.


- One loop will loop through the rows, and another will loop through the columns
Example:
Output:

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 can create a parameter by:

Int function_name (data_type parameter_name, int x) {


//code block
}

- 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

- We created a function named “addTwoValues” that returns an integer and needs 2


integer parameter values.

- The addTwoValues creates a new variable called total, and then adds the 2 parameter
values.

- It then returns the total.

- Then the addTwoValues function is called in the main function. As it being called it takes
the value of num1, and num2.

- What happens inside the function is:

1. Total = a + b is replaced by Total= num1 +num2 -> total = 5+7


2. Total = 12
3. Then the function returns total which has a value of 12.

- 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:

Parameters: Additional Information


- Actual Parameters : are parameters passed to a function
o In the case of the code above, the num1 and num2 are the actual parameters.
- Formal parameter: are parameters received by a function
o In the case of the code above, the int a and int b are the formal parameters.
Important points about Function
- Every C has a function called main(). This the function called by the operating system
when the user runs the program.
- Every function has a return type
- Functions can return any type except arrays and functions
- Function could have an empty parameter list, which can be called without any
parameters.
- In a C program, a function is called before its declaration
Return Statement
- Returns a value according to the data type of the function
- Return statement TERMINATES the function, and the program is transferred back to the
line it is being called.
String Function
- Strings in C are an array of characters.
- The length of string is determined by a terminating null character: ‘\0\’. So, strings with
the contents, say ‘abc’ has 4 characters: ‘a’, ‘b’, ‘c’, and the null character.
- Since programming strings are cumbersome, it has its own standard library : <string.h>
Gets() and Puts()
- gets(string) : gets the string value and store it
- puts(string): prints the last string stored by the gets() function
Example:
Strcat()
- concatenates / append / puts together 2 strings.

strcat(target, source)

Examples:

Output:

Strncat() [Str + n + cat ()]


- strncat() appends a portion of the source to the target

strncat(target, source, length)

Example
Output

Strcopy()
- Copies string to the target

Strcopy(target, source)

- Replaces the target with the source.


Strncopy() [with n]
- Copies string to the target with a specific length

Strncopy(target, source, length)

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)

Strrchr() [with extra r]


- Returns the pointer(index of the character) to the last occurrence of given character

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

Strnset(string, char, length)

- Example:
Char string1[20] = “hello world”
Strnset(string1, “#”, 4);
Output: ###o world

Strtok()
- Tokenizes the given string using delimiter

Strtok(string, delimiter);
Example:

Char string1[20] = “Hello world”

Strktok(string1, “ ”);

//the delimiter is spaces

Output:
hello
world

- Strtok seperates the string hello world. It separates the word in newline by new spaces.

You might also like