Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
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
avg = sum / 10 ;
printf ( "\nAverage marks = %d", avg ) ;
getch();
}
Question 2
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} };
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
}
printf("\n\n");
getch();
}
An entire array can be passed as argument to
function.
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()
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();
}