Sei sulla pagina 1di 58

 5.

1 Array-Concepts, Declaration, Definition,


Accessing array element, One-dimensional
and Multidimensional array.

 5.2 String- Basic of String, Array of String ,


Functions in String.h
 Array is the collection of
variables, which have
common data type and
characteristics.

 Each item in an array is


called an element.

 All elements in an array


are referenced by the
name of the array and
stored in a set of
consecutive, adjacent
memory slots.
Syntax:
data type array_name[capacity];

Where,
data type is the type of the data that will be stored
in array
array_name is the name of array
capacity known as subscript or element indicates
the number of variables to be grouped under one
name

e.g.
int roll_no[5];
float average[5];
 Declaring Arrays
type arrayName [ arraySize ];
e.g. double balance[10];

 Initializing Arrays

double balance[5] = {1000.0, 2.0, 3.4, 7.0, 50.0};


double balance[] = {1000.0, 2.0, 3.4, 7.0, 50.0};
balance[4] = 50.0;
#include <stdio.h>
#include<conio.h>
void main ()
{
int m[10]={1,2,3,4,5,6,7,8,9,10};
int i;
for (i = 0; i < 10; i++ )
{
printf("Element[%d] = %d\n", i, m[i] );
}
getch();
}
#include <stdio.h>
#include<conio.h>
void main ()
{
int n[ 10 ]; /* n is an array of 10 integers */
int i;

/* initialize elements of array n to 0 */

printf("enter data into array");


for ( i = 0; i < 10; i++ )
{
scanf("%d",&n[i]);
}
/* output each array element's value */

for (i = 0; i < 10; i++ )


{
printf("Element[%d] = %d\n", i, n[i] );
}
getch();
}
 Array which have only one subscript
(dimension) is called One dimensional array.
 Question 1

 Write a program to find average marks


obtained by a class of 10 students in a test.
#include<stdio.h>
#include<conio.h>
void main( )
{
int avg, i,sum = 0,;
int marks[10] ; /* array declaration */

for ( i = 0 ; i < 10 ; i++ )


{
printf( "\n Enter marks " ) ;
scanf( "%d", &marks[i] ) ; /* store data in array */
}

for ( i = 0 ; i < 10 ; i++ )


sum = sum + marks[i] ; /* read data from an array*/

avg = sum / 10 ;
printf ( "\nAverage marks = %d", avg ) ;
getch();
}
 Question 2

 WAP to find largest element of array


#include <stdio.h>
#include<conio.h>
void main()
{
int i, n;
float arr[100];
printf("Enter total number of elements: ");
scanf("%d", &n);
printf("\n");
// Stores number entered by the user
for(i = 0; i < n; ++i)
{
printf("Enter Number %d: ", i+1);
scanf("%f", &arr[i]);
}
// Loop to store largest number to arr[0]
for(i = 1; i < n; ++i)
{
if(arr[0] < arr[i])
arr[0] = arr[i];
}
printf("Largest element = %.2f", arr[0]);
getch();
}
 Arrays can have more than one
dimension (subscript), these
array of arrays is called
multidimensional arrays.

 They are very similar to


standard arrays with the
exception that they have
multiple sets of square brackets
after the array name.

 Syntax :
type name[size1][size2]...[sizeN];

 e.g.
int threedim[5][10][4];
 The simplest form of multidimensional array is
the two-dimensional array.
 A two-dimensional array is a list of one-
dimensional arrays.
 Syntax :
type arrayname[size_of_x][size_of_y];
Where, x is row
y is column
 E.g
int a[3][4];
 int a[3][4]={ {8,16,9,52},
{3,15,27,6},
{14,25,2,10} };

 int a[3][4]={ 8,16,9,52,


3,15,27,6,
14,25,2,10 };

 int a[3][4]={ 8,16,9,52,3,15,27,6,14,25,2,10 };


