Chapter 6.1 Array and String
Chapter 6.1 Array and String
Arrays
Array (11 Marks)
6.1 Introduction to an Array
6.2 One-dimensional Array
6.3 Two-dimensional Array
6.4 Multidimensional Array
6.5 Introduction to String
6.6 String and String Handling Functions
6.7 Array of String
6.7 Definition and Declaration of a Pointer
6.8 Types of Pointer
6.9 Pointer Arithmetic
6.10 Relationship between Pointer and Arrays
Background
• If we deal with similar type of variables in more number at a time, we
may have to write lengthy programs along with long list of variables.
• There should be more number of assignment statements in order to
manipulate on variables.
• When the number of variables increases, the length of program also
increase.
• In the above situations described above, where more number of same
types of variables is used, the concept of ARRAYS is employed in C
language.
• These are very much helpful to store as well as retrieve the data of
similar type of data.
•Why arrays is necessary?
•If we have to store information of 100 students.
•Without array:
int a1,a2,a3,a4,a5, … ,a100;
•With array:
int a[100];
a[0],a[1],…a[99]
Index 0-99
Array
• An array is the collective name given to a group of similar quantities.
• An array is a set of similar data elements which are stored in consecutive
memory locations under a common variable name.
• The individual values in an array are called elements.
• Array is sets of values of the same type, which have single name followed
by an index.
• The similar element of an array would be all integers or all floats or all
characters. Usually, the array of character is called string.
• The elements of an array are of same data type and each item can be
accessed using the same name.
Array Declaration
• Declaring the name and type of an array and setting the number of
elements in the array is known as dimensioning(declaring) the array.
• It must be declared before it is used like other variables.
• In the array declaration, we must define the type of the array, name of
the array and number of subscripts in the array. In general, one-
dimensional array may be expressed as:
Syntax: data_type array_name[size]; Similarly,
Declaring Arrays
Array variables are declared identically to variables of their data type,
except that the variable name is followed by one pair of square [ ]
brackets for each dimension of the array.
data_type array_name[array_size];
For example: if you want to store marks of 100 students, you can create an
array for it.
float marks[100];
The size and type of arrays cannot be changed after its declaration
Initialization of Array
• Arrays may be initialized when they are declared, just as any other variables.
• Place the initialization data in curly { } braces following the equals sign.
• An array may be partially initialized by providing fewer data items than the size of the
array. The remaining array elements will be automatically initialized to null values.
Initializing Arrays
➢
Arrays may be initialized when they are declared,
just as any other variables.
➢
Place the initialization data in curly { } braces following the
➢
equals sign. An array may be partially initialized, by providing
fewer data items than the size of the array. The remaining
array elements will be automatically initialized to zero.
➢
If an array is to be completely initialized, the dimension of the
array is not required.
int Arrayname[ 6 ] = { 1, 2, 3, 4, 5, 6 };
float Arrayname[ 100 ] = { 1.0f, 5.0f, 20.0f };
double piFractions[ ] = { 3.141592654, 1.570796327, 0.785398163 };
Compile Time Initialization
Syntax:
Data_Type array_name[]={list of arguments};
Example: int arrayname[ 6 ] = { 1, 2, 3, 4, 5, 6 };
Global and local initialization
Just like local and global variable initialization
Runtime Initialization
Data_type arrayname[];
arrayname[index]=value;
Array Memory Diagrams
Suppose the following array declaration and code to
initialize it.
int a[5]; // Allocates memory for 5 ints.
...
a[0] = 1;
for (int i=1; i<5; i++) {
a[i] = a[i-1] * 2;
}
• Strings are sequence of characters used in programming language for storing and
manipulating texts such as words, sentence, names etc.
• In C, there is no built-in data type for strings.
• String can be represented as a one-dimensional array of characters.
• A character string is stored in an array of character type.
• String should always have a NULL character (‘\0’) at the end, to represent the end of
string.
• The elements of the string are stored in continuous memory locations.
• When we initialize the string, the compiler automatically puts a null
character (‘\0’) at the end of last character of the array.
• The '\0' represents the end of the string. We can declare it as one or multi-
dimensional array. Recall : getchar(),putchar(),puts() and gets() function
• There are various built in functions related to the string. They are available
in string.h header file. Some of them are:
Declare, Initialize Array of Char Type
•A string variable is any valid C variable name and is always declared as an array.
The syntax of declaration of a string variable is:
• Data_type string-name[size];
The size determines the number of character in the string name
Example:
•An array of char type to store the string “Program” is to be declared
as follows:
char str[8];
An array of char is also called as a string variable, since it can store a string
and it permits us to change its contents. In contrast, a sequence of characters
enclosed within a pair of double quotes is called a string constant
Initialization of Arrays of char Type
The syntax of initializing a string variable has two
variations:
a. Variation 1
Here, str1 is declared to be a string variable with size six. It can store
maximum six characters. The initializer – list consists of comma
separated character constants. Note that the null character ‘\0’ is
clearly listed. This is required in this variation.
b. Variation 2
char str2 [6] = { “Hello” };
1. STRLEN (S1)
2. STRCMP (S1,
S2)
3. STRCPY (S1, S2)
4. STRCAT (S1, S2)
5. STRREV(S)
6. STRLWR(S)
7. STRUPR(S)
Strlen(s1)
This function is used to return the length of the string
name S1.
strcmp(S1, S2)
This is a library function used to perform comparison between two strings.
This function returns a value < zero when string S1 is less than S2, return a
value 0 whenS1=S2 and return a value > 0 when S1>S2.
strcpy (S1, S2)
This is a library function used to copy the string
S2 to S1
strcat(S1, S2)
This is a library functionused to join two strings one after the other. This
function concatenates string S2 at the end of string S1
strrev(s)
The strrev() function is used to reverse the given string.
Strlwr(s)
• The strlwr( ) function is a built-in function in C and is used to convert
a given string into lowercase.
• This is a non-standard function that works only with older versions of
Microsoft C.
• It returns the modified string obtained after converting the characters
of the given string “s” to lowercase.
• Same as lower() which is defined in <ctype.h>
Strupr(s1)
• The strupr( ) function is used to converts a given string to uppercase.
// Compare two string without using strcmp() function
#include <stdio.h>
int main()
{ if(c==0)
char s1[20],s2[20],c=0,i=0; printf("strings are
printf("Enter the first string : "); same");
scanf("%s",s1); else
printf("Enter the second string : "); printf("strings are
scanf("%s",s2); not same");
while(s1[i]!='\0' &&s2[i]!='\0') return 0;
{ }
if(s1[i]!=s2[i])
{
c=1;
break;
}
i++;
}
Example 1. WAP to reverse a string Example 2. WAP to reverse a string entered from
entered from keyboard using in build keyboard without using strrev() function
string function
Example 3. WAP to copy string Example 4. WAP to copy string from one array
from one array onto another onto another without using in build string function
using in build string function
Example 5. A program to calculate the Example 6. A program to calculate the length
length of the string entered from keyboard. of the string entered from keyboard.
#include <stdio.h>
int main()
{
char s[] = "Programming is fun";
int i;
}
Example 13: Program to print NEPAL as #include<stdio.h>
#include<conio.h>
#include<string.h>
int main()
{
char str[10]="NEPAL";
int i,j;
for(i=0;i<strlen(str);i++)
{
printf("%d",i+1);
for(j=0;j<=i;j++)
{
printf("%c",str[i]);
}
printf("\n");
}
getch();
return 0;
}
Example 14: Program to print pulchowk as
#include<stdio.h>
#include<conio.h>
#include<string.h>
int main()
{
int i,j,k=7;
char str[10]="pulchowk";
for(i=7;i>=0;i--)
{
for(j=k;j<=7;j++)
{
putchar(str[j]);
}
putchar('\n');
k--;
}
return 0;
}
Example 15: Program to print pulchowk as
#include<stdio.h>
#include<conio.h>
#include<string.h>
int main()
{
int i,j,k=7;
char str[10]="pulchowk";
for(i=7;i>=0;i--)
{
for(j=i;j>=0;j--)
{
putchar(' ');
}
for(j=k;j<=7;j++)
{
putchar(str[j]);
}
putchar('\n');
k--;
}
return 0;
}