0% found this document useful (0 votes)
259 views38 pages

Unit - Ii: Important Two Mark Questions With Answer

An array is a group of related data items that share a common name. Arrays can be one-dimensional, two-dimensional, or multi-dimensional. One-dimensional arrays use a single subscript to allocate continuous memory locations. Arrays must be declared before use by specifying the data type, array name, and size. Arrays can be initialized during declaration by providing initial values inside curly braces.

Uploaded by

Shashi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
259 views38 pages

Unit - Ii: Important Two Mark Questions With Answer

An array is a group of related data items that share a common name. Arrays can be one-dimensional, two-dimensional, or multi-dimensional. One-dimensional arrays use a single subscript to allocate continuous memory locations. Arrays must be declared before use by specifying the data type, array name, and size. Arrays can be initialized during declaration by providing initial values inside curly braces.

Uploaded by

Shashi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 38

STAFF NAME: GOUTHAM.

E Arrays and strings – Programming in C

UNIT – II
Important two mark questions with answer

1) What is an array? What are the classifications of an array? (AU – 2010)


Array means sequence of elements that share a common name with similar data types.
This is known as Array.
Types:
 One-dimensional array.
 Two-dimensional array and,
 Multi-dimensional array.
2) Write the features of arrays. (AU Jan 2013)
 An array is a derived data types. It is used to represent a collection of elements of the
same data type.
 The elements can be accessed with base address and the subscript defined for the
position of the element.
 The elements are stored in continuous memory location.
 The starting memory location is represented by the array name and it is known as the
base address of the array.
3) Define one – dimensional array.
The collection of data item can be stored under a one variable name using only one
subscript , such a variable is called one – dimensional array.
Example:
int a[10];
4) What is two – dimensional array?
An array with two subscripts is termed as two-dimensional array. A two dimensional
array enables us to store multiple rows of elements.
Example:
int a[10][10];
5) Give example for array initialization.
An array can be initialized at the time of declaration is known as compile time
initialization.
Example:
int a[5]={5,8,7,4,1};
An array can be explicitly initialized at run time.

Example:
for(i=0;i<5;i++)
scanf(“%d”,&a[i]);

1
STAFF NAME: GOUTHAM.E Arrays and strings – Programming in C

6) What is the value of b[0] in the following program? (AU-Chennai,Jan.2012)


main()
{
int a[5]={1,3,6,7,0};
int *b;
b=&a[2];
}
Output: 6.
7) Why do you need to use array?
In many cases, we need to declare a set of variables that are of the same data type.
Instead of declaring each variable separately, we can declare all variables collectively in the
format of an array. Each variable, as an element of the array, can be accessed either through
the array element references or through a pointer that references the array.
8) What is the minimum index of an array?
The minimum index of a one-dimensional array is 0(zero), which marks the first
element of the array.
Example:
int a[8];
The first element of the array is a[0]. Likewise, for a multidimensional array, the
minimum index of each dimension starts at 0(zero).
9) List out the disadvantages of an array.
 The elements in the array must be same data types.
 The size of an array is fixed.
 If we need more space at run time, it is not possible to extend array.
 The insertion and deletion an operation is an array require shifting of elements which
takes time.
10) List out few characteristics of arrays.
 All the elements of an array share the same name and they are distinguished from one
another with help of an element number.
 The element number in an array plays major role for calling each element.
 An element of an array a[] can be assigned.
 The array elements are stored in continuous memory locations.
11) List out the properties of an array.
 All the elements of an array share the same name and they are distinguished from one
another with the help of an element number.
 The element number in an array plays major role for calling each element.
 The type of an array is the data type of its elements.
 The array elements are stored in continuous memory locations.
 The location of an array is the location of its first element.

2
STAFF NAME: GOUTHAM.E Arrays and strings – Programming in C

 The length of an array is the no. of data elements in the array.


 The size of an array is the length of the array times the size of an elements.
12) What is string? Give example.
String is the sequence of character or array of character enclosed within double quotes.
That string must terminate with null character (`\0`).
Eg. “super”.
13) How strings are represented in language C? (AU-Chennai, Jan. 2010)
String is the sequence of character or array of character enclosed within double quotes.
That string must terminate with null character (`\0`).
Eg.
char str[]=”super”;
The elements of the array are
str[0]=’s’;
str[1]=’u’;
str[2]=’p’;
str[3]=’e’;
str[4]=’r’;
str[5]=’\0’;
14) List out the types of string handling functions.
 strlen() - Used to find the length of a string.
 strcat() - Used to combine two strings.
 strrev() - Used to reverse a string.
 strcmp() - Used to compare a string with another string.
 strcpy() - Used to copy a string to another string.