#include <stdio.h>
#include<conio.h>
void main ()
{
/* an array with 5 rows and 2 columns*/
int a[5][2] = { {0,0}, {1,2}, {2,4}, {3,6},{4,8}};
int i, j;
clrscr();

/* output each array element's value */


for ( i = 0; i < 5; i++ )
{
for ( j = 0; j < 2; j++ ) {
printf("a[%d][%d] = %d\n", i,j, a[i][j] );
}
}
getch();
}
 Example 1

 Write a program to store roll number and


marks obtained by a student side by side in a
matrix. (Accepting input by user)
#include<stdio.h>
#include<conio.h>
void main()
{
int stud[4][2];
int i,j;
clrscr();
for(i=0;i<=3;i++)
{
printf("enter roll no. and marks");
scanf("%d%d",&stud[i][0],&stud[i][1]);
}
printf("roll no.\t marks");
for(i=0;i<=3;i++)
{
printf("\n%d\t\t%d",stud[i][0],stud[i][1]);
}
getch();
}
 Question 1

 WAP for addition of two 2D matrices.


#include<stdio.h>
void main()
{
int a[3][3],b[3][3],c[3][3],i,j;
printf("Enter the First matrix->"); \\giving values for first matrix
for(i=0;i<3;i++)
for(j=0;j<3;j++)
scanf("%d",&a[i][j]);

printf("\nEnter the Second matrix->"); \\giving values for second matrix


for(i=0;i<3;i++)
for(j=0;j<3;j++)
scanf("%d",&b[i][j]);

printf("\nThe First matrix is\n"); \\printing values for first matrix


for(i=0;i<3;i++){
printf("\n");
for(j=0;j<3;j++)
printf("%d\t",a[i][j]);
} //contd…
printf("\nThe Second matrix is\n"); \\printing values for second matrix
for(i=0;i<3;i++)
{
printf("\n");
for(j=0;j<3;j++)
printf("%d\t",b[i][j]);
}

for(i=0;i<3;i++)
for(j=0;j<3;j++)
c[i][j]=a[i][j]+b[i][j]; \\ Addition of matrices

printf("\nThe Addition of two matrix is\n"); \\printing values for result matrix
for(i=0;i<3;i++)
{
printf("\n");
for(j=0;j<3;j++)
printf("%d\t",c[i][j]);
}
}
 Question 2

 WAP to find transpose of a two dimensional


matrix.
#include <stdio.h>
#include<conio.h>
void main()
{
int a[10][10], transpose[10][10], r, c, i, j;
printf("Enter rows and columns of matrix: ");
scanf("%d %d", &r, &c);

// Storing elements of the matrix


printf("\nEnter elements of matrix:\n");
for(i=0; i<r; ++i)
for(j=0; j<c; ++j)
{
printf("Enter element a%d%d: ",i+1, j+1);
scanf("%d", &a[i][j]);
}

// Displaying the matrix a[][] */


printf("\nEntered Matrix: \n");
for(i=0; i<r; ++i)
for(j=0; j<c; ++j)
{
printf("%d ", a[i][j]);
}
printf("\n\n");
//contd…
// Finding the transpose of matrix a
for(i=0; i<r; ++i)
for(j=0; j<c; ++j)
{
transpose[j][i] = a[i][j];
}

// Displaying the transpose of matrix a


printf("\nTranspose of Matrix:\n");
for(i=0; i<c; ++i)
for(j=0; j<r; ++j)
{
printf("%d ",transpose[i][j]);

}
printf("\n\n");

getch();
}
 An entire array can be passed as argument to
function.

 To pass an array to function while calling


function, the array name must appear by
itself without bracket and index as an
argument in function call.

 The function declaration should have an array


declaration as argument with square
brackets.
#include<stdio.h>
#include<conio.h>
void fun(int arr[])
{
int i;
for(i=0;i< 5;i++)
arr[i] = arr[i] + 10;
}
void main()
{
int arr[5],i;
clrscr();
printf("\nEnter the array elements : ");
for(i=0;i< 5;i++)
scanf("%d",&arr[i]);
printf("\nPassing entire array .....");
fun(arr); // Pass only name of array
for(i=0;i< 5;i++)
printf("\nAfter Function call a[%d] : %d",i,arr[i]);
getch();
}
 Question 1

 WAP to find average of the array element by


