0% found this document useful (0 votes)
22 views

Array in C

Uploaded by

Srivatsan SP
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views

Array in C

Uploaded by

Srivatsan SP
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 41

IT22101 PROGRAMMING FOR PROBLEM SOLVING SVCE

UNIT - III ARRAYS AND STRINGS 9


Array: declaration, initialization. Multi-dimensional arrays. Strings: Strings vs Character
arrays, string operations

Suggested Activities - Grade sheet generation in SMS

INTRODUCTION TO ARRAYS:
ARRAY DEFINITION
An array is a data structure that is used for the storage of homogenous data i.e., data
of the same type. An array is a collection of elements of same data type.
The data type of an element is called element type. The individual elements of an
array are distinguished and are referred to or accessed according to their positions in an
array.
The position of an element in an array is specified with an integer value known as
index or subscript. The array index in C starts with 0 i.e. index of the first element of an
array is 0. The memory space required by an array can be computed as size of element type
& number of elements in an array. Arrays are always stored in contiguous memory
location.
Classification
1. Single dimensional array/one dimensional array
2. Two dimensional array
3. Multi-dimensional array

SINGLE DIMENSIONAL ARRAY


It consists of a fixed number of elements of the same data type organized as a simple
linear sequence. The element of a single dimensional array can be accessed by using a
single subscript.

1. Declaration of Array
The general form of a single dimensional array declaration is
type variable_name [size];

PREPARED BY
R.GNANAVEL AP/CSE
IT22101 PROGRAMMING FOR PROBLEM SOLVING SVCE

A single dimensional array declaration consists of a type specifier i.e. element type,
an identifier i.e. name of array and a size specifier i.e. number of elements in the array
enclosed with in square brackets.
e.g. int number[5];
Consider array elements to be assigned as follows
number[0] = 12 number[1] = 22 number[2] = 33
number[3] = 44 number[4] = 56
This would cause the array number to be store the value as shown below

[0] [1] [2] [3] [4]


number 12 22 33 44 56

2. Initialization of Single dimensional Array


Like variable array can be initialized. The syntactic rules about the initialization of
array elements are follow
1. The element of an array can be initialized using an initialization list. An
initialization list is a comma separated list of initializers enclosed within braces.
2. An initializer is an expression that determines the initial value of an element of
the array.
3. If the type of initializer is not the same as the element type of array, implicit type
casting will be done, if the type are compatible. Otherwise there will be a
compilation error.
4. The number of initializer in the initialization list should be less than or at most
equal to the value of size of specifier.
General Form
type array-name [size]={ list of values };
Example
int a[5] = { 1, 3, 5, 7, 9 };
will declare the variable a as an array of size 5 and will initialize the array with 1,
3, 5, 7, 9assigned
int a[5] = {1, 3, 5};
PREPARED BY
R.GNANAVEL AP/CSE
IT22101 PROGRAMMING FOR PROBLEM SOLVING SVCE

will initialize the first three elements to 1, 3, 5 and the remaining two elements to zero.
The size may be omitted. In such cases, the compiler allocated enough space for all
initialized elements.
int a[] = { 1, 3, 5, 6 };
will declare the a array to contain four elements with initial values 1, 3, 5 and 6.
3. Reading Elements of Single – Dimensional Array
An iteration statement is used for reading the elements of a single – dimensional
array
General Form
for ( i=0; i< size of array ; i++)
scanf(“control string”, &array_name[i]);
where i is the index variable. We can use any integer variable as index variable.
Example
int a[5];
The elements of the array can be read using the following statement
for ( i=0; i< 5 ; i++)
scanf(“%d”, &a[i]);

4. Accessing Single dimensional array


The element of a single dimensional array can be accessed by using a subscript
operator and a subscript.
The General form
array-name[index]
Example
int a[5] = { 1, 3, 5, 7, 9 };
printf(“%d”,a[0]);
will print the element of the array a at position 0 i.e. 1.
PREPARED BY
R.GNANAVEL AP/CSE
IT22101 PROGRAMMING FOR PROBLEM SOLVING SVCE

printf(“%d”,a[2]);
will print the element of the array a at position 2 i.e. 5.

TWO DIMENSIONAL ARRAY


C allows us to define tables of items by using two –dimensional array.
1. Declaration of Array
The general form of a Two single dimensional array declaration is
type variable_name [row_size][Column_size];
Each dimension of the array is indexed from zero to its maximum size minus one;
the first index selects the row and the second index selects the column within that row.
Representation
e.g. int number[4][3];

Column Column Column


0 1 2
[0][0] [0][1] [0][2]
Row[0]
[1][0] [1][1] [1][2]
Row[1]
[2][0] [2][1] [2][2]
Row[2]
[3][0] [3][1] [3][2]
Row[3]

Consider array elements to be assigned as follows


number[0] = 12 number[1] = 22 number[2] = 33

PREPARED BY
R.GNANAVEL AP/CSE
IT22101 PROGRAMMING FOR PROBLEM SOLVING SVCE

number[3] = 44 number[4] = 56
This would cause the array number to be store the value as shown below

[0] [1] [2] [3] [4]


number 12 22 33 44 56

2. Initialization of Single dimensional Array