And so on…
15) Define strlen() function.
strlen(), this function is used to count and return the number of character present in a
string.
Syntax:
len=strlen(string);
16) Define strcat() function.
strcat(), this function is used to concatenate or combine two strings together then
forms a new string.
Syntax:
strcat(str1,str2);
17) Define strrev() function.
strrev(), this function is used to reverse a string. This function takes and returns only
one argument.
Syntax:
strrev(str);

3
STAFF NAME: GOUTHAM.E Arrays and strings – Programming in C

18) Define strcmp() function.


strcmp(), this function which compares two strings to find whether they are same or
different. If two strings are equal means it returns a zero otherwise numeric difference
between the non-matching characters.
Syntax:
strcmp(str1,str2);

19) Define strcpy() function.


strcpy(), this is used to copy the contents of a string to another strings.
Syntax:
strcpy(str1,str2);

20) List out few types of sorting.


 Bubble sort.
 Selection sort.
 Insertion sort.
 Merge sort.
 Heap sort.
 Quick sort.

21) Declare a character array of size 5 and assign vowels to it. (Nov/Dec 2015)
Array Declaration with Initialization
type array_name[size] = {value_list};
For example:
char vowel[6] = {'a', 'e', 'i', 'o', 'u', '\0'};

22) Give some examples of string functions. (Nov/Dec 2015)


Strlen( )
char str1[20] = "BeginnersBook";
printf("Length of string str1: %d", strlen(str1));

Strcat()
char s1[10] = "Hello";
char s2[10] = "World";
strcat(s1,s2);
printf("Output string after concatenation: %s", s1);

Strcpy()
char s1[30] = "string 1";
char s2[30] = "string 2 : I’m gonna copied into s1";
strcpy(s1,s2);
printf("String s1 is: %s", s1);

4
STAFF NAME: GOUTHAM.E Arrays and strings – Programming in C

PART - B
1) WHAT IS AN ARRAY? HOW TO DECLARE AND INITIALIZE AN
ARRAY? EXPLAIN WITH EXAMPLE.
“An array is a group of related data items that share a common name. The value is
indicated by writing a number called index (subscript) in brackets after the array name.” This
is called an array.
Types of array
• One Dimensional Array
• Two Dimensional Array
• Multi Dimensional Array
(a) One Dimensional Array
An array with a single subscript is known as one dimensional array. It is used to
allocate continuous memory location.
Example:
int rollno[5];

The memory representation of above example is given below in pictorial form.

Array name rollno[0] rollno[1] rollno[2] rollno[3] rollno[4]

Value stored 12 15 6 17 25

Address 1024 1026 1028 1030 1032


Declaration of Arrays
We need to declare the array variable before they are used like ordinary variable. The
general form of array declaration is given below.
Synatx:
datatype array-name[size];

The type specifies the type of element, such as int, float, or char.
The size indicates the maximum number of elements that can be stored inside the array.
Example:
int rno[10];
float cgpa[10];
char sname[20];

[0] [1] [2] [3] [4] [5] [6] [7] [8] [9]
int rno[10] rno

[0] [1] [2] [3] [4] [5] [6] [7] [8] [9]
float cgpa[10] cgpa

[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] …………[19]

5
STAFF NAME: GOUTHAM.E Arrays and strings – Programming in C

char sname[20] sname

Here:
int,float, char data type
rno, cgpa, sname array name
[10], [10], [20] size of an array

Initialization of Arrays
We can also initialize the elements of arrays like an ordinary variable initialization.
An array can be initialized in two way, they are

(i) Compile time initialization.


(ii) Run time initialization.

(i) Compile time Initialization


Initialization in made at the time of declaration (in the declaration part) is known as
compile time initialization. The general form of initialization of arrays is:
Syntax:
datatype array-name[size] = {list of values};

Example:
int rollno[3] = {26,32,12};

 The value of array are enclosed in braces and separated by commas.


 The values are assigned to the array by assignment operator (=).
 If the number of values in the list is less than the number of elements, then only that
many elements will be initialized.
 The remaining elements will be set to zero automatically.

(ii) Run time Initialization


When the users have to initialize more number of elements means it is difficult to use
compile time initialization. To avoid this situation the user can initialize elements in run time.
Program
/*Program showing one-dimensional array*/

#include<stdio.h>
main()
{
int a[100],sum=0,i,n;

6
STAFF NAME: GOUTHAM.E Arrays and strings – Programming in C

printf(“Enter no. of terms:”);


scanf(“%d”,&n);
printf("Enter %d integer numbers\n",n);
for(i=1;i<=n;i++)
{
scanf("%d",&a[i]); //Run time Initialization
sum=sum+a[i];
}
printf("The sum of given numbers is : %d\n",sum);
getch();
}
Note: The entire sorting & searching algorithm must use the one-dimensional array.