passing array as argument to the function.
#include <stdio.h>
#include<conio.h>
double getAverage(int arr[], int size);
int main ()
{
int balance[5] = {1000, 2, 3, 17, 50};
double avg;
clrscr();
avg = getAverage( balance, 5 ) ;
printf( "Average value is: %f ", avg );
getch();
return 0;
}
double getAverage(int arr[], int size)
{
int i;
double avg;
double sum;
for (i = 0; i < size; i++)
{
sum += arr[i];
}
avg = sum / size;
return avg;
}
 String is one dimensional array of characters,
terminated by a null(‘\0’) character.

 Character arrays are many times also called


as strings.

 Strings are used to store text information and


to perform manipulation on them.
 Character array can be initialized in two ways
as individual characters or as a single string.

Ex.
char name[]={‘H’, ’e’, ’l’, ’l’, ’o’,’\0’};

char name[]=“Hello”;
#include<stdio.h>
#include<conio.h>
void main()
{
char name[20];
char greet[6]={'h','e','l','l','o','\0'};
int i;
clrscr();
for(i=0;i<=5;i++)
printf("%c",greet[i]);
printf("enter the string :");
scanf("%s",name);
printf("entered string is : %s",name);
getch();
}
 A string is an array of characters; so, an array
of strings is an array of arrays of characters.
 The maximum size is the same for all the
strings stored in a two dimensional array.
#include<stdio.h>
#include<conio.h>
void main()
{
int i;
char s1[10][30];
char
s2[5][10]={"cow","goat","horse","dog","cat"};
clrscr();
printf("enter the strings:");
for(i=0;i<5;i++)
{
scanf("%s",s1[i]);
}
printf("\nentered array of the strings:");
for(i=0;i<5;i++)
{
printf("\n%s",s1[i]);
}
printf(" \narray of the strings s2 is:");
for(i=0;i<5;i++)
{
printf("\n%s",s2[i]);
}
getch();
}
 The string functions allow us to perform various
operation or manipulation on string.
 To use these functions, the header file string.h
must be included in the program.
 Commonly used string manipulation functions are:
◦ strlen( )
◦ strcat( )
◦ strcpy( )
◦ strcmp( )
◦ strupr( )
◦ strlwr( )
◦ strrev( )
strlen()

 The strlen( ) function determines length of


the string.
 The length of string is the number of
characters present in it, excluding the
terminating null character.
 Syntax :
strlen(string);
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char string1[10];
int length;
printf("enter a string : ");
scanf("%s",string1);
length=strlen(string1);
printf("The length of string is : %d",length);
getch();
}
strcat()
 The strcat() function accepts two stings as
parameters and concatenates them.
 It appends the source string at the end of target
string.
 Syntax : strcat(target,source);

strncat()
 Concatenates the content of two string upto length n.
 It appends the source string at the end of the target
string upto length n.
 Syntax: strncat(target,source,n)
#include <stdio.h>
#include <string.h>
#include<conio.h>
void main ()
{
char s1[15]="programming";
char s2[15]="language";
char s3[15]="hello";
char s4[15]="world";
clrscr();
strcat(s1,s2);
printf("\nAfter concatenation string is :%s",s1);
strncat(s3,s4,3);
printf("\nAfter concatenation string is :%s",s3);
getch();
}
strcpy()
 The strcpy() function copies content of one string to
another string.
 It copies the source string character by character into
the target string.
 Syntax : strcpy(target,source);

strncpy()
 The strncpy() function copies content of one string to
another upto length n of source string.
 Syntax: strncpy(target,source,n)
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char string1[]="Hello";
char string2[]="world";
char string3[]="ABCD";
char string4[]="PQRS";
clrscr();
printf("\nBefore copying two strings are :%s\t%s",string1,string2);