Like one dimensional array, two dimensional array may be initialized.
Example
int table[2][3] = { 1, 3, 5, 7, 8, 2 };
and the values to be stores as

Column Column Column


0 1 2
[0][0] [0][1] [0][2]
Row[0] 1 3 5
[1][0] [1][1] [1][2]
Row[1] 7 8 2

We can also initialize the above two dimensional array in the form of a matrix as
shown below
int table[2][3] = {
{ 1, 3, 5},
{ 7, 8, 2 }
};

PREPARED BY
R.GNANAVEL AP/CSE
IT22101 PROGRAMMING FOR PROBLEM SOLVING SVCE

When the array is completely initialized with all values we need not specify the size
of the first dimension. That is
int table[ ][3] = {
{ 1, 3, 5},
{ 7, 8, 2 }
};
is permitted.

3. Reading Elements of Single – Dimensional Array


An iteration statements is used for reading the elements of a two – dimensional array
General Form
for ( i=0; i< row_size; i++)
for(j=0; j< column_size; j++)
scanf(“control string”, &array_name[i][j]);
where I, j are the index variables. We can use any integer variable as index variable.
Example
int table[2][3];
The elements of the array can be read using the following statement
for ( i=0; i< 2 ; i++)
for( j=0; j<3; j++ )
scanf(“%d”, &table[i][j]);

4. Accessing Single dimensional array


The element of a single dimensional array can be accessed by using a subscript
operator and a subscript.

PREPARED BY
R.GNANAVEL AP/CSE
IT22101 PROGRAMMING FOR PROBLEM SOLVING SVCE

The General form


array-name[row_index][column_index]
Example
int table[2][3] = { 1, 3, 5, 7, 8, 2 };
printf(“%d”,table[0][0]);
will print the element of the array table at row 0 and column 0 i.e. 1.
printf(“%d”, table[1][2]);
will print the element of the array table at row 1 and column 2 that is 2.

/*Program to Read and Print an Array */