2) HOW WILL PASSING ONE DIMENSIONAL ARRAYS TO FUNCTION?


EXPLAIN WITH EXAMPLE. (or) Arrays as function arguments
User can pass an array element or an entire array as argument to a function. Passing
an entire array as argument is some differ from passing its individual elements as arguments.
(i) Passing individual elements
We can pass individual elements to a function like any other variable. The type of array
element matches the function parameter type, it can be passed. This method passes the value
of the array element. Since it is a passed by value, the function cannot change the original
value of the array element.
Program
#include<stdio.h>
void cube(int);
main()
{
int i,num[5]={2,4,6,8,9};
for(i=0;i<5;i++)
{
cube(num[i]);
}
}
void cube(int n)
{
printf(“%d\n”,n*n*n);
}
Output
8

7
STAFF NAME: GOUTHAM.E Arrays and strings – Programming in C

64
216
512
729
(ii) Passing the entire array
An entire array can be transferred to a function as a parameter. To transfer an array to
a function, the array name is enough without subscripts as actual parameters within the
function call.

Program
#include<stdio.h>
void sum(int,int[]);
main()
{
int a[5],i,n=5;
printf(“Enter 5 elements:\n”);
for(i=0;i<5;i++)
{
scanf(“%d”,&a[i]);
}
sum(n,a);
}

void sum(int x,int b[])


{
int add=0,i;

for(i=0;i<5;i++)
{
add=add+b[i];
}
printf(“The Answer is %d”,add);
}

Output
Enter 5 elements:
1
2
3
4

8
STAFF NAME: GOUTHAM.E Arrays and strings – Programming in C

5
The Answer is 15

3) WRITE THE C PROGRAM TO COMPUTE MEAN


#include<stdio.h>
main()
{
int i,n,a[20],sum=0;
float mean;
printf("Enter N:");
scanf("%d",&n);

for(i=0;i<n;i++)
{
printf("\nEnter %d number:", i+1);
scanf("%d",&a[i]);
}
for(i=0;i<n;i++)
{
sum=sum+a[i];
}
mean=(float)sum/n;
printf("\nThe mean value is %f",mean);
}

Output:
Enter N:5
Enter 1 number:12
Enter 2 number:134
Enter 3 number:14
Enter 4 number:5
Enter 5 number:-8
The mean value is 31.400000

4) WRITE THE C PROGRAM TO COMPUTING MEDIAN

#include<stdio.h>
main()

9
STAFF NAME: GOUTHAM.E Arrays and strings – Programming in C

{
int i,j,temp,n,a[20],sum=0;
float median;
printf("Enter N:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\nEnter %d number:", i+1);
scanf("%d",&a[i]);
}
for(i=0; i<n-1; i++)
{
for(j=i+1; j<n; j++)
{
if(a[j] < a[i])
{
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}

if(n%2==0)
{
// if there is an even number of elements, return mean of the two elements in the middle

median=((a[n/2] + a[n/2 - 1]) / 2.0);


}
else
{
// else return the element in the middle
median= a[n/2];
}
printf("\nThe median value is %f",median);
}

Output 1:
Enter N:5
Enter 1 number:11
Enter 2 number:12

10
STAFF NAME: GOUTHAM.E Arrays and strings – Programming in C

Enter 3 number:13
Enter 4 number:14
Enter 5 number:15
The median value is 13.000000

Output 2:
Enter N:4
Enter 1 number:14
Enter 2 number:12
Enter 3 number:11
Enter 4 number:13
The median value is 12.500000

5) WRITE THE C PROGRAM TO COMPUTING MODE


#include <stdio.h>
main()
{

int maxValue = 0, maxCount = 0, i, j,a[20],n,count=0;


printf("Enter N:");
scanf("%d",&n);
for (i = 0; i < n; ++i)
{
printf("\nEnter %d number:",i+1);
scanf("%d",&a[i]);
}
for (i = 0; i < n; ++i)
{
//int count = 0;

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


{
if (a[j] == a[i])
++count;
}
if (count > maxCount)
{
maxCount = count;
maxValue = a[i];

11
STAFF NAME: GOUTHAM.E Arrays and strings – Programming in C

}
}
printf("The mode value is%d",maxValue);
}

Output:
Enter N:4
Enter 1 number:0
Enter 2 number:6
Enter 3 number:7
Enter 4 number:2
Enter 5 number:7
The mode value is 7

6) ILLUSTRATE TWO-DIMENSIONAL ARRAYS WITH EXAMPLE.


Arrays whose elements are specified by two subscripts are called Two-dimensional
arrays (or) double-subscripted arrays.
Syntax:
datatype array-name[row_size][column_size];
Example:
int a[3][3];

The pictorial representation of the above example.


column 0 column 1 column 2

row 0 a[0][0] a[0][1] a[0][2]

