0% found this document useful (0 votes)
18 views27 pages

Unit 3

This document provides an overview of arrays and strings in C programming, including their declaration, initialization, and manipulation. It covers one-dimensional and two-dimensional arrays, as well as string handling techniques such as accessing, modifying, and using string functions. Additionally, it highlights the differences between sizeof and strlen, and demonstrates how to take string input from users.

Uploaded by

ccpatil.24
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)
18 views27 pages

Unit 3

This document provides an overview of arrays and strings in C programming, including their declaration, initialization, and manipulation. It covers one-dimensional and two-dimensional arrays, as well as string handling techniques such as accessing, modifying, and using string functions. Additionally, it highlights the differences between sizeof and strlen, and demonstrates how to take string input from users.

Uploaded by

ccpatil.24
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/ 27

Unit III

Array & String in C


Dr. Prashant B Wakhare
Mob NO-7020880915
Mail Id: [email protected]
https://www.linkedin.com/in/pbwakhare/
Syllabus
• Array-Declaration, Initialization, Two-
Dimensional Arrays, Multi-Dimensional Array
• String-Declaration and Initialization of Strings,
Array of Strings, String functions.
Arrays
1. Arrays are used to store multiple values in a single
variable, instead of declaring separate variables for
each value.
2. To create an array, define the data type (like int) and
specify the name of the array followed by square
brackets [].
3. To insert values to it, use a comma-separated list,
inside curly braces:
e.g.
int myNumbers[] = {25, 50, 75, 100};
We have now created a variable that holds an array of four integers.
Access the Elements of an Array
• To access an array element, refer to its index number.
• Array indexes start with 0: [0] is the first element. [1] is
the second element, etc.
• This statement accesses the value of the first element
[0] in myNumbers:
• E.g.
int myNumbers[] = {25, 50, 75, 100};
printf("%d", myNumbers[0]);

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

for (i = 0; i < 4; i++) {


printf("%d\n", myNumbers[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 matrix[2][3] = { {1, 4, 2}, {3, 6, 8} };


Access the Elements of a 2D Array
• To access an element of a two-dimensional array, you must
specify the index number of both the row and column.
• This statement accesses the value of the element in
the first row (0) and third column (2) of the matrix array.
#include <stdio.h>

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

• Strings are used for storing text/characters.


• For example, "Hello World" is a string of characters.
• Unlike many other programming languages, C does not have a String type to easily create
string variables. Instead, you must use the char type an create array of characters to make a
string in C

char msg[] = “AISSMS IOIT!";

• Note that you have to use double quotes (“ “)

• 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

• To change the value of a specific character in a string, refer to the index


number, and use single quotes.
#include <stdio.h>

int main() {
char greetings[] = "Hello World!";
greetings[0] = 'J';
printf("%s", greetings);
return 0;
}
Loop Through a String

• You can also loop through the characters of a


string, using a for loop,

#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 concatenate (combine) two strings, you can use


the strcat() function
#include <stdio.h>
#include <string.h>
int main() {
char str1[20] = "Hello ";
char str2[] = "World!";
// Concatenate str2 to str1 (the result is stored in str1)
strcat(str1, str2);
// Print str1
printf("%s", str1);
return 0;
}
Copy 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

• To compare two strings, you can use the strcmp() function.


• It returns 0 if the two strings are equal, otherwise a value that is
not 0

#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];

printf("Type your full name: \n");


scanf("%s", &fullName);

printf("Hello %s", fullName);

// Type your full name: John Doe


// Hello John
Take String Input
• when working with strings, we often use the fgets() function to read a line of text. Note
that you must include the following arguments: the name of the string variable, sizeof
(string_name), and stdin.
#include <stdio.h>
int main() {
// Create a string
char fullName[30];
// Ask the user to input some text (full name)
printf("Type your full name and press enter: \n");
// Get the text
fgets(fullName, sizeof(fullName), stdin);
// Output the text
printf("Hello %s", fullName);
return 0;
}

You might also like