#include<stdio.h>
void main()
{
int a[10],i,n;
printf("\nEnter the number of Element");
scanf("%d",&n);
printf("\nEnter the Elements\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("\nThe Given Array is \n");
for(i=0;i<n;i++)
printf("\t%d",a[i]);
}

Sample Input / Output


Enter the number of Element
6
Enter the Elements
10 20 30 40 50 60
The Given Array is
10 20 30 40 50 60

PREPARED BY
R.GNANAVEL AP/CSE
IT22101 PROGRAMMING FOR PROBLEM SOLVING SVCE

/*Program to find Sum and Average of the Elements in an Array */


#include<stdio.h>
void main()
{
int a[10],i,j,n,sum=0;
float average;
printf("\nEnter the number Element in the array");
scanf("%d",&n);
printf("\nEnter the Elements in the Array \n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n;i++)
sum=sum+a[i];
average=sum/n;
printf("\nThe Sum = %d",sum);
printf("\nAverage = %f",average);
}
Sample Input / Output
Enter the number Element in the array
5
Enter the Elements in the Array
10 20 30 40 50
The Sum = 150
Average = 30.000000

/*Program to Find Largest Element in an Array */


#include<stdio.h>
void main()
{
int a[10],i,j,n,big;
printf("\nEnter the number Element in the array\n");
scanf("%d",&n);
printf("\nEnter the Elements in the Array \n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
big=a[0];
for(i=1;i<n;i++)
PREPARED BY
R.GNANAVEL AP/CSE
IT22101 PROGRAMMING FOR PROBLEM SOLVING SVCE

{
if(a[i]>big)
big=a[i];
}
printf("\nThe Biggest Element = %d",big);
}
Sample Input / Output
Enter the number Element in the array
6
Enter the Elements in the Array
125 56 254 35 485 55
The Biggest Element = 485

/*Program to Find Smallest Element in an Array */


#include<stdio.h>
void main()
{
int a[10],i,j,n;
int small;
printf("\nEnter the number Element in the array\n");
scanf("%d",&n);
printf("\nEnter the Elements in the Array \n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
small=a[0];
for(i=1;i<n;i++)
{ if(a[i]<small)
small=a[i];
}
printf("\nThe Smallest Element = %d",small);
}

Sample Input / Output


Enter the number Element in the array
6
Enter the Elements in the Array
PREPARED BY
R.GNANAVEL AP/CSE
IT22101 PROGRAMMING FOR PROBLEM SOLVING SVCE

25 653 4 58 96 5
The Smallest Element = 4

/*Program to Find Total and Average of 6 Marks */


#include<stdio.h>
void main()
{
int m[6],i,total;
float average;
printf("\nEnter the Marks");
for(i=0;i<6;i++)
scanf("%d",&m[i]);
total=0;
for(i=0;i<6;i++)
total=total+m[i];
average=total/6;
printf("\nTotal = %d",total);
printf("\nAverage = %f",average);
}
Sample Input / Output
Enter the Marks
98 86 65 74 95 61
Total = 479
Average = 79.000000

/*Program to Separate the given list into Positive and Negative Numbers*/
#include<stdio.h>
void main()
{
int a[20],i,n,pos[20],neg[20],pc,nc;
pc=0;
nc=0;
printf("\nEnter the number of Elements");
scanf("%d",&n);
printf("\nEnter the Elements");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n;i++)
{
if(a[i]>=0)
{

PREPARED BY
R.GNANAVEL AP/CSE
IT22101 PROGRAMMING FOR PROBLEM SOLVING SVCE

pos[pc]=a[i];
pc=pc+1;
}
else
{
neg[nc]=a[i];
nc=nc+1;
}
}
printf("\nPositive Numbers in the array\n");
for(i=0;i<pc;i++)
printf("\t%d",pos[i]);
printf("\nNegative Numbers in the array\n");
for(i=0;i<nc;i++)
printf("\t%d",neg[i]);
}

Sample Input / Output


Enter the number of Elements
10
Enter the Elements
12 -8 0 -32 -9 -43 54 562 466 232
Positive Numbers in the array
12 0 54 562 466 232
Negative Numbers in the array
-8 -32 -9 -43

/*Program to Separate the given list into Odd and Even Numbers*/
#include<stdio.h>
void main()
{
int a[20],i,n,odd[20],even[20],oc,ec;
oc=0;
ec=0;
printf("\nEnter the number of Elements");
scanf("%d",&n);
printf("\nEnter the Elements");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n;i++)
PREPARED BY
R.GNANAVEL AP/CSE
IT22101 PROGRAMMING FOR PROBLEM SOLVING SVCE

{
if(a[i]%2==0)
{
even[ec]=a[i];
ec=ec+1;
}
else
{
odd[oc]=a[i];
oc=oc+1;
}
}
printf("\nEven Numbers in the array");
for(i=0;i<ec;i++)
printf("\t%d",even[i]);
printf("\nOdd Numbers in the array");
for(i=0;i<oc;i++)
printf("\t%d",odd[i]);
}

Sample Input / Output


Enter the number of Elements
8
Enter the Elements
12 23 45 67 2345 7 874 56

Even Numbers in the array 12 874 56


Odd Numbers in the array 23 45 67 2345 7

/*Program to Search for a Number in the given List - Linear Search*/


#include<stdio.h>
void main()
{
int a[20],i,n,x,f=0;
printf("\nEnter the number of Elements");
scanf("%d",&n);
printf("\nEnter the Elements");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("\nEnter the Element to be Searched");
scanf("%d",&x);
PREPARED BY
R.GNANAVEL AP/CSE
IT22101 PROGRAMMING FOR PROBLEM SOLVING SVCE

for(i=0;i<n;i++)
{
if(a[i]==x)
{
f=1;
printf("\nThe Element Found at %d",i);
break;
}
}
if(f==0)
printf("\nThe Element Not Found in the List");
}

Sample Input / Output


Enter the number of Elements
6
Enter the Elements
123 67 98 45 -76 7
Enter the Element to be Searched -76
The Element Found at 4

Example Program: Computing Mean, Median and Mode


#include<stdio.h>
void main()
{
int i,j,a[20]={0},sum=0,n,t,b[20]={0},k=0,c=1,max=0,mode;
float x=0.0,y=0.0;
printf("\nEnter the limit\n");
scanf("%d",&n);
printf("Enter the set of numbers\n");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
sum=sum+a[i];
}
x=(float)sum/(float)n;
printf("Mean\t= %f",x);

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

PREPARED BY
R.GNANAVEL AP/CSE
IT22101 PROGRAMMING FOR PROBLEM SOLVING SVCE

{
for(j=i+1;j<n;j++)
{
if(a[i]>a[j])
{
t=a[i];
a[i]=a[j];
a[j]=t;
}
}
}
if(n%2==0)
y=(float)(a[n/2]+a[(n-1)/2])/2;
else
y=a[(n-1)/2];
printf("\nMedian\t= %f",y);

for(i=0;i<n-1;i++)
{
mode=0;
for(j=i+1;j<n;j++)
{
if(a[i]==a[j])
{
mode++;
}
}
if((mode>max)&&(mode!=0))
{
k=0;
max=mode;
b[k]=a[i];
k++;
}
else if(mode==max)
{
b[k]=a[i];
k++;
}
}
for(i=0;i<n;i++)
{
PREPARED BY
R.GNANAVEL AP/CSE
IT22101 PROGRAMMING FOR PROBLEM SOLVING SVCE

if(a[i]==b[i])
c++;
}
if(c==n)
printf("\nThere is no mode");
else
{
printf("\nMode\t= ");
for(i=0;i<k;i++)
printf("%d ",b[i]);
}
}
Example Program: Matrix Operations (Addition, Scaling, Determinant and
Transpose)
/*Program to Read and Print Two - Dimensional Array*/
#include<stdio.h>
void main()
{
int A[5][5],i,j,r,c;
printf("\nEnter the number of Rows and Columns");
scanf("%d%d",&r,&c);
printf("\nEnter the Matrix");
for(i=0;i<r;i++)
for(j=0;j<c;j++)
scanf("%d",&A[i][j]);
printf("\nThe Given Matrix is ");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
printf("\t%d",A[i][j]);
printf("\n");
}
}

Sample Input / Output