row 1 a[1][0] a[1][1] a[1][2]

row 2 a[2][0] a[2][2] a[2][2]


Program
/*Program for two dimensional array*/
#include<stdio.h>
main()
{
int i,j,a[3][3];
printf("Enter the First Matrix");
for(i=1;i<=3;i++)
{
for(j=1;j<=3;j++)

12
STAFF NAME: GOUTHAM.E Arrays and strings – Programming in C

{
scanf("%d",&a[i][j]);
}
}
printf("Transpose Matrix");
for(i=1;i<=3;i++)
{
for(j=1;j<=3;j++)
{
printf("%d\t",a[j][i]);
}
printf(“\n”);
}
}
Output
Enter the First Matrix
1 2 3
2 3 4
5 8 7
Transpose Matrix
1 2 5
2 3 8
3 4 7
Initialization of two dimensional an array:
There are two types of array initialization, they are given below.
Types:
a) Compile time initialization.
b) Run time initialization.

a) Compile time initialization.


The two dimensional array can be initialized at the time of declaration is known
as Compile time initialization.

Example:
int a[2][3]={1,1,1,3,3,3};
(or)
int a[2][3]={
{1,1,1},
{3,3,3}
};

13
STAFF NAME: GOUTHAM.E Arrays and strings – Programming in C

This statement will initialize the elements of first row to 1 and second row to 3.

int a[2][]={1,1,1,3,3,3};
int a[][]={1,1,1,3,3,3};

The above two examples will never work. To make the above two initialization in better
manner means we must mention the column size then only the compiler knows where the first
row ends.

The row size is optional if we initialize the array in the declaration part itself.

b) Run time initialization.


An array can be explicitly initialized at run time by using scanf() function.
Example:
for(i=0;i<2;i++)
{
for(j=0;j<3;j++)
{
scanf(“%d”,&a[i][j]);
}
}

7) WRITE C PROGRAM TO CALCULATE MATRIX ADDITION


#include<stdio.h>
main()
{
int i,j,n,a[3][3],b[3][3],c[3][3];
printf(“Enter size of the Matrix:”);
scanf(“%d”,&n);
printf("Enter the First Matrix");
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
scanf("%d",&a[i][j]);
}

14
STAFF NAME: GOUTHAM.E Arrays and strings – Programming in C

printf("Enter the Second Matrix");


for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
scanf("%d",&b[i][j]);
}
}

for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
c[i][j]=a[i][j]+b[i][j];
}
}
printf("Display Addition Matrix");
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
printf("%d \t",c[i][j]);
}

printf("\n");
}

getch();

}
Output
Enter size of Matrix: 3
Enter the First Matrix
1 2 3
2 1 2
3 1 1
Enter the second Matrix
1 2 3
2 1 2
3 1 1

15
STAFF NAME: GOUTHAM.E Arrays and strings – Programming in C

Display Addition matrix


2 4 6
4 2 4
6 2 2

8) WRITE C PROGRAM TO MANIPULATE MATRIX MULTIPLICATION


#include<stdio.h>
main( )
{
int a[2][2], b[2][2],c[2][2],i,j;
int i,j,k,n;
printf("Enter first matrix:\n" );
for( i=1;i<=3;i++)
{
for( j=1;j<=3;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("Enter second matrix:\n");
for(i=1;i<=3;i++)
{
for(j=1;j<=3;j++)
{
scanf("%d",&b[i][j]);
}
}
for(i=1;i<=3;i++)
{
for(j=1;j<=3;j++)
{
c[i][j]=0;
for(k=1;k<=3;k++)
{
c[i][j] =c[i][j]+a[i][k]*b[k][j];
}
}

16
STAFF NAME: GOUTHAM.E Arrays and strings – Programming in C

}
printf("Matrix Multiplication Is: \n");
for(i=1;i<=3;i++)
{
for (j=1;j<=3;j++)
{
printf("%d\n",c[i][j]);
}
}
getch();
}

Output
Enter the First Matrix
1 2 3
4 5 6
7 8 9
Enter the second Matrix
2 4 1
6 7 4
3 5 7
Matrix Multiplication
23 33 30
56 81 66
89 129 102

9) WRITE C PROGRAM FOR THE LOGIC OF MATRIX SCALING


#include<graphics.h>
#include<stdlib.h>
#include<stdio.h>
#include<math.h>
main()
{
int graphdriver=DETECT,graphmode,errorcode;
int i;
int x2,y2,x1,y1,x,y;
printf("Enter the 2 line end points:");
printf("x1,y1,x2,y2");
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);

17
STAFF NAME: GOUTHAM.E Arrays and strings – Programming in C

initgraph(&graphdriver,&graphmode,"c:\\tc\\bgi");
line(x1,y1,x2,y2);
printf("Enter scaling co-ordinates ");
printf("x,y");
scanf("%d%d",&x,&y);
x1=(x1*x);
y1=(y1*y);
x2=(x2*x);
y2=(y2*y);
printf("Line after scaling");
line(x1,y1,x2,y2);
getch();
closegraph();
}

