0% found this document useful (0 votes)
6 views14 pages

2 Array

The document explains the concepts of arrays and strings in programming, detailing their definitions, memory allocation, and how to access and manipulate their elements. It covers one-dimensional and two-dimensional arrays, including examples of how to find their sizes and the total number of elements. Additionally, it discusses string manipulation functions in C and provides examples for string length calculation and concatenation without using built-in functions.

Uploaded by

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

2 Array

The document explains the concepts of arrays and strings in programming, detailing their definitions, memory allocation, and how to access and manipulate their elements. It covers one-dimensional and two-dimensional arrays, including examples of how to find their sizes and the total number of elements. Additionally, it discusses string manipulation functions in C and provides examples for string length calculation and concatenation without using built-in functions.

Uploaded by

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

ARRAYS AND STRINGS

WHY ARRAY?
Many times we need to work with a number of variables of similar data
type that needs similar manipulation. In such case, it may be difficult to
declare multiple variables with different names and manipulate them
separately.

WHAT IS AN ARRAY?
An array can be defined as fixed number of memory locations, each of
which can store the same data type and which can be referenced through
the same variable name.
ACCESSING AN ARRAY ELEMENT
Array element can be accessed using index
Index ranges from 0 to [size of array -1]
ARRAY AND MEMORY
When the array declaration statement is encountered, memory
required for the array is calculated and contiguous memory space is
allocated.
For example, assuming, size of an int is 4 bytes, for int size[4] 4*4 =16
bytes continuous memory space are allocated.
TWO DIMENSIONAL ARRAY(2D)
• Two dimensional array is like a table containing elements in the
intersection of a row and a column.
• The best example of two dimensional array is a matrix.
2D ARRAY IN MEMORY
How to find no of rows and columns in a 2D array?
#include <stdio.h>
int main()
{
int a[2][3];
int i,j,s,row,col; Output
for(i=0;i<2;i++) 8
6
{
4
for(j=0;j<3;j++) 5
{ 2
=(6*4)/(3*4)
scanf("%d",&a[i][j]); 1
=24/12
} size of array row is 2
=2
size of array col is 3
}
s=sizeof(a);
row=sizeof(a)/sizeof(a[0]);
col=sizeof(a[0])/sizeof(a[0][0]);
=(3*4)/(1*4)
printf("size of array row is %d\n",row);
=12/4
printf("size of array col is %d",col); =3
return 0;
}
How sizeof() operator works
with multi-dimensional array?
#include <stdio.h>
int main()
Output
{
int a[2][2]={1,2,3,4}; 1
int i,j,s; 2
for(i=0;i<2;i++) 3
{ 4
size of array is 16
for(j=0;j<2;j++)
{
printf("%d\n",a[i][j]);
}
}
s=sizeof(a);
printf(“size of array is %d",s);
return 0;
}
How to find the total number of elements
is 2D array?
#include <stdio.h>
int main()
{
int a[2][2]={1,2,3,4};
int i,j,n; Output
for(i=0;i<2;i++) 1
{ 2
3
for(j=0;j<2;j++)
4
{ number of elements in
printf("%d\n",a[i][j]); this array is 4
}
}
n=sizeof(a)/sizeof(a[0][0]);
printf(“number of elements in this array is %d",n);

return 0;
}
STRINGS
• String is sequence or collection of characters terminated by
null character.
• Null terminates the string but not part of it.
• Strings are accessed through arrays/ pointer .
• String.h contains prototypes of many useful functions.
• A special character, called a “null”, marks the end.
• This may be written as ’\0’.
• This is the only character whose ASCII value is zero.
• Depending on how arrays of characters are built, we may need
to add the null by hand , or the compiler may add it for us.
EXAMPLE
If we happen to declare a string like this :
char my_drink[3]= “tea”;
We will get the following syntax error:
error c2117:’tea’: array bound overflow .
Instead , we need to declare the array with
(the size of string +1) to accommodate the null terminate
character ’\0’.

char my_drink[4]=“tea”;

Therefore , if any array of characters is to be used to store a


string, the array must be large enough to store the string and its
terminating NULL character.
String Manipulation in C
These functions are defined in string.h header file. Hence we need to include this header file.
• strlen Finds length of a string
• strlwr Converts a string to lowercase
• strupr Converts a string to uppercase
• strcat Appends one string at the end of another
• strncat Appends first n characters of a string at the end of another
• strcpy Copies a string into another
• strncpy Copies first n characters of one string into another
• strcmp Compares two strings
• strncmp Compares first n characters of two strings
• strcmpi Compares two strings without regard to case ("i" denotes that this function ignores
case)
String Manipulation in C

• stricmp Compares two strings without regard to case (identical to strcmpi)


• strnicmp Compares first n characters of two strings without regard to case
• strdup Duplicates a string
• strchr Finds first occurrence of a given character in a string
• strrchr Finds last occurrence of a given character in a string
• strstr Finds first occurrence of a given string in another string
• strset Sets all characters of string to a given character
• strnset Sets first n characters of a string to a given character
• strrev Reverses string
• gets() and puts() are two string functions to take string input from the user and display it
respectively
Finding length of a string without
strlen()
#include <stdio.h>
scanf("%[^\n]s",a);
int main()
{ This reads a
char a[]="Welcome Home"; string until user
inputs a new line
int i,count=0; character,
for(i=0;a[i]!='\0';i++) considering the
{ white spaces also
as string.
count=count+1;
} Output
printf(“the length is %d",count); The length
return 0; is 12

}
Concatenation without strcat()
#include<stdio.h>
int main()
{
char str1[20]="welcome";
char str2[20]="home";
int i,j;
for(i=0;str1[i]!='\0';i++)
continue;
for(j=0;str2[j]!='\0';j++)
{ Output
str1[i+j]=str2[j]; Welcomehome – after concatenation

}
printf("%s - after concatenation",str1);
return 0;
}
THANK YOU

You might also like