Enter the number of Rows and Columns
2 4
Enter the Matrix
PREPARED BY
R.GNANAVEL AP/CSE
IT22101 PROGRAMMING FOR PROBLEM SOLVING SVCE

1 2 3 5
4 6 2 8
The Given Matrix is
1 2 3 5
4 6 2 8

/*Program to Find Addition of Two Matrices*/


#include<stdio.h>
void main()
{
int A[5][5],B[5][5],C[5][5],i,j,r,c;
printf("\nEnter the number of Rows and Columns");
scanf("%d%d",&r,&c);
printf("\nEnter the Matrix A");
for(i=0;i<r;i++)
for(j=0;j<c;j++)
scanf("%d",&A[i][j]);
printf("\nEnter the Matrix B");
for(i=0;i<r;i++)
for(j=0;j<c;j++)
scanf("%d",&B[i][j]);
for(i=0;i<r;i++)
for(j=0;j<c;j++)
C[i][j]=A[i][j]+B[i][j];
printf("\nThe Resultant Matrix is\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
printf("\t%d",C[i][j]);
printf("\n");
}
}
Sample Input / Output
Enter the number of Rows and Columns
2 4

PREPARED BY
R.GNANAVEL AP/CSE
IT22101 PROGRAMMING FOR PROBLEM SOLVING SVCE

Enter the Matrix A


1 2 3 4
5 6 7 8
Enter the Matrix B
6 7 8 3
4 6 2 3
The Resultant Matrix is
7 9 11 7
9 12 9 11

/*Program to Find Subtraction of Two Matrices*/


#include<stdio.h>
void main()
{
int A[5][5],B[5][5],C[5][5],i,j,r,c;
printf("\nEnter the number of Rows and Columns");
scanf("%d%d",&r,&c);
printf("\nEnter the Matrix A");
for(i=0;i<r;i++)
for(j=0;j<c;j++)
scanf("%d",&A[i][j]);
printf("\nEnter the Matrix B");
for(i=0;i<r;i++)
for(j=0;j<c;j++)
scanf("%d",&B[i][j]);
for(i=0;i<r;i++)
for(j=0;j<c;j++)
C[i][j]=A[i][j]-B[i][j];
printf("\nThe Resultant Matrix is\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
printf("\t%d",C[i][j]);
printf("\n");
}
}
PREPARED BY
R.GNANAVEL AP/CSE
IT22101 PROGRAMMING FOR PROBLEM SOLVING SVCE

Sample Input / Output


Enter the number of Rows and Columns
2 4
Enter the Matrix A
8 7 9 7
6 8 5 4
Enter the Matrix B
1 2 3 4
4 5 3 1
The Resultant Matrix is
7 5 6 3
2 3 2 3

/*Program to Find Transpose of a Matrix*/


#include<stdio.h>
void main()
{
int A[5][5],B[5][5],i,j;
int r1,c1,r2,c2;
printf("\nEnter the number of Rows and Columns ");
scanf("%d%d",&r1,&c1);
printf("\nEnter the Matrix");
for(i=0;i<r1;i++)
for(j=0;j<c1;j++)
scanf("%d",&A[i][j]);
r2=c1;
c2=r1;
for(i=0;i<r1;i++)
for(j=0;j<c1;j++)
B[j][i]=A[i][j];
printf("\nThe Transpose Matrix is\n");
for(i=0;i<r2;i++)
{

PREPARED BY
R.GNANAVEL AP/CSE
IT22101 PROGRAMMING FOR PROBLEM SOLVING SVCE

for(j=0;j<c2;j++)
printf("\t%d",B[i][j]);
printf("\n");
}
}

Sample Input / Output


Enter the number of Rows and Columns
2 4
Enter the Matrix
1 2 3 4
5 6 7 8

The Transpose Matrix is


1 5
2 6
3 7
4 8

/*Program to Find Multiplication of two Matrices*/


#include<stdio.h>
void main()
{
int A[10][10],B[10][10],C[10][10];
int i,j,k,r1,c1,r2,c2;
printf("\nEnter the Number of Rows and Columns of Matrix 1\n");
scanf("%d%d",&r1,&c1);
printf("\nEnter the Number of Rows and Columns of Matrix 2\n");
PREPARED BY
R.GNANAVEL AP/CSE
IT22101 PROGRAMMING FOR PROBLEM SOLVING SVCE

scanf("%d%d",&r2,&c2);
if(c2==r2)
printf("\nMatrix Muliplication is not Possible");
else
{
printf("\nEnter the Matrix A");
for(i=0;i<r1;i++)
for(j=0;j<c1;j++)
scanf("%d",&A[i][j]);
printf("\nEnter the Matrix B");
for(i=0;i<r2;i++)
for(j=0;j<c2;j++)
scanf("%d",&B[i][j]);
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
C[i][j]=0;
for(k=0;k<r2;k++)
{
C[i][j]=C[i][j]+A[i][k]*B[k][j];
}
}
}

}
Printf(“\nThe Resultant Matrix is\n”);
for(i=0;i<r1;i++)
{
for(j=0;j<c2;j++)
printf("\t%d",C[i][j]);
printf("\n");
}
}
Sample Input / Output
Enter the Number of Rows and Columns of Matrix 1
24
Enter the Number of Rows and Columns of Matrix 2
42

PREPARED BY
R.GNANAVEL AP/CSE
IT22101 PROGRAMMING FOR PROBLEM SOLVING SVCE

Enter the Matrix A


1234
5678
Enter the Matrix B
12
34
56
78
The Resultant Matrix is
50 60
114 140

/*Find determinant of nxn matrix.*/

#include<stdio.h>
int a[20][20],m;
int determinant(int f[20][20],int a);
int main()
{
int i,j;
printf("\n\nEnter order of matrix : ");
scanf("%d",&m);
printf("\nEnter the elements of matrix\n");
for(i=1;i<=m;i++)
{
for(j=1;j<=m;j++)
{
printf("a[%d][%d] = ",i,j);
scanf("%d",&a[i][j]);
}
}
printf("\n\n---------- Matrix A is --------------\n");
for(i=1;i<=m;i++)
{
printf("\n");

PREPARED BY
R.GNANAVEL AP/CSE
IT22101 PROGRAMMING FOR PROBLEM SOLVING SVCE

for(j=1;j<=m;j++)
{
printf("\t%d \t",a[i][j]);
}
}
printf("\n \n");
printf("\n Determinant of Matrix A is %d .",determinant(a,m));
}

int determinant(int f[20][20],int x)


{
int pr,c[20],d=0,b[20][20],j,p,q,t;
if(x==2)
{
d=0;
d=(f[1][1]*f[2][2])-(f[1][2]*f[2][1]);
return(d);
}
else
{
for(j=1;j<=x;j++)
{
int r=1,s=1;
for(p=1;p<=x;p++)
{
for(q=1;q<=x;q++)
{
if(p!=1&&q!=j)
{
b[r][s]=f[p][q];
s++;
if(s>x-1)
{
r++;
s=1;
}
}
}
}
for(t=1,pr=1;t<=(1+j);t++)
pr=(-1)*pr;
c[j]=pr*determinant(b,x-1);
PREPARED BY
R.GNANAVEL AP/CSE
IT22101 PROGRAMMING FOR PROBLEM SOLVING SVCE

}
for(j=1,d=0;j<=x;j++)
{
d=d+(f[1][j]*c[j]);
}
return(d);
}
}

PREPARED BY
R.GNANAVEL AP/CSE
IT22101 PROGRAMMING FOR PROBLEM SOLVING SVCE

STRINGS
STRING MANIPULATION
In ‘C’ language the group of character, digits, and symbols enclosed within
quotation marks are called as string otherwise strings are array of characters. Null character
(‘\0’) is used to mark the end of the string.

Example:
char name[]={‘B’,’A’,’B’,’U’,’\0’};
Each character is stored in one byte of memory and successive characters of the
string are stored in successive byte.
Memory map of string

String B A B U
Address 5001 5002 5003 5004

INITIALIZATION OF STRING
The string can be initialized as follows.
char name[]=”BABU”;
The characters of the string are enclosed within a pair of double quotes.
The initialization of NULL character is not essential because the C compiler inserts
the NULL (\0) character automatically at the end of the string.

READING AND WRITING STRING


The ‘%s’ control string can be used in scanf() statement to read a string from the
terminal and the same may be used to write string to the terminal in printf() statement.
Example:
char name[10];
scanf(“%s”,name);
printf(“%s”,name);

/*Program to Read and Print a String*/


#include<stdio.h>
#include<string.h>
#include<conio.h>
void main()
{
char s1[20];
clrscr();
printf("\nEnter the String\n");
scanf("%s",s1);
printf("\nThe Given String is %s",s1);
getch();
PREPARED BY
R.GNANAVEL AP/CSE
IT22101 PROGRAMMING FOR PROBLEM SOLVING SVCE

}
Sample Input / Output
Enter the String
Welcome
The Given String is Welcome

STRINGS STANDARD FUNCTIONS


C compiler provides the following string handling functions.
S.NO FUNCTION PURPOSE
1. Strlen() Used to find length of the string
2. Strcpy() Used to copy one string to another
3. Strcat() Used to combine two strings
4. Strcmp() Used to compare characters of two strings(difference between
small and capital letters)
5. Strrev() Used to reverse a string
6. Strlwr() Used to convert strings into lower case
7. Strupr() Used to convert strings into upper case
8. Strdup() Used to duplicate a string
9. Strncpy() used to copy first ‘n’ characters of one string into another
10. Strncmp() Used to compare first ‘n’ characters of two strings
11. Strcmpi() Used to compare two strings without regarding the case.
12. Strncmpi() Used to compare first ‘n’ characters of two strings without
regarding the case.
13. Stricmp() Compare two strings(Not difference between small and capital
letters)
14. Strchr() Determines first occurrence of a given character in a string
15. Strrchr() Determines last occurrence of a given character in a string
16. Strstr() Determines first occurrence of a given string in another string
17. Strncat() Appends source string to destination string upto specified length.
18. Strnset() Sets specified number of characters of string with a given
argument or symbol.
19. Strspn() Finds upto what length two strings are identical.
20. Strbrk() Searches the first occurrence of the character in a given string and
then it displays the string from that character.

The strlen() function