10) WRITE C PROGRAM TO FIND MATRIX DETERMINANT OF 2 X 2

#include<stdio.h>
main()
{
int a[2][2],i,j;
long determinant;
printf("Enter the 4 elements of matrix: ");
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
scanf("%d",&a[i][j]);
}
}
determinant = a[0][0]*a[1][1] - a[1][0]*a[0][1];

18
STAFF NAME: GOUTHAM.E Arrays and strings – Programming in C

printf("\nDeterminant of 2X2 matrix: %ld",determinant);


}

Output:
Enter the 4 elements of matrix:
4 8
3 9
Determinant of 2X2 matrix: 12

11) WRITE A C PROGRAM TO FIND MATRIX DETERMINANT FOR 3 X 3

#include<stdio.h>
main()
{
int a[3][3],i,j;
long determinant;
printf("Enter the 9 elements of matrix: ");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf("%d",&a[i][j]);
}
}

19
STAFF NAME: GOUTHAM.E Arrays and strings – Programming in C

determinant = a[0][0]*((a[1][1]*a[2][2]) - (a[2][1]*a[1][2])) -


a[0][1]*(a[1][0]*a[2][2] - a[2][0]*a[1][2]) + a[0][2]*(a[1][0]*a[2][1] -
a[2][0]*a[1][1]);
printf("\nDeterminant of 3X3 matrix: %ld",determinant);
}
Output:
Enter the 9 elements of matrix:
1 2 3
4 5 6
7 8 9
Determinant of 3X3 matrix: 0

12) WRITE A C PROGRAM TO COMPUTE MATRIX TRANSPOSE

#include<stdio.h>
main()
{
int i,j,a[3][3];
printf("Enter the First Matrix");
for(i=1;i<=3;i++)
{
for(j=1;j<=3;j++)
{
scanf("%d",&a[i][j]);
}
}

printf("Transpose Matrix");
for(i=1;i<=3;i++)
{
for(j=1;j<=3;j++)
{
printf("%d\t",a[j][i]);
}
printf(“\n”);
}

Output

20
STAFF NAME: GOUTHAM.E Arrays and strings – Programming in C

Enter the First Matrix


1 2 3
2 3 4
5 8 7

Transpose Matrix
1 2 5
2 3 8
3 4 7

13) HOW STRING IS DEFINED IN C? EXPLAIN THE OPERATIONS OF


STRING WITH EXAMPLE.
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[]={‘g’,’o’,’o’,’d’,’\0’};

Each character is stored in one byte of memory.

Example:
Sting g o o d \0

Address 1004 1005 1006 1007 1008


The operations that are performed on character strings are
 Reading and writing strings.
 Combining strings together.
 Copying one string to another.
 Comparing strings for equality.
 Extracting a portion of a string.

Declaring and Initializing String Variables


A string variable is any valid C variable name and is always declared as an array.
The general form of declaration of a string variable is
Syntax:
char string_name[size];

Example:
char city[10];

21
STAFF NAME: GOUTHAM.E Arrays and strings – Programming in C

char name[30];

When the compiler assigns a character string to a character array, it automatically


supplies a null character (‘\0’) at the end of the string. Therefore, the size should be equal to
the maximum number of characters in the string plus one.

C permits a character array to be initialized in either of the following two forms

char str[] = “THINK POSITIVE”;


char str[] = {‘T’, ‘H’, ‘I’,’N’,’K’, ‘ ‘, ‘P’, ‘O’, ‘S’, ‘I’,’T’,’I’,’V’,’E’, ‘\0’};

Reading Words
The familiar input function scanf() can be used with %s format specification to read in a
string of characters.
Example:
char address[15];
scanf(“%s”,address);
Program
/*Reading a series of words using scanf function*/
#include<stdio.h>
main()
{
char w1[40],w2[40],w3[40],w4[40];
printf(“Enter text:\n”);
scanf(“%s%s%s%s”,w1,w2,w3,w4);
printf(“\n”);
printf(“word1 = %s \n word2 = %s \n”,w1, w2);
printf(“word3 = %s \n word4 = %s \n”,w3, w4);
getch();
}

Output
Enter text:
Hi How are you
word1=Hi
word2=How
word3=are
word4=you
Note: scanf() function terminates its input on the first white space it finds.

22
STAFF NAME: GOUTHAM.E Arrays and strings – Programming in C

14) LIST OUT AND EXPLAIN IN BRIEFLY ABOUT STRING - HANDLING