strcpy(string1,string2);
printf("\nAfter copying two strings are :%s\t%s",string1,string2);
printf("\nBefore copying two strings are :%s\t%s",string3,string4);

strncpy(string3,string4,2);
printf("\nAfter copying two strings are :%s\t%s",string3,string4);
getch();
}
 The strrev( ) function is use to convert the
string content in the reverse order.
 Syntax :
strrev(string);
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{

char s1[10];
clrscr();
printf("enter string :");
scanf("%s",s1);
printf("\nreverse of the string is : %s",strrev(s1));
getch();
}
strcmp()
 The strcmp() function is used to compare
two strings character by character.
 The function accepts two strings as
parameters and returns as integer value.
 The comparision continues until any
character differs or end of string is reached.
 Syntax : strcmp(string1,string2);
Return value Description
Less than 0 If string1 is less than string2
Equal to 0 If string1 and string2 are identical
Greater than 0 If string1 is greater than string2

strncmp()
 The strncmp() function is used to compare two
strings upto length n.
 Syntax: strncpy(string1,string2,n)
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char string1[]="New Delhi";
char string2[]="New York";
int x,y;
clrscr();
x=strcmp(string1,string2);
if(x==0)
printf("string1=string2");
else if(x<0)
printf("string1<string2");
else
printf("string1>string2");

y=strncmp(string1,string2,3);
if(y==0)
printf("\nboth the strings are equal");
else
printf("\nboth the strings are not equal");
getch();
}
strcmpi()
 The strcmpi() function is used to compare two
strings without considering case i.e. ignores
the case of characters.
 The comparison continues until any character
differs or end of string is reached.
 Syntax : strcmpi(string1,string2);

strncmpi()
 The strncmpi() function is used to compare two
strings without considering case upto length n.
 Syntax: strncmpi(string1,string2,n)
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char s1[]="Good";
char s2[]="Day";
int r;
clrscr();
r=strcmpi(s1,s2);
if(r==0)
printf("\nstrings are equal");
else
printf("\nstrings are not equal");
r=strncmpi(s1,s2,2);
if(r==0)
printf("\nstrings are equal");
else
printf("\nstrings are not equal");
getch();
}
strupr()
 The strupr() function is use to convert
characters in string to uppercase.
 Syntax : strupr(string)

strlwr()
 The strlwr() function is use to convert
character in string to lowercase.
 Syntax: strlwr(string)
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char name[10];
clrscr();
printf("enter name: ");
scanf("%s",name);
printf("\nThe string in uppercase is : %s",strupr(name));
printf("\nThe string in lowercase is : %s",strlwr(name));
getch();
}
strchr()
 The strchr() function is used to find first occurrence
of a given character in a string.
 The function will return a pointer to the character
or NULL if the character is not found.
 Syntax : strchr(string,char)

strrchr()
 The strrchr() function is used to find last occurrence
of a given character in a string.
 Syntax : strrchr(string,char)
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char s1[]="Good";
char ch='o';
int r;
clrscr();

printf("%s",strchr(s1,ch));
printf("\n%s",strrchr(s1,ch));
getch();
}
strstr()
 The strstr() function is used to find first
occurrence of a given string in a another
string.
 The function will return a pointer to the
string or NULL if the string is not found.
 Syntax : strstr(string1,string2)
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char s1[]="Good Day";
char s2[]="od";
clrscr();
printf("%s",strstr(s1,s2));
getch();
}
strset()
 The strset() function is used to set all
characters of string to a given character.
 Syntax : strset(string1,char)

strnset()
 The strnset() function is used to set all
characters of string to a given character upto
length n.
 Syntax : strnset(string1,char,n)
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char s1[]="Good";
char s2[]="hello";
char ch='X';
int n=2;
clrscr();
strset(s1,ch);
printf("string is : %s",s1);

strnset(s2,ch,n);
printf("string is : %s",s2);
getch();
}

Potrebbero piacerti anche