Unit 3
Unit 3
// Outputs 25
Change an Array Element
#include <stdio.h>
int main()
{
int myNumbers[] = {25, 50, 75, 100};
myNumbers[0] = 33;
printf("%d", myNumbers[0]);
return 0;
}
Loop Through an Array
• You can loop through the array elements with the for loop.
#include <stdio.h>
int main() {
int myNumbers[] = {25, 50, 75, 100};
int i;
return 0;
}
Set Array Size
#include <stdio.h>
int main() {
// Declare an array of four integers:
int myNumbers[4];
// Add elements to it
myNumbers[0] = 25;
myNumbers[1] = 50;
myNumbers[2] = 75;
myNumbers[3] = 100;
printf("%d\n", myNumbers[0]);
return 0;
}
Multidimensional Arrays
• If you want to store data as a tabular form, like
a table with rows and columns, you need to
get familiar with multidimensional arrays.
• A multidimensional array is basically an array
of arrays.
Two-Dimensional Arrays
• A 2D array is also known as a matrix (a table of
rows and columns).
int matrix[2][3] = { {1, 4, 2}, {3, 6, 8} };
• The first dimension represents the number of
rows [2], while the second dimension
represents the number of columns [3].
Two-Dimensional Arrays
int main() {
int matrix[2][3] = { {1, 4, 2}, {3, 6, 8} };
printf("%d", matrix[0][2]);
return 0;
}
Change Elements in a 2D Array
#include <stdio.h>
int main()
{
int matrix[2][3] = { {1, 4, 2}, {3, 6, 8} };
matrix[0][0] = 9;
printf("%d", matrix[0][0]); // Now outputs 9 instead of 1
return 0;
}
Loop Through a 2D Array
To loop through a multi-dimensional array, you need one loop for each of the array's
dimensions.
#include <stdio.h>
int main()
{
int matrix[2][3] = { {1, 4, 2}, {3, 6, 8} };
int i, j;
for (i = 0; i < 2; i++) {
for (j = 0; j < 3; j++) {
printf("%d\n", matrix[i][j]);
}
}
return 0;
}
String
String-Declaration and Initialization
of Strings, Array of Strings, String
functions
C Strings
• To output the string, you can use the printf() function together with the format specifier %s
to tell C that we are now working with strings:
C Strings
#include <stdio.h>
int main()
{
char msg[] = "AISSMS IOIT!";
printf("%s", msg);
return 0;
}
Access Strings
• Since strings are actually arrays in C, you can access a string by referring to its index number inside
square brackets []
• This example prints the first character (0)
#include <stdio.h>
int main()
{
char msg[] = "AISSMS IOIT!";
printf("%c", msg[0]);
return 0;
}
Modify Strings
int main() {
char greetings[] = "Hello World!";
greetings[0] = 'J';
printf("%s", greetings);
return 0;
}
Loop Through a String
#include <stdio.h>
int main() {
char carName[] = "VENUE";
int i;
for (i = 0; i < 5; ++i) {
printf("%c\n", carName[i]);
}
return 0;
}
C String Functions
• C also has many useful string functions, which can be used to perform certain operations on strings.
• To use them, you must include the <string.h> header file in your program:
• For example, to get the length of a string, you can use the strlen() function:
#include <stdio.h>
#include <string.h>
int main() {
char alphabet[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
printf("%d", strlen(alphabet));
return 0;
}
Difference between sizeof and strlen
#include <stdio.h>
#include <string.h>
int main() {
char alphabet[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
printf("Length is: %d\n", strlen(alphabet));
printf("Size is: %d\n", sizeof(alphabet));//also includes the \0 character when counting
return 0;
}
//output
Length is: 26
Size is: 27
// It is also important that you know that sizeof will always return the memory size (in bytes), an not the
actual string length
Concatenate Strings
• To copy the value of one string to another, you can use the
strcpy() function
#include <stdio.h>
#include <string.h>
int main() {
char str1[20] = "Hello World!";
char str2[20];
// Copy str1 to str2
strcpy(str2, str1);
// Print str2
printf("%s", str2);
return 0;
}
Compare Strings
#include <stdio.h>
#include <string.h>
int main() {
char str1[] = "Hello";
char str2[] = "Hello";
char str3[] = "Hi";
// Compare str1 and str2, and print the result
printf("%d\n", strcmp(str1, str2));
// Compare str1 and str3, and print the result
printf("%d\n", strcmp(str1, str3));
return 0;
}
Take String Input
#include <stdio.h>
int main() {
// Create a string
char firstName[30];
// Ask the user to input some text (name)
printf("Enter your first name and press enter: \n");
// Get and save the text
scanf("%s", firstName);
// Output the text
printf("Hello %s", firstName);
return 0;
}
Note that you must specify the size of the string/array (we used a
very high number, 30, but atleast then we are certain it will store
enough characters for the first name), and you don't have to specify
the reference operator (&) when working with strings in scanf()
Take String Input
• The scanf() function has some limitations: it considers space (whitespace, tabs, etc) as a
terminating character, which means that it can only display a single word (even if you type
many words). For example:
char fullName[30];