BPOPS103 Mod3@AzDOCUMENTS - in
BPOPS103 Mod3@AzDOCUMENTS - in
Moodabidri. 22POP13
22POP13
C-Programming for Problem solving -21PSP13/23
MODULE-3
ARRAYS AND STRINGS
Why Arrays?
#include<stdio.h>
void main()
{
int number1;
int number2;
int number3;
int number4;
int number5;
number1 = 10;
number2 = 20;
number3 = 30;
number4 = 40;
number5 = 50;
printf( "number1: %d \n", number1);
printf( "number2: %d \n", number2);
printf( "number3: %d \n", number3);
printf( "number4: %d \n", number4);
printf( "number5: %d ", number5);
}
It was simple, because we had to store just 5 integer numbers. Now let's assume we have to
store 5000 integer numbers, so what is next???
Types of array
Single Dimensional Array :- An Array which has only one subscript is known as Single
dimensional array or One dimensional array
The individual array elements are processed by using a common array name with different
index values that start with Zero and ends with array_size-1
data_type array_name[array_size];
where
array_size : an integer constant indicating the maximum number of data elements to be stored.
Array Representation
and so on..
Example:
int b[4]={10,12,14,16};
Here each value will be stored in respective index values of the array.
i.e in location b[0] we store the value 10, in location b[1] we store the value 12 and so on…
Suppose if we try to insert more values then the size of the array it will give us an error “Excess
elements in array initializer”
Example:
int b[4]={10,12,14,16,18};
Here the size of the array b is 4 but we are trying to store 5 values hence we will be getting the
error in this case.
2. Initialization of array elements one by one:- Here the user has the liberty to select the
locations and store values and the array. This type of initialization is not used much practically
Example
int b[4];
b[0]= 10;
b[2]=14;
Only the array locations specified by the user will contain the values which the user wants the
other locations of array will either be 0 or some garbage value.
Example:
int b[4]={10,12};
4. Array initialization without specifying the size :- Here the size or the array is not specified
by the user, the compiler will decide the size based on the number of values declared in the
array.
Example:
int b[ ]={6,12,18};
Here the size of the array is specified and the compiler will set the array size as 3 for this
example
5. Run Time array Initialization:- If the values are not known by the programmer in advance
then the user makes use of run time initialization. It helps the programmer to read unknown
values from the end users of the program from keyboard by using input function scanf().
Here we make use of a looping construct to read the input values from the keyboard and store
them sequentially in the array.
We can access the elements of array using index or subscript of element. An index gives the
portion of element in the array .To access an array element make use of array_name[index]
To access value 16 we write b[2]=16 similarly if we wish to print the value 18 we write
printf(“%d”,b[3]);
#include<stdio.h>
void main()
{
int a[20 ],n,i;
printf(“Enter the array size”);
scanf(“%d”,&n);
Printf(“Enter the array elements\n”);
for(i=0;i<n;i++)
{
scanf(“%d”,&a[i]);
}
printf(“The elements entered in the array are\n”);
for(i=0;i<n;i++)
#include<stdio.h>
void main()
{
int a[20 ],n,i;
printf(“Enter the number of elements\n”);
scanf(“%d”,&n);
printf(“The first %d natural numbers are:\n”,n);
for(i=0;i<n;i++)
{
a[i]=i+1;
printf("a[%d]=%d\n",i,a[i]);
}
}
include<stdio.h>
void main()
{
int a[20 ],b[20],c[20],n, i;
printf(“Enter the number of elements\n”);
scanf(“%d”,&n);
printf(“Enter the elements of Array A\n”);
for(i=0;i<n;i++)
scanf(“%d”,&a[i]);
printf(“Enter the elements of Array B\n”);
for(i=0;i<n;i++)
scanf(“%d”,&b[i]);
printf(“Array Addition\n”);
for(i=0;i<n;i++)
c[i]=a[i]+b[i];
printf(“The resultant array is \n”);
for(i=0;i<n;i++)
printf(“%d\n”,c[i]);
}
include<stdio.h>
void main()
{
int a[10 ],n,i,max,min
printf(“Enter the number of elements\n”);
scanf(“%d”,&n);
printf(“Enter the values \n”);
for(i=0;i<n;i++)
scanf(“%d”,&a[i]);
max=min=a[0];
for(i=0;i<n;i++)
{
if(a[i]>max)
max=a[i];
if(a[i]<min)
min=a[i];
}
printf(“Largest number=%d\n”,max);
printf(“Smallest number=%d\n”,min);
}
5. Write a C program to read n integer elements in an array and print the same in
reverse order.
#include<stdio.h>
void main()
{
int a[20 ],n,i;
printf(“Enter the array size”);
scanf(“%d”,&n);
Printf(“Enter the array elements\n”);
for(i=0;i<n;i++)
scanf(“%d”,&a[i]);
printf(“The elements entered in the array are\n”);
for(i=0;i<n;i++)
printf(“%d\t”,a[i]);
printf(“The elements of the array in reverse order are\n”);
for(i=n-1;i>=0;i--)
printf(“%d\t”,a[i]);
}
#include<stdio.h>
#include<conio.h>
void main()
{
int fib[20],n,i;
printf(“Enter the no. of Fibonacci series to be generated\n”);
scanf("%d",&n);
fib[0]=0;
fib[1]=1;
if(n==1)
printf(“fibonacci series is %d”,fib[0]);
else if(n==2)
printf(“fibonacci series is %d \t %d”,fib[0],fib[1]);
else
for(i=2;i<n;i++)
Sorting Techniques:-
The Process of arranging the elements in ascending or descending order is called sorting
Bubble sort: The sorting algorithm is a comparison based algorithm in which each pair of
adjacent elements is compared and the elements are swapped if they are not in order. This
algorithm is not suitable for large datasets as its average and worst case time complexity are
of O(n2). where n is the number of items.
#include<stdio.h>
void main()
{
int a[50],n,i,j,temp;
printf("Enter the number of elements\n");
scanf("%d",&n);
printf("Enter %d elements\n",n);
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("The entered elements are\n");
for(i=0;i<n;i++)
{
printf("%d\t",a[i]);
}
printf("\n***** SORTING ******\n");
for(i=1;i<n;i++)
{
for(j=0;j<n-i;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
/*C program to sort numbers in ascending order using selection sort technique*/
#include<stdio.h>
void main()
{
int a[20],n,i,j,temp;
printf("Enter total elements\n");
scanf("%d",&n);
printf("Enter %d elements\n",n);
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
printf("The sorted elements are\n");
for(i=0;i<n;i++)
Searching Techniques:
The process of finding a particular element in the large amount of data is called searching.
Linear search:- A Linear search is also called as sequential Search. In this technique we search
for a given specific element called as key element in the large list of data in sequential order.
If the key element is present in the list of data then the search is successful otherwise search is
unsuccessful.
Benefits:
• Simple approach
• Works well for small arrays
• Used to search when the elements are not sorted
Disadvantages:
#include<stdio.h>
void main()
{
int a[100],n,i,key,flag=0;
printf("Enter the no of elements\n");
scanf("%d",&n);
printf("Enter %d elements ",n);
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("Enter the element to be searched\n" );
scanf("%d",&key);
for(i=0;i<n;i++)
{
Binary Search: It is fast search algorithm which works on the principle of divide and conquer.
for this algorithm to work properly the data collection should be in the sorted form
– middle element
2. If the middle element is the correct value, done. Otherwise, go to step 1. using only the
half of the array that may contain the correct value.
3. Continue steps 1. and 2. until either the value is found or there are no more elements to
examine
Advantages:
Disadvantages:
#include<stdio.h>
void main()
{
int a[100],n,i,low,high,mid,key,flag=0;
printf("Enter the size of the array\n");
Syntax:
data_type array_name[size1][size2];
where,
data_type: is the type of data to be stored and processed in the computer’s memory
Example:
int a[2][3];
Represents a is a two dimensional integer array that holds two rows and three columns.
1)Initializing all elements row wise:- A multidimensional array can be initialized by specifying
bracketed values for each row.
Example:
int[2][3]={{5,3,4} {6,1,2}} ;
The feasible way of accessing elements in a two dimensional array is by using nested loops.
Printing 2D array
where m-rowsize, n-columnsize, i-row index and j-column index
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("%d", a[i][j]);
}
printf(“\n”)
}
1. Write a c program to read and print the matrix of m rows and n columns
#include<stdio.h>
void main()
{
int a[20 ][20]m,n,i,j;
printf(“enter the row and column size\n”);
scanf(“%d%d”,&m,&n);
printf(“Enter the elements of matrix\n”);
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf("%d", &a[i][j]);
}
}
printf(“The elements of matrix are\n”);
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
Output
Enter the row and column size
23
Enter the elements of matrix
3 4 5 9 10 12
The elements of matrix are
3 4 5
9 10 12
include<stdio.h>
void main()
{
int a[20 ][20],b[20][20],c[20][20],m,n, i,j;
printf(“enter the rows and column of matrix\n”);
scanf(“%d%d”,&m,&n);
printf(“Enter the elements of Matrix A\n”);
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf(“%d”,&a[i][j]);
}
}
printf(“Enter the elements of Matrix B\n”);
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf(“%d”,&b[i][j]);
}
}
printf(“Matrix Addition\n”);
include<stdio.h>
void main()
{
int a[20 ][20],b[20][20], m,n, i,j;
printf(“enter the rows and column of matrix\n”);
scanf(“%d%d”,&m,&n);
printf(“Enter the elements of Matrix\n”);
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf(“%d”,&a[i][j]);
}
}
printf(“Enter the elements of Matrix are\n”);
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf(“%d\t”,a[i][j]);
}
printf(“\n”);
}
printf(“Matrix Transpose\n”);
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
include<stdio.h>
void main()
{
int a[20 ][20], m,n, i,j;
printf(“enter the no rows and column of matrix\n”);
scanf(“%d%d”,&m,&n);
printf(“Enter the elements of Matrix\n”);
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf(“%d”,&a[i][j]);
}
}
printf(“Matrix A is \n”);
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf(“%d\t”,a[i][j]);
}
printf(“\n”);
}
printf(“The diagonal Elements are\n”);
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
if(i==j)
{
5. Write a C Program to find sum of the the rows and columns of given matrix
#include<stdio.h>
void main()
{
int a[20 ][20], m,n, i,j,sum;
printf(“enter the no rows and column of matrix\n”);
scanf(“%d%d”,&m,&n);
printf(“Enter the elements of Matrix\n”);
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf(“%d”,&a[i][j]);
}
}
printf(“Martix A is Displayed as \n”);
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf(“%d\t”,a[i][j]);
}
printf(“\n”);
}
for(i=0;i<m;i++)
{
sum=0;
for(j=0;j<n;j++)
{
sum=sum+a[i][j];
}
printf(“sum of the elements of row %d in matrix=%d”,i,sum);
}
for(i=0;i<m;i++)
{
sum=0;
for(j=0;j<n;j++)
{
sum=sum+a[j][i];
#include<stdio.h>
void main()
{
int a[20 ][20], m,n, i,j,sum=0;
printf(“enter the order of matrix”);
scanf(“%d%d”,&m,&n);
printf(“Enter the elements of Matrix\n”);
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf(“%d”,&a[i][j]);
}
}
printf(“Martix A is Displayed as \n”);
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf(“%d\t”,a[i][j]);
}
printf(“\n”);
}
for(i=0;i<m;i++)
{
sum=sum+a[i][i];
}
printf(“The sum of the diagonal elements of matrix=%d\n”,sum);
}
#include<stdio.h>
void main()
{
int a[20 ][20], m,n, i,j,large;
printf(“enter the order of matrix\n”);
scanf(“%d%d”,&m,&n);
large=0;
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
if(a[i][j]>large)
large=a[i][j];
}
}
printf(“The largest element in the matrix = %d\n”,large);
}
Multidimensional array
An array having 3 or more subscript or dimensions is known as multidimensional array.
It is also known as arrays of arrays or matrix.
The general form of multidimensional array is
data_type array_name[s1][s2][s3]….[sn];
Where s1 is the size of ith dimension.
Strings:-
Definition:- A string constant or a string literal can be defined as a sequence of characters
enclosed in double quotes that will be treated as a single data element followed by a null
character ‘\0’
Syntax:
char string_name[size];
Where,
char is the data type of strings
string_name is a valid identifier which is the name for the string variable
size indicates the length of the string which is to be stored
Initialization of strings:
char str1[10]= “peter”;
or
char str1[10]={‘p’,‘e’,‘t’,‘e’,‘r’};
Both the initializations mentioned are same
if we directly specify the entire string at a time we use “ ”
String Length :- The function strlen() is used to find the length of the string in terms of number
of characters in it.
SYNTAX:
strlen(string_data);
/* C program to find length of the string using strlen() function */
#include<stdio.h>
#include<string.h>
void main()
{
char str1[50];
int len;
printf("Enter a string\n");
scanf("%s",str1);
len=strlen(str1);
printf("Length of the String=%d\n",len);
}
String Compare:- The function strcmp() is used to compare the string data every character
of one string is compared with the corresponding position character of second string.
SYNTAX
strcmp(str1,str2)
• The function returns 0 if there is complete match (str1==str2)
• Returns positive value if str1>str2
• Returns negative value if str1<str2
/* C program to compare two strings using strcmp( ) function */
#include<stdio.h>
#include<string.h>
void main()
{
char str1[20],str2[20];
int k;
printf("Enter string 1\n");
scanf("%s",str1);
printf("Enter string 2\n");
scanf("%s",str2);
k=strcmp(str1,str2);
SYNTAX
strcpy(str2,str1);
#include<stdio.h>
#include<string.h>
void main()
{
char str1[30],str2[30];
printf("Enter string1\n");
scanf("%s",str1);
strcpy(str2,str1);
printf("The copied string is = %s\n",str2);
}
#include<stdio.h>
#include<string.h>
void main()
{
char str1[30],str2[30];
int i=0;
printf("Enter string1\n");
scanf("%s",str1);
while(str1[i]!='\0')
{
str2[i]=str1[i];
i++;
}
str2[i]='\0';
printf("The Original String=%s\n",str1);
printf("The Copied String=%s\n",str2);
}
String n Copy :-The strncpy() funtion copies the n characters of one string to another string
SYNTAX
strncpy(dest_string,source_string,n);
#include<stdio.h>
#include<string.h>
void main()
{
char str1[30],str2[30];
printf("Enter String1\n");
gets(str1);
printf("String 1= %s\n",str1);
strncpy(str2,str1,10);
printf("String 2= %s\n",str2);
}
Enter String1
MANGALORE_INSTITUTE_OF_TECHNOLOGY
String 1= MANGALORE_INSTITUTE_OF_TECHNOLOGY
String 2= MANGALORE_
SYNTAX
strcat(str1,str2);
#include<stdio.h>
#include<string.h>
void main()
{
char str1[30],str2[30];
printf("Enter String1\n");
scanf("%s",str1);
printf("Enter String2\n");
scanf("%s",str2);
strcat(str1,str2);
printf("The concatenated string is=%s\n",str1);
}
#include<stdio.h>
#include<string.h>
void main()
{
char str1[20],str2[20],str3[50];
int i,k;
printf("Enter String1\n");
scanf("%s",str1);
printf("Enter String2\n");
scanf("%s",str2);
k=0;
for(i=0;str1[i]!='\0';i++)
String n Concatenate: - The function strncat() is used to concatenate the specified number
of characters only
SYNTAX
strncat(string1,string2,n);
Where n is an integer value which concatenates only n characters of string2 to string1
#include<stdio.h>
#include<string.h>
void main()
{
char str1[30],str2[30];
printf("Enter String1 and String2\n");
scanf("%s%s",str1,str2);
strncat(str1,str2,5);
printf("The concatenated string is=%s\n",str1);
}
The characters from left to right in the original string are placed in the reverse order
SYNTAX
strrev(str1)
#include<stdio.h>
#include<string.h>
void main()
{
char str1[30];
printf("Enter String1\n");
gets(str1);
strrev(str1);
printf("The Reversed string =%s\n",str1);
}
#include<stdio.h>
#include<string.h>
void main()
{
char str1[50],str2[50];
int i,j,len,x;
printf("Enter String1\n");
gets(str1);
j=0;
len=strlen(str1);
for(i=len-1;i>=0;i--)
{
str2[j]=str1[i];
j++;
}
str2[j]='\0';
printf("The original String =%s\n",str1);
printf("The reversed String= %s\n",str2);
x=strcmp(str1,str2);
if(x==0)
printf("%s is a palindrome\n",str1);
else
printf("%s is not a palindrome\n",str1);
}
String Lower :- The function strlwr() converts each character of the string to lowercase.
SYNTAX
strlwr(string_data);
String Upper :- The function strupr() converts each character of the string to uppercase.
SYNTAX
strupr(string_data);
/* C program to convert strings to uppercase and lowercase using strupr( ) and strlwr ( )
functions*/
#include<stdio.h>
#include<string.h>
void main()
{
char str1[50],str2[50];
printf("Enter the string in lower case letters\n");
scanf("%s",str1);
printf("The string in Upper case letter is= %s\n", strupr(str1));
printf("Enter the string in Upper case letters\n");
scanf("%s",str2);
printf("The string in Lower case letter is= %s\n", strlwr(str2));
}
#include<stdio.h>
#include<string.h>
#include<ctype.h>
main()
{
char str1[50],ch;
int i,vc=0,cc=0,ac=0,ec=0,ic=0,oc=0,uc=0;
printf("\n Enter a sentence\n");
gets(str1);
printf("The entered sentence is\n");
puts(str1);
for (i=0;i<strlen(str1);i++)
{
if(isalpha(str1[i]))
{
ch=str1[i];
if(ch=='a'||ch=='A')
ac++;
else if(ch=='e'||ch=='E')
ec++;
else if(ch=='i'||ch=='I')
ic++;
else if(ch=='o'||ch=='O')
oc++;
else if(ch=='u'||ch=='U')
uc++;
else
cc++;
}
}
vc=ac+ec+ic+oc+uc;
printf("\n The total number of vowels =%d\n",vc);
printf("\n The frequency of vowel a is =%d\n",ac);
printf("\n The frequency of vowel e is =%d\n",ec);
/* C program to count the number of Vowels and Consonants in a given String using
while loop */
#include<stdio.h>
void main()
{
chat str[100];
int i=0,vc=0,cc=0;
printf(“Enter any string\n”);
gets(str);
while(str[i]!= ‘\0’)
{
if(str[i]== ‘a’|| str[i]== ‘e’|| str[i]== ‘i’|| str[i]== ‘o’|| str[i]== ‘u’|| str[i]==
‘A’|| str[i]== ‘E’|| str[i]== ‘I’|| str[i]== ‘O’|| str[i]== ‘U’)
{
vc++;
}
else
{
cc++;
}
i++;
}
printf(“Number of Vowels in the string = %d\n”,vc);
printf(“Number of consonants in the string=%d\n”,cc);
}