FUNCTIONS WITH EXAMPLE FOR EACH
C library supports a large number of string-handling functions that can be used to carry
out many of the string manipulation activities.
Popular string functions
i. strcat( ) Concatenates two strings.
ii. strcmp( ) Compares two strings.
iii. strcpy( ) Copies one string over another.
iv. strlen( ) Finds the length of the string.
v. strrev() Finds the reverse string
Let see one by one as follows,
(i) strcat( ) Function – String concatenation
The strcat() function is used to joins two strings together.
Syntax:
strcat(string1,string2);

String1 and String2 are character type arrays or string constant


Example:
strcat(“THINK “, “POSITIVE”);

strcat(strcat(string1,string2),string3);
Here three strings are concatenated and the result is stored in string1.

Program:
#include<stdio.h>
#include<string.h>
main()
{
char str1[50],str2[50];
printf(“Enter the first string:”);
gets(str1);
printf(“Enter the second string:”);
gets(str2);
strcat(str1,str2);
printf(“Result=%s”,str1);
}
Output:
Enter the first string: Computer

23
STAFF NAME: GOUTHAM.E Arrays and strings – Programming in C

Enter the second string: Science


Result=Computer Science
Before Execution

str1 C o m p u t e r

str2 S c i e n c e

After Execution
str1 C o m p u t e r S c i e n c e

str2 S c i e n c e

(ii) strcmp( ) Function – String Comparison


It is used to compare two strings identified by the arguments and has a value 0 if they
are equal.

Syntax:
strcmp(string1,string2);

Example:
strcmp(name1,name2);
strcmp(name1,”john”;
strcmp(“ram”, “rom”);
Program:
#include<stdio.h>
#include<string.h>
main()
{
char str1[50],str2[50];
int n;
printf(“Enter the first string:”);
gets(str1);
printf(“Enter the second string:”);
gets(str2);
n=strcmp(str1,str2);
if(n==0)
printf(“Strings are equal”);
else
printf(“Strings are not equal”);
}
Output 1:
Enter the first string: computer

24
STAFF NAME: GOUTHAM.E Arrays and strings – Programming in C

Enter the second string: Programming


Strings are not equal
Output 2:
Enter the first string: computer
Enter the second string: computer
Strings are equal
(iii) strcpy( ) Function – String Copy
This function works almost like a string assignment operator. It takes the form
Syntax:
strcpy(string1,string2);
This assigns the content of string2 to string1.
Example:
strcpy(str1, “SUPER”);
strcpy(str1,str2);
Program:
#include<stdio.h>
#include<string.h>
main()
{
char str1[50],str2[50],str3[50];
int n;
printf(“Enter the first string:”);
gets(str1);
strcpy(str2,”SUPER”);
strcpy(str3,str1);
printf(“String1=%s\n”,str1);
printf(“String2=%s\n”,str2);
printf(“String3=%s\n”,str3);
getch();
}
Output :
Enter the first string: computer
String1= Computer
String2= SUPER
String3= Computer

(iv) strlen( ) Function – String Length


The process of finding the number of characters in a string with the help of strlen().
This function counts and returns the number of characters in a given string.

25
STAFF NAME: GOUTHAM.E Arrays and strings – Programming in C

Syntax:
n = strlen(string);

Program
/*Illustration of string-handling functions*/

#include<stdio.h>
#include<string.h>
main()
{
char s1[20],s2[20],s3[20];
int x,len1,len2,len3;
printf(“Enter two string constants \n”);
scanf(“%s%s”,s1,s2);
x=strcmp(s1,s2);
if(x!= 0)
printf(“Strings are not equal \n”);
else
printf(“Strings are equal \n”);
strcat(s1, s2);
strcpy(s3,s1);
len1=strlen(s1);
len2=strlen(s2);
len3=strlen(s3);
printf(“\ns1=%s\tlength=%dcharacters\n”,s1,len1);
printf(“\ns2= %s \tlength=%dcharacters\n”,s2, len2);
printf(“\ns3=%s\tlength=%dcharacters\n”,s3,len3);
}

OUTPUT
Enter two string constants
New York
Strings are not equal
s1=New York length=7 characters
s2=York length=4 characters
s3=New York length=7 characters

(V) strrev() Function – String Reverse


This function is used to find the reverse of the given string.
Syntax:

26
STAFF NAME: GOUTHAM.E Arrays and strings – Programming in C

strrev(string);

Example:
str2=strrev(“success”);
The reverse of the string “success” is stored in str2 as “sseccus”
Program:
#include<stdio.h>
#include<conio.h>
#include<string.h>
main()
{
char str1[50],str2[50];
clrscr();
printf(“Enter the string:”);
gets(str1);
str2=strrev(str1);
printf(“String1=%s\n”,str1);
printf(“String2=%s\n”,str2);
getch();
}
Output 1:
Enter the string: success
String1= success
String2= sseccus

15) WRITE A C PROGRAM TO ARRANGE ‘n’ NUMBER OF ELEMENTS IN


ASCENDING ORDER BY USING THE CONCEPT OF SELECTION SORT.

#include <stdio.h>
main()
{
int n, i, j, x, min, a[50];
printf("Enter the value of n: ");
scanf("%d", &n);
printf("Enter %d values", n);
for(i = 0; i < n; i++)
{
scanf("%d", &a[i]);
}

27
STAFF NAME: GOUTHAM.E Arrays and strings – Programming in C

printf("Before Sorting\n");
for(i = 0; i < n; i++)
{
printf("%d\t", a[i]);
}
for(i = 0; i < n; i++)
{
min=i;
for(j = i+1; j < n; j++)
{
printf("\n%d==%d\n", a[min], a[j]);
if(a[min] > a[j])
{
min = j;
}
}
if(min != i)
{
x = a[min];
a[min] = a[i];
a[i] = x;
}
}
printf("\nAfter sorting\n");
for(i = 0; i < n; i++)
{
printf("%d\t", a[i]);
}
}
Enter the value of n: 5

Enter 5 values:
12 0 -14 5 16

Before Sorting:
12 0 -14 5 16

After Sorting:
-14 0 5 12 16

28
STAFF NAME: GOUTHAM.E Arrays and strings – Programming in C

16) WRITE A C PROGRAM TO FIND SEARCHING ELEMENT IN AN ARRAY


OF NUMBERS USING LINEAR SEARCH (OR) SEQUENTIAL SEARCH
#include<stdio.h>
#inlcude<conio.h>
main()
{
int i,n,a[30],key,flag=100;
clrscr();
printf(“Enter the no. of terms:”);
scanf(“%d”,&n);
for(i=0;i<n;i++)
{
scanf(“%d”,&a[i];
}
printf(“Enter the search element:”);
scanf(“%d”,&key);
for(i=0;i<n;i++)
{
if(key==a[i])
{
printf(“The searching element is %d present in location %d”,key,i);
flag=i;
}
}
if(flag==i)
printf(“The searching element is not present”);
getch();
}

Output 1
Enter the no. of terms:5

10 12 14 16 18
Enter the search element:18
The searching element is 18 present in location 5

Output 1
Enter the no. of terms:5

29
STAFF NAME: GOUTHAM.E Arrays and strings – Programming in C

10 12 14 16 18
Enter the search element:22

The searching element is not present”);

17) WRITE A C PROGRAM TO FIND SEARCHING ELEMENT IN AN ARRAY


OF NUMBERS USING BINARY SEARCH.