This function is used to count and return the number of characters present in a string.

Syntax:
var=strlen(string);

PREPARED BY
R.GNANAVEL AP/CSE
IT22101 PROGRAMMING FOR PROBLEM SOLVING SVCE

Example:
/*Program to Find Length of a String*/
#include<stdio.h>
#include<string.h>
#include<conio.h>
void main()
{
char s1[20];
int n;
clrscr();
printf("\nEnter the String\n");
scanf("%s",s1);
n=strlen(s1);
printf("\nThe Length of the Given String is %d",n);
getch();
}

Sample Input / Output


Enter the String
Welcome
The Length of the Given String is 7

The strcpy() function


This function is used to copy the contents of one string to another and it almost
works like string assignment operator.

Syntax:
strcpy(string1,string2);
i.e., The contents of string2 is assigned to the contents of string1.

Example:
/*Program to Copy a String into another String*/
#include<stdio.h>
#include<string.h>
#include<conio.h>
void main()
{
char s1[20],s2[20];
clrscr();
printf("\nEnter the String1\n");
PREPARED BY
R.GNANAVEL AP/CSE
IT22101 PROGRAMMING FOR PROBLEM SOLVING SVCE

scanf("%s",s1);
strcpy(s2,s1);
printf("\nThe Copied String is %s",s2);
getch();
}

Sample Input / Output


Enter the String1
Welcome
The Copied String is Welcome

The strcat() function


The strcat() function is used to concatenate or combine, two strings together and
forms a new concatenated string.

Syntax:
strcat(string1,string2);
When the above strcat() function is executed, string2 is combined with string1 and it
removes the null character (\0) of string1 and places string2 from there.

Example:
/*Program to Concate two Strings*/
#include<stdio.h>
#include<string.h>
#include<conio.h>
void main()
{
char s1[20],s2[20];
clrscr();
printf("\nEnter the String1\n");
scanf("%s",s1);
printf("\nEnter the String 2\n");
scanf("%s",s2);
strcat(s1,s2);
printf("\nThe Concatenated String is %s",s1);
getch();
}

Sample Input / Output


Enter the String1
Good
Enter the String 2
PREPARED BY
R.GNANAVEL AP/CSE
IT22101 PROGRAMMING FOR PROBLEM SOLVING SVCE

Morning
The Concatenated String is GoodMorning

The strcmp() function


This is a function which compares two strings to find out whether they are same or
different. The two strings are compared character by character until the end of one of the
string is reached. If the two strings are identical strcmp() returns a value zero. If they are
not equal, it returns the numeric difference between the first non-matching characters.

Syntax:
Strcmp(string1,string2);

Example:
/*Program to Compare two Strings*/
#include<stdio.h>
#include<string.h>
#include<conio.h>
void main()
{
char s1[20],s2[20];
clrscr();
printf("\nEnter the String1\n");
scanf("%s",s1);
printf("\nEnter the String 2\n");
scanf("%s",s2);
if(strcmp(s1,s2)>0)
printf("\n%s Comes Before %s",s2,s1);
else if(strcmp(s1,s2)<0)
printf("\n%s Comes Before %s",s1,s2);
else
printf("\n%s Equals to %s",s1,s2);
}

Sample Input / Output


Enter the String1
Aswin
Enter the String 2
Ashwin
Ashwin Comes Before Aswin

The strrev() function


PREPARED BY
R.GNANAVEL AP/CSE
IT22101 PROGRAMMING FOR PROBLEM SOLVING SVCE

The strrev() function is used to reverse a string. This function takes only one
argument and return one argument.

Syntax:
strrev(string);
Example:
/*Program to Find Reverse of a String*/
#include<stdio.h>
#include<string.h>
#include<conio.h>
void main()
{
char s1[20];
clrscr();
printf("\nEnter the String 1");
scanf("%s",s1);
strrev(s1);
printf("\nReverse string is %s",s1);
getch();

Sample Input / Output


Enter the String 1Welcome
Reverse string is emocleW

/*Program to Convert the Lower Case String to Uppercase String*/


#include<stdio.h>
#include<string.h>
#include<conio.h>
void main()
{
char s1[20],s2[20];
clrscr();
printf("\nEnter the String1\n");
scanf("%s",s1);
strupr(s1);
printf("\nThe String in upper case is %s",s1);
getch();
}

PREPARED BY
R.GNANAVEL AP/CSE
IT22101 PROGRAMMING FOR PROBLEM SOLVING SVCE

Sample Input / Output


Enter the String1
welcome
The String in upper case WELCOME

/*Program to Convert the Upper Case String to Lowercase String*/


#include<stdio.h>
#include<string.h>
#include<conio.h>
void main()
{
char s1[20],s2[20];
clrscr();
printf("\nEnter the String1\n");
scanf("%s",s1);
strlwr(s1);
printf("\nThe Strind is %s",s1);
getch();
}

Sample Input / Output


Enter the String1
WELCOME
The String in lower case welcome

/*Program to Copy string without using Library Function*/


#include<stdio.h>
void main()
{
char s1[20],s2[20];
int i=0;
printf("\nEnter the String1\n");
scanf("%s",s1);
while(s1[i]!='\0')
{
s2[i]=s1[i];
i++;
}
s2[i]='\0';
printf("\nThe Copied String is %s",s2);
}
PREPARED BY
R.GNANAVEL AP/CSE
IT22101 PROGRAMMING FOR PROBLEM SOLVING SVCE

Sample Input / Output


Enter the String1
Welcome
The Copied String is Welcome

/*Program to Find the Length of a string without using Library Function*/


#include<stdio.h>
void main()
{
char s1[20];
int i=0,n;
printf("\nEnter the String1\n");
scanf("%s",s1);
while(s1[i]!='\0')
{
i++;
}
n=i;
printf("\nThe Length of the String %s is %d",s1,n);
}

Sample Input / Output


Enter the String1
welcome
The Length of the String welcome is 7

/*Program to Compare two strings without using Library Function*/


#include<stdio.h>
void main()
{
char s1[20],s2[20];
int i=0,f=0;
printf("\nEnter the String1\n");
scanf("%s",s1);
printf("\nEnter the String2\n");
scanf("%s",s2);
while(s1[i]!='\0')
{
if(s1[i]==s2[i])
PREPARED BY
R.GNANAVEL AP/CSE
IT22101 PROGRAMMING FOR PROBLEM SOLVING SVCE

i++;
else
{
f=1;
break;
}
}
if(f==1)
printf("\nThe two Strings are not Equal");
else
printf("\nThe two Strings are Equal");
}

Sample Input / Output


Enter the String1
Aswin
Enter the String2
Asvin
The two Strings are not Equal

/*Program to Concatenate two strings without using Library Function*/


#include<stdio.h>
void main()
{
char s1[20],s2[20];
int i=0,j=0;
printf("\nEnter the String1\n");
scanf("%s",s1);
printf("\nEnter the String2\n");
scanf("%s",s2);
while(s1[i]!='\0')
i++;
while(s2[j]!='\0')
{
s1[i]=s2[j];
i++;
j++;
}
s1[i]=’\0’;
printf("\nThe Concatenated String is %s",s1);
}
PREPARED BY
R.GNANAVEL AP/CSE
IT22101 PROGRAMMING FOR PROBLEM SOLVING SVCE

Sample Input / Output


Enter the String1
Good
Enter the String2
Morning
The Concatenated String is GoodMorning

/*Program to Reverse a string without using Library Function*/


#include<stdio.h>
void main()
{
char s1[20],s2[20];
int i=0,j=0;
printf("\nEnter the String1\n");
scanf("%s",s1);
while(s1[i]!='\0')
i++;
i=i-1;
while(i>=0)
{
s2[j]=s1[i];
i--;
j++;
}
s2[j]='\0';
printf("\nThe Reverse of the String %s is %s ",s1,s2);
}

Sample Input / Output


Enter the String1
welcome
The Reverse of the String welcome is emoclew

/*Palindrome or not*/

PROGRAM:

# include <stdio.h>
# include <string.h>

PREPARED BY
R.GNANAVEL AP/CSE
IT22101 PROGRAMMING FOR PROBLEM SOLVING SVCE

void main()
{
char str[20], rev[20] ;
int i, j, l ;
printf("Enter a string : ") ;
scanf("%s", str) ;
for(l = 0 ; str[l] != '\0' ; l++) ;
for(i = l - 1, j = 0 ; i >= 0 ; i--, j++)
{
rev[j] = str[i] ;
}
rev[j] = '\0' ;
if(stricmp(str, rev) == 0)
printf("\nThe given string is a palindrome") ;
else
printf("\nThe given string is not a palindrome") ;
}

OUTPUT:

Enter a string : madam


The given string is a palindrome

/*SORT THE STRINGS IN ALPHABETICAL ORDER*/


PROGRAM:
#include <stdio.h>
#include <string.h>
void main()
{
char str[10][20], temp[20] ;
int n, i, j ;
printf("Enter the number of strings : ") ;
scanf("%d", &n) ;
printf("\nEnter the strings : \n\n") ;
for(i = 0 ; i < n ; i++)
{
scanf("%s", str[i]) ;
}
for(i = 0 ; i < n - 1 ; i++)
{
for(j = 0 ; j < n - 1; j++)
PREPARED BY
R.GNANAVEL AP/CSE
IT22101 PROGRAMMING FOR PROBLEM SOLVING SVCE

{
if(strcmp(str[j], str[j + 1]) > 0)
{
strcpy(temp, str[j]) ;
strcpy(str[j], str[j + 1]) ;
strcpy(str[j + 1], temp) ;
}
}
}
printf("\nThe sorted order of strings are : \n\n") ;
for(i = 0 ; i < n ; i++)
{
printf("%s \n", str[i]) ;
}
}

OUTPUT:
Enter the number of strings : 5
Enter the strings :
viji
udaya
priya
bhuvan
satish

The sorted order of strings are :


bhuvan
priya
satish
udaya
viji

/*Program to count vowels, consonants etc*/


Program:

#include <stdio.h>

void main()
{
char line[150];
int i, vowels, consonants, digits, spaces;

PREPARED BY
R.GNANAVEL AP/CSE
IT22101 PROGRAMMING FOR PROBLEM SOLVING SVCE

vowels = consonants = digits = spaces = 0;

printf("Enter a line of string: ");


scanf("%[^\n]", line);

for(i=0; line[i]!='\0'; ++i)


{
if(line[i]=='a' || line[i]=='e' || line[i]=='i' ||
line[i]=='o' || line[i]=='u' || line[i]=='A' ||
line[i]=='E' || line[i]=='I' || line[i]=='O' ||
line[i]=='U')
{
++vowels;
}
else if((line[i]>='a'&& line[i]<='z') || (line[i]>='A'&& line[i]<='Z'))
{
++consonants;
}
else if(line[i]>='0' && line[i]<='9')
{
++digits;
}
else if (line[i]==' ')
{
++spaces;
}
}

printf("Vowels: %d",vowels);
printf("\nConsonants: %d",consonants);
printf("\nDigits: %d",digits);
printf("\nWhite spaces: %d", spaces);
}

Output:

Enter a line of string: adfslkj34 34lkj343 34lk


Vowels: 1
Consonants: 11
Digits: 9
White spaces: 2

PREPARED BY
R.GNANAVEL AP/CSE
IT22101 PROGRAMMING FOR PROBLEM SOLVING SVCE

/*SELECTION SORT*/
Program:
#include<stdio.h>
void main()
{
inti,j,k,n,c,a[20],min;
printf("$$$ SELECTION SORT $$$");
printf("\nEnter the array limit:");
scanf("%d",&n);
printf("Enter array elements:");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("Passes of selection sort:\n");
for(i=0;i<n-1;i++)
{
min=i;;
for(j=i+1;j<n;j++)
{
if(a[min]>a[j])
{
min=j;
}
}
if(i!=min)
{
k=a[i];
a[i]=a[min];
a[min]=k;
}
printf("\nPass=%d,\t",i+1);
for(c=0;c<n;c++)
{
printf("%d\t",a[c]);
}
}
printf("\nThe sorted elements are:\t");
for(i=0;i<n;i++)
{
printf("%d\t",a[i]);
}
PREPARED BY
R.GNANAVEL AP/CSE
IT22101 PROGRAMMING FOR PROBLEM SOLVING SVCE

OUTPUT:
$$$ SELECTION SORT $$$
Enter the array limit: 6
Enter array elements: 23 78 45 8 32 56
Passes of selection sort:
Pass=1, 8 78 45 23 32 56
Pass=2, 8 23 45 78 32 56
Pass=3, 8 23 32 78 45 56
Pass=4, 8 23 32 45 78 56
Pass=5, 8 23 32 45 56 78
The sorted elements are: 8 23 32 45 56 78

/*LINEAR SEARCH*/
Program:
#include<stdio.h>
void main()
{
int a[10],i,n,key,f=0;
printf("\n\n LINEAR SEARCH \n\n");
printf("Enter the array limit:\t");
scanf("%d",&n);
printf("Enter array elements:\n");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("Enter the element to be searched:\t");
scanf("%d",&key);
for(i=0;i<n;i++)
{
if(a[i]==key)
{
printf("Element found\n");
f=1;
break;
}
}
if(f==0)
{
PREPARED BY
R.GNANAVEL AP/CSE
IT22101 PROGRAMMING FOR PROBLEM SOLVING SVCE

printf("Element not found\n");


}
}

OUTPUT:
LINEAR SEARCH
Enter the array limit: 5
Enter array elements:
1
3
5
7
9
Enter the element to be searched: 9
Element found

LINEAR SEARCH
Enter the array limit: 5
Enter array elements:
2
4
6
8
10
Enter the element to be searched: 9
Element not found

/*BINARY SEARCH*/
PROGRAM:
#include<stdio.h>
void main()
{
int c, first, last, middle, n, f=0, search, array[100];
printf("\n\n\n Binary Search using Array \n\n\n");
printf("Enter number of elements:\t");
scanf("%d",&n);
printf("Enter %d integers\n", n);
for(c=0;c<n;c++)
{
scanf("%d",&array[c]);
}
PREPARED BY
R.GNANAVEL AP/CSE
IT22101 PROGRAMMING FOR PROBLEM SOLVING SVCE

printf("Enter value to find:\t");


scanf("%d",&search);
first = 0;
last = n - 1;
while( first <= last )
{
middle = (first+last)/2;
if ( search < array[middle] )
{
last = middle - 1;
}
else if( search > array[middle] )
{
first= middle + 1;
}
else
{
printf("%d found at location %d.\n", search, middle+1);
f=1;
break;
}
}
if ( f==0 )
{
printf("Not found! %d is not present in the list.\n", search);
}
}

OUTPUT:

Binary Search using Array

Enter number of elements: 5


Enter 5 integers
2
4
6
8
10
Enter value to find: 10
10 found at location 5.

PREPARED BY
R.GNANAVEL AP/CSE
IT22101 PROGRAMMING FOR PROBLEM SOLVING SVCE

Binary Search using Array

Enter number of elements: 5


Enter 5 integers
1
3
5
7
9
Enter value to find: 8
Not found! 8 is not present in the list.

PREPARED BY
R.GNANAVEL AP/CSE

You might also like