#include<stdio.h>
#inlcude<conio.h>
main()
{
int i,n,a[30],key,low,high,mid;
clrscr();
printf(“Enter the no. of terms:”);
scanf(“%d”,&n);
for(i=0;i<n;i++)
{
scanf(“%d”,&a[i];
}
printf(“Enter the search element:”);
scanf(“%d”,&key);
low=0;
high=n-1;
while(low<=high)
{
mid=(low+high)/2;
if(key<a[mid])
high=mid-1;
else if(key>a[mid])
low=mid+1;
else
{
printf(“The searching element is %d present in location %d”,key,mid);
break;
}
}
getch();

30
STAFF NAME: GOUTHAM.E Arrays and strings – Programming in C

Output
Enter the no. of terms:5

10 12 14 16 18
Enter the search element:18
The searching element is 18 present in location 5

18)_WRITE A C PROGRAM TO FIND SMALLEST & LARGEST IN AN


ARRAY
#include<stdio.h>
#inlcude<conio.h>
main()
{
int i,n,a[30],large,small;
clrscr();
printf(“Enter the no. of terms:”);
scanf(“%d”,&n);
for(i=0;i<n;i++)
{
scanf(“%d”,&a[i];
}
large=a[0];
small=a[0];
for(i=0;i<n;i++)
{
if(a[i]>large)
large=a[i];
else if(a[i]<small)
small=a[i];
}
printf(“Largest element in array is %d”,large);
printf(“Smallest element in array is %d”,small);
getch();
}
Output
Enter no. of terms: 5

31
STAFF NAME: GOUTHAM.E Arrays and strings – Programming in C

12 15 2 16 85

Largest element in array is 85


Smallest element in array is 2

19)STRING HANDING PROGRAM WITHOUT USING STRING HANDLING


FUNCTION
STRING COMPARE

#include<stdio.h>
#include<conio.h>
main()
{
int length1 = 0, length2 = 0, i = 0,c = 0;
char str1[25], str2[25];
clrscr();
printf("Enter the first string:");
gets(str1);
printf("Enter the second string:");
gets(str2);

while(str1[i] != '\0' && str2[i] != '\0')


{
if(str1[i] != str2[i])
{
break;
}
i++;
}
if(str1[i] == '\0' && str2[i] == '\0')
{
printf("The strings are equal");
}
else
{
printf("The strings are NOT equal");
}

getch( );

32
STAFF NAME: GOUTHAM.E Arrays and strings – Programming in C

Output 1
Enter the first string: Hai
Enter the second string: Hai
The strings are equal

Output 2
Enter the first string: Hai
Enter the Second string: Hello
The strings are NOT equal

STRING CONCATENATION

#include<stdio.h>
#include<conio.h>
main()
{
int i, j = 0;
char str1[25], str2[25], str3[50];
clrscr();
printf("Enter the first string:");
gets(str1);
printf("Enter the second string:");
gets(str2);
i = 0;
while(str1[i] != '\0')
{
str3[j++] = str1[i];
i++;
}
i = 0;
while(str2[i] != '\0')
{
str3[j++] = str2[i];
i++;
}
str3[j] = '\0';
printf("The concatenated string is %s", str3);

33
STAFF NAME: GOUTHAM.E Arrays and strings – Programming in C

getch( );
}

Output
Enter the first string: Hai
Enter the second string: Hello
The concatenated string is HaiHello
STRING COPY

#include<stdio.h>
#include<conio.h>
main()
{
int i=0;
char str[25], copystr[25];
clrscr();
printf("Enter the string:");
gets(str);
while(str[i] != '\0')
{
copystr[i] = str[i];
i++;
}
copystr[i] = '\0';
printf("The copied string is %s", copystr);
getch( );
}
Output
Enter the string: success
The copied string is success

STRING LENGTH
#include<stdio.h>
#include<conio.h>
main()
{
int length=0;
char str[25];
clrscr();

34
STAFF NAME: GOUTHAM.E Arrays and strings – Programming in C

printf("Enter the string:");


gets(str);
while(str[length] != '\0')
{
length++;
}
printf("The length of the string %s is %d", str, length);
getch( );
}

Output
Enter the string: success
The length of the string success is 7

20) PROGRAM FOR VOWELS & CONSONANTS AND PALINDROME

#include<stdio.h>
#include<conio.h>
main()
{
int v=0,c=0,i=0;
char str[25];
clrscr();
printf("Enter the string:");
gets(str);
while(str[i] != '\0')
{
switch(str[i])
{
case 'a':
case 'A':
case 'E':
case 'I':
case 'O':
case 'U':
case 'e':
case 'i':
case 'o':
case 'u':

35
STAFF NAME: GOUTHAM.E Arrays and strings – Programming in C

v++;
break;
default:
c++;
}
i++;
}
printf("The number of vowels are %d\n",v);
printf("The number of consonants are %d",c);
getch( );
}

Output
Enter the string: success
The number of vowels are 2
The number of consonants are 5

PALINDROME STRING

#include<stdio.h>
#include<conio.h>
main()
{
int length=0, i, j = 0, c = 0;
char str[25], revstr[25];
clrscr();
printf("Enter the string:");
gets(str);
while(str[length] != '\0')
{
length++;
}
for(i = length-1; i >= 0; i--)
{
revstr[j] = str[i];
j++;
}
revstr[j] = '\0';
for(i = 0; i < length; i++)

36
STAFF NAME: GOUTHAM.E Arrays and strings – Programming in C

{
if(str[i] == revstr[i])
c++;
}
if(c == length)
{
printf("The string %s is palindrome", str);
}
else
{
printf("The string %s is not a palindrome", str);
}
getch( );
}

Output 1
Enter the string: success
The string success is palindrome

Output 2
Enter the string: liril
The string liril is palindrome

21) CONVERT THE GIVEN STRING FROM LOWER CASE CHARACTERS TO


UPPERCASE AND UPPER CASE TO LOWERCASE. Eg: (hello = hello)

#include<stdio.h>
#include<conio.h>
#include<string.h>
main()
{
char str[30];
int i=0;
printf(“Enter any string:”);
gets(str);
while(str[i]!=`\0`)
{
if(islower(str[i])
putchar(toupper(str[i]);

37
STAFF NAME: GOUTHAM.E Arrays and strings – Programming in C

else
putchar(tolower(str[i]);
i++;
}
}
Output
Enter any string: sUccEsS
SuCCeSs

Important Questions

1) About array
2) String handling functions
3) Programs
a. Mean, Median, Mode
b. Matrix Operations
i. Addition
ii. Multiplication
iii. Scaling
iv. Determinant
v. Transpose
c. Selection Sort
d. Liner and Binary search

“The Struggle you're in Today is the developing


the Strength you need for Tomorrow”

38

You might also like