Past NEB Board Examination Questions from Array
Define array. Write a program to input any 10 numbers in an array and display it. Find the biggest number among the input numbers. [2+4+4] 2076
Write a program to enter 10 integer numbers in an array and display in ascending order. [10] 2075 GIE
Write a program to input 10 integer numbers into an array and display the sum of the numbers. [10] 2075 Set A
Write a program to enter 5 integer numbers into an array and display. [5] 2075 Set B
Write a program to input any 10 integer numbers in an array and find the total. [10] 2074 Supp.
Write a program to find addition of any two matrix of size 2*2 using array. [10] 2074 Set A
Describe the syntax of array. Write a program to sort ten integer numbers in ascending order. [2+8] 2074 Set B
Write a program to enter elements into 4*4 matrix and find the sum of the elements of the matrix. [5+5] 2073 Supp.
Write a program to read five positive numbers using an array and find out the smallest among them. [10] 2073 Set C
What is array? Write a program to sort twenty integer numbers in ascending order. [2+8] 2073 Set D
Write a program which asks nth terms of numbers and sort them in ascending order. [10] 2072 Set C
What is array? Write a program to find addition of two matrices. (3x3) [2+8] 2072 Set D
What is an array? Write a program to enter 20 integer numbers into an array and display the greatest entered. [3+7] 2072 Set E
Write a program which asks the user to input ‘n’ terms of number and find out the greatest and smallest among those numbers. [10] 2070 Supp.
4.6 Array and Strings
Definition of Array
It may be convenient to store a collection of similar data elements in different separate variables. For example, if the age of 100 persons were to be stored in variables with unique names, it certainly would be difficult, that means 100 variables needed for assigning the individual values. So an array certainly solves this problem because basically the variable name is the same. We differentiate among the values in an array by its unique subscripts with defined size.
POINTS TO REMEMBER
An array is a collection of similar types of data items treated as a single unit. It acts to store related data under the same name with an index, also known as a subscript which helps to access individual array elements. Array data type may be int, float, char etc, depending upon the nature of problems.
Characteristics of Array
All the array elements share the common name.
The elements of the array are stored in contiguous memory locations.
By declaring or using an array, the program becomes short and simple which handles a large volume of similar kinds of data items.
We put the array size of fixed length as required that means once the size is declared then the size will be fixed at the execution time of the program, so called static type.
We can randomly access to every element using numeric index, index starts from 0 and ends at size-1.
Advantages of Array
It is easier for handling similar types of data in a program.
It is efficient for solving problems like sorting, searching, indexing etc.
It is very close to matrix, therefore it is easy for solving matrix related problems.
Graphic is an array of pixels, so graphics manipulation can be easily done using an array.
Disadvantages of Array
It is not possible to hold a dissimilar type of data in an array.
It is difficult to visualize the multi-dimensional array.
It is static in nature so it is difficult to define the size of array during running time.
Types of Array
There are two types of array which are as follows.
One Dimensional Array: An array which has only one subscript is named as one dimensional array, a subscript is a number of large brackets in which we put the size of the array.
Multi Dimensional Array: An array which has more than one subscript is named as a multi dimensional array, subscripts define each dimension of the array.
Declaration of One Dimensional Array: Like any other variable, we have to declare array before they are used. The general form of one dimensional array declaration is,
Syntax:
Data_type array_name[array_size];
As in above syntax, the data_type refers to the type of array elements, array the name of the array variable and the array size indicates number of elements that can be stored inside the array, Hence if the size of array is n, for example: a[n], a[0] is the first element and a[n-1] is the last element of the array.
Example 1
int a[5]:
In this example, the variable a is an array of integer with size five elements.
Example 2
float weight[100];
In this example, the variable weight is an array of floating point numbers having size 100.
Example 3
char temp[10];
In this example, temp variable is an array of characters with size 10 and it is also known as string.
Initialization
We can initialize arrays at the time of declarations. The initial value must appear which will be assigned to the individual array elements, enclosed within the brace and separated by commas.
Syntax
data_type array name [size] = {val1, val2, val3,..valn};
Here val1 is the value for the first array element, val2 is the value for the second array element, val3 is the value for the three array element and valn is the value for the last array element.
Example:
int marks[5]={50, 60, 65, 70, 55};
Here the number of subscripts determines the dimension of array ; that is one dimensional array means here is only one subscript. The value in the bracket is the size of the array. marks[0] is the first element of the array where 0 is the starting index of array.
marks[0] | marks[1] | marks[2] | marks[3] | marks[4] |
50 | 60 | 65 | 70 | 55 |
index 0 | index 1 | index 2 | index 3 | index 4 |
The values to the array elements can be assigned as follows:
marks[0]=50;
marks[1]=60;
marks[2]=65;
marks[3]=70;
marks[4]=55;
Other Examples:
int a[]={6,7,8,9,10}; //default size is 5
float x[5]={70.5,45.75,85.97,78.87,66.33};
char name[10]={‘n’,‘e’,‘p’,‘a’, ‘l’};
Assignment 19
Define the array with syntax and example.
Write down advantages of array.
Program 32 Write a program to input 5 numbers in an array and display them. [NEB 2075 Set B]
#include <stdio.h>
int main()
{
int i,num[5];
for(i=0;i<5;i++)
{
printf("Enter %d number = ", i+1);
scanf("%d",&num[i]);
}
printf("Array elements are: ");
for(i=0;i<5;i++)
{
printf(" %d ",num[i]);
}
return 0;
}
Program 33 Write a program to input 5 numbers with constant values initialization in array and display their sum.
#include <stdio.h>
int main()
{
int i,sum=0;
int num[5]={100,30,55,60,75};
printf(" The five numbers of array are: ");
for(i=0;i<5;i++)
{
printf(" %d ", num[i]);
sum=sum+num[i];
}
printf("\n Sum of five numbers is : %d ", sum);
return 0;
}
NEB OLD Questions Answers
QN. 1) Write a program to input 10 integer numbers into an array and display the sum of the numbers. [10] 2075 Set A OR
Write a program to input any 10 integer numbers in an array and find the total. [10]
2074 Supp.
Answers:
#include <stdio.h>
int main()
{
int num[10],i,sum=0;
for(i=0;i<10;i++)
{
printf("Enter %d number ",i+1);
scanf("%d", &num[i]);
}
for(i=0;i<10;i++)
{
sum=sum+num[i];
}
printf(" \n The sum is %d ", sum);
return 0;
}
Program 34 Write a program to input the age of 20 students and count the number of students having age in between 20 to 25.
#include <stdio.h>
int main()
{
int age[20],i,count=0;
for(i=0;i<20;i++)
{
printf("Student No. %d age ",i+1);
scanf("%d", &age[i]);
}
for(i=0;i<20;i++)
{
if(age[i]>=20 && age[i]<=25)
count++;
}
printf(" \n The number of students having age in between 20 to 25 is %d ", count);
return 0;
}
[HSEB 2062-10 Mark] Write an algorithm and C Program to read salaries of 200 employees and count the number of employees getting salaries between 5,000-10,000.
Algorithm:
Step 1: Start
Step 2: Set I =0 and count=0
Step 3: Check is I<200
3.1: If Yes, Input Salary and go to step 4
3.2: If No, goto step 5
Step 4: Calculate I=I+1 and go to step 3
Step 5: Set I =0
Step 6: Check is I<200
6.1: If Yes, Check is salary between 5000 to 10000. If yes increase the value of count by 1, If no, goto step 7
6.2: If No, goto step 5
Step 7: Calculate I=I+1 and go to step 6
Step 8: Display the count
Step 9: Stop
C Program:
#include <stdio.h>
int main()
{
int salary[200],i,count=0;
for(i=0;i<2;i++)
{
printf("Employee no . %d Salary = ", i+1);
scanf("%d",&salary[i]);
}
for(i=0;i<2;i++)
{
if(salary[i]>=5000 && salary[i]<=10000)
count++;
}
printf("Number of employee getting salary between 5000 to 10000 is %d ", count);
return 0;
}
[HSEB 2063-10 Mark] Write a C program to read the age of 40 students and count the number of students of the age between 15 to 22.
Answer:
#include <stdio.h>
int main()
{
int age[40],i,count=0;
for(i=0;i<40;i++)
{
printf("Student No. %d age ",i+1);
scanf("%d", &age[i]);
}
for(i=0;i<40;i++)
{
if(age[i]>=15 && age[i]<=22)
count++;
}
printf(" \n The number of students having age in between 15 to 22 is %d ", count);
return 0;
}
[HSEB 2068-10 Mark] Write a C Program to read salaries of 300 employees and count the number of employees getting salaries between 10,000 to 15,000.
Answer:
#include <stdio.h>
int main()
{
int salary[300],i,count=0;
for(i=0;i<300;i++)
{
printf("Employee No. %d salary ",i+1);
scanf("%d", &salary[i]);
}
for(i=0;i<300;i++)
{
if(salary[i]>=10000 && salary[i]<=15000)
count++;
}
printf(" \n The number of Employee getting salary between 10000 to 15000 is %d ", count);
return 0;
}
[HSEB 2070-10 Mark] Write a C Program to read salaries of 25 employees and count the number of employees getting salaries between 30,000 to 40,000.
Answer:
#include <stdio.h>
int main()
{
int salary[25],i,count=0;
for(i=0;i<2;i++)
{
printf("Employee No. %d salary ",i+1);
scanf("%d", &salary[i]);
}
for(i=0;i<2;i++)
{
if(salary[i]>=30000 && salary[i]<=40000)
count++;
}
printf(" \n The number of Employee getting salary between 30000 to 40000 is %d ", count);
return 0;
}
[NEB 2076- 2+4+4] Define array. Write a program to input any 10 numbers in an array and display it. Find the biggest number among the input numbers.
Answer:
C program to display 10 numbers using array
#include <stdio.h>
int main()
{
int num[10],i;
for(i=0;i<10;i++)
{
printf(" Enter %d number ", i+1);
scanf("%d", &num[i]);
}
printf("Entered ten numbers are : \n ");
for(i=0;i<10;i++)
{
printf(" %d ",num[i]);
}
return 0;
}
Also,biggest number among ten numbers
#include <stdio.h>
int main()
{
int num[10],i,big;
for(i=0;i<10;i++)
{
printf(" Enter %d number ", i+1);
scanf("%d", &num[i]);
}
big=num[0];
for(i=0;i<10;i++)
{
if(num[i]>big)
big=num[i];
}
printf("Biggest number is %d ",big);
return 0;
}
[NEB 2073- 10 Marks] Write a C program to read five positive numbers using an array and find out the smallest among them.
Answer:
#include <stdio.h>
int main()
{
int num[5],i,smallest;
for(i=0;i<5;i++)
{
printf("Enter %d number ", i+1);
scanf("%d", &num[i]);
}
smallest=num[0];
for(i=0;i<5;i++)
{
if(num[i]<smallest)
smallest=num[i];
}
printf("smallest number is %d ", smallest);
return 0;
}
Program 35 Write a program to find the largest number among ‘n’ numbers.
#include <stdio.h>
int main()
{
int num[100],i,n,largest;
printf("Enter the size of array not more than 100 ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf(" Enter %d number ", i+1);
scanf("%d", &num[i]);
}
largest=num[0];
for(i=0;i<n;i++)
{
if(num[i]>largest)
largest=num[i];
}
printf("Largest number is %d ",largest);
return 0;
}
Program 36 Write a program to input ‘n’ numbers and search whether it is in the list or not.
#include <stdio.h>
int main()
{
int n,num[100],i,searchnum;
printf(" Enter the size of array not more than 100 ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter %d number ", i+1);
scanf("%d", &num[i]);
}
printf("Enter a number to be searched ");
scanf("%d",&searchnum);
for(i=0;i<n;i++)
{
if(searchnum==num[i])
{
printf("%d is in the list", searchnum);
break;
}
}
if(n==i)
printf(" %d is not in the list", searchnum);
return 0;
}
[NEB 2074 , 2077 - 10 Marks] Define array. Describe the syntax of array. Write a program to sort ten integer numbers in ascending order.
Answer:
Syntax of Array:
Data_type Array_name [Array-size];
C Program
#include <stdio.h>
int main()
{
int num[10],i,j,temp;
for(i=0;i<10;i++)
{
printf("Enter %d numebr ",i+1);
scanf("%d", &num[i]);
}
for(i=0;i<9;i++)
{
for(j=i+1;j<10;j++)
{
if(num[i]>num[j])
{
temp=num[i];
num[i]=num[j];
num[j]=temp;
}
}
}
printf(" Array in Ascending Order \n");
for(i=0;i<10;i++)
{
printf(" %d ", num[i]);
}
return 0;
}
Ten integers in Descending order:
#include <stdio.h>
int main()
{
int num[10],i,j,temp;
for(i=0;i<10;i++)
{
printf("Enter %d number ", i+1);
scanf("%d",&num[i]);
}
for(i=0;i<9;i++)
{
for(j=i+1;j<10;j++)
{
if(num[i]<num[j])
{
temp=num[i];
num[i]=num[j];
num[j]=temp;
}
}
}
printf("Ten integers in Descending order\n");
for(i=0;i<10;i++)
{
printf(" %d ",num[i]);
}
return 0;
}
Program 37 Write a program to input ‘n’ numbers and sort them in ascending order.
#include <stdio.h>
int main()
{
int n,num[100],i,j,temp;
printf("Enter the size of array not more than 100\n ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter %d number ", i+1);
scanf("%d",&num[i]);
}
for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
{
if(num[i]>num[j])
{
temp=num[i];
num[i]=num[j];
num[j]=temp;
}
}
}
printf("Numbers in Ascending order\n");
for(i=0;i<n;i++)
{
printf(" %d ",num[i]);
}
return 0;
}
Assignment 20
Write a program to find the smallest number among 10 numbers.
Write a program to input 10 numbers and sort them in descending order.
Multi Dimensional Array
Multi dimensional arrays are defined the same as one-dimensional arrays, except that it consists of more than one pair of square brackets that are subscripts. Thus a two dimensional array will require two square brackets, one for row size and other for column size. The maximum capacity of elements of an array is the product of row size and column size. A three dimensional array will require three square brackets, and so on.
In general terms, a multidimensional array definition can be written as
data_type array_name [expression1] [expression2]..... [expression n];
In two dimensional array declaration we write,
data_type array_name [row_size] [column_size];
As in above syntax, the data_type refers to the type of array elements, array_name is the name of the array variable and row_size, column_size are the number of array elements associated with each subscript that can be stored inside the array.
For example, a[m][n] is a 2 dimensional array in which the starting element of array is a[0][0] and the last element of array is a[m-1][n-1]. Here, in a[m][n], m denotes the row size and n denotes the column size in array.
Initialization
If we have m*n array, it will have m*n elements and will require m*n element size bytes of storage. To allocate storage for an array you must reserve this amount of memory. The elements of a two dimensional array are stored row wise.
In two dimensional array (suppose 3x3 matrix), data items are stored in memory below:
| Column 0 [0][0] | Column 1 [0][1] | Column 2 [0][2] |
Row 0 | 100 | 200 | 300 |
| [1][0] | [1][1] | [1][2] |
Row 1 | 200 | 150 | 50 |
| [2][0] | [2][1] | [2][2] |
Row 2 | 350 | 500 | 600 |
As in above example, two dimensional array consist of 3 rows and 3 columns, the starting array element is 100 that is stored in position matrix[0][0] and the last array element is 600 that is stored in position matrix[2][2].
This means,
int matrix[3][3]={100, 200, 300, 200, 150, 50, 350, 500, 600};
matrix[0][0]= 100;
matrix[0][1]= 200;
matrix[0][2]= 300;
matrix[1][0]= 200;
matrix[1][1]= 150;
matrix[1][2]= 50;
matrix[2][0]= 350;
matrix[2][1]= 500;
matrix[2][2]= 600;
Program 38 Write a program to store 9 numbers with constant values in 2 dimensional array that is 3x3 matrix and print in matrix form.
#include <stdio.h>
int main()
{
int matrix[3][3]={1,2,3,4,5,6,7,8,9},i,j;
printf("The given matrix is \n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("%3d",matrix[i][j]);
}
printf("\n");
}
return 0;
}
Worked out Examples
Program 39 Write a program to input data in two dimensional array for example 3x3 matrix and display in matrix form.
#include <stdio.h>
int main()
{
int matrix[3][3],i,j;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("Enter [%d][%d] elements" , i+1,j+1);
scanf("%d",&matrix[i][j]);
}
}
printf("The given matrix is \n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("%3d",matrix[i][j]);
}
printf("\n");
}
return 0;
}
Program 40 Write a program to input a 2x3 matrix and display in transpose form that is 3x2 matrix.
#include <stdio.h>
int main()
{
int matrix[2][3],i,j;
for(i=0;i<=1;i++)
{
for(j=0;j<3;j++)
{
printf("Enter [%d][%d] elements" , i+1,j+1);
scanf("%d",&matrix[i][j]);
}
}
printf("The transpose of given matrix is \n");
for(i=0;i<3;i++)
{
for(j=0;j<2;j++)
{
printf("%3d",matrix[j][i]);
}
printf("\n");
}
return 0;
}
[NEB Question - 2074 ]Write a program to find addition of any two matrix of size 2*2 using array. [10]
#include <stdio.h>
int main()
{
int m1[2][2],m2[2][2],m3[2][2],i,j;
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
printf("Enter [%d][%d] elements of First Matrix " , i+1,j+1);
scanf("%d",&m1[i][j]);
}
}
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
printf("Enter [%d][%d] elements of Second Matrix " , i+1,j+1);
scanf("%d",&m2[i][j]);
}
}
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
m3[i][j]=m1[i][j]+m2[i][j];
}
}
printf("Sum of given matrix is \n");
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
printf("%d\t",m3[i][j]);
}
printf("\n");
}
return 0;
}
[NEB Question 2073 Supp ]Write a program to enter elements into 4*4 matrix and find the sum of the elements of the matrix. [5+5]
#include <stdio.h>
int main()
{
int m1[4][4],m2[4][4],m3[4][4],i,j;
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
printf("Enter [%d][%d] element of First matrix ",i+1,j+1);
scanf("%d",&m1[i][j]);
}
}
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
printf("Enter [%d][%d] element of Second matrix ",i+1,j+1);
scanf("%d",&m2[i][j]);
}
}
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
m3[i][j]=m1[i][j]+m2[i][j];
}
}
printf("Sum of two 4*4 matrices is \n");
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
printf(" %3d ",m3[i][j]);
}
printf("\n");
}
return 0;
}
Program 41 Write a program to input two matrices. Add two matrices and display in proper format.
#include <stdio.h>
int main()
{
int m1[5][5],m2[5][5],m3[5][5],i,j,r,c;
printf("Enter the row and column size");
scanf("%d%d",&r,&c);
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
printf("Enter [%d][%d] element of First matrix ",i+1,j+1);
scanf("%d",&m1[i][j]);
}
}
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
printf("Enter [%d][%d] element of Second matrix ",i+1,j+1);
scanf("%d",&m2[i][j]);
}
}
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
m3[i][j]=m1[i][j]+m2[i][j];
}
}
printf("Sum of two %d*%d matrices is \n",r,c);
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
printf(" %3d ",m3[i][j]);
}
printf("\n");
}
return 0;
}
Differentiate between one dimensional and two dimensional array:
One Dimensional Array | Two Dimensional Array |
It consists of only one subscript. | It consists of two subscripts. |
Maximum size will be the size of the array which we define in the program. | Maximum size will be the product of row and column size of array which we define in the program. |
It stores data either row wise or column wise. | It stores data in matrix form that is row wise and column wise. |
Syntax: Data_type array_name[array_size]; | Syntax: Data_type array_name[row_size][column_size]; |
Example: int num[5]; | Example: int matrix[2][2]; |
Program: #include <stdio.h> int main() { int num[4]={1,2,3,4},i; for(i=0;i<4;i++) { printf("%3d",num[i]); } return 0; }
| Program: #include <stdio.h> int main() { int matrix[2][2]={1,2,3,4},i,j; for(i=0;i<2;i++) { for(j=0;j<2;j++) { printf("%3d",matrix[i][j]); } printf("\n"); } return 0; } |
Assignment 21
Compare one dimensional array and two dimensional array.
Write a program to transpose a given matrix with size 2x2.
Write a program to subtract two 2x2 matrices.
String (Important Question)
NEB PAST QUESTIONS
Explain the function with an example. Write a program to demonstrate any two string function using C. [2+5.5] 2077 Mangsir 10
Explain any two string functions with examples.[5] 2076
What is function? Explain any four string functions with example.[2+8] 2075 “A”
Answer:
A self contained block of code that performs a specific task is called function. In C programming, there are two types of function.
(i) User Defined Function
(ii) Library Function
Components of function:
What is string? Explain any four string function with an example. [2+8] 2074
Describe the string manipulation function in C. Explain strcpy and strcmp with example. [2+3] 2073
Describe any five “string handling functions “ with examples. [10] 2070
*********
Definition of String:
String is the set of characters,digits and symbols. We can also define string as the array of characters. The end of the string is marked with a special character , the ‘\0’ that means Null character. The size in a character string represents the maximum number of characters that the string can hold.
POINTS TO REMEMBER
A string is an array of characters. It contains characters, symbols, numbers etc.
Syntax:
char string_name[string_size];
As in above syntax, char refers to the character data type that will be stored in the array, string_name is the name of the string variable and string_size indicates the number of characters that can be stored inside the array.
Example
char name[10];
char address[10];
This declares the name as a character array (string) variable that can hold a maximum of 10 characters.
String is simply a sequence of characters, so to declare a string, we have to put data type char with string name with square brackets.
Initialization of Strings
Let us take a string:
char name[10]={‘c’, ‘ ’, ‘p’, ‘r’, ‘o’, ‘g’, ‘r’, ‘a’, ‘m’, ‘\0’};
or second method
char name[10]= “c program” ;
Each character is treated as an element of the array name and is stored in the memory as follows:
name[0] | name[1] | name[2] | name[3] | name[4] | name[5] | name[6] | name[7] | name[8] | name[9] |
‘c’ | ‘ ’ | ‘p’ | ‘r’ | ‘o’ | ‘g’ | ‘r’ | ‘a’ | ‘m’ | ‘\0’ |
Index 0 | Index 1 | Index 2 | Index 3 | Index 4 | Index 5 | Index 6 | Index 7 | Index 8 | Index 9 |
When the compiler sees a character string, it terminates it with an additional null character means empty. Thus, the element name[9] holds the null character ‘\0’ at the end. When declaring a character array, we must always allow one extra element space for the null terminator.
Program 42 Write a program to input character wise in array and display them.
#include <stdio.h>
int main()
{
char name[10]={'c','','p','r','o','g','r','a','m','\0'};
printf("The given string is : %s",name);
return 0;
}
Program 43 Write a program to input string in array and display them.
#include <stdio.h>
int main()
{
char name[10]="c program";
printf("The given string is : %s",name);
return 0;
}
Assignment 22
What do you mean by the term string? Explain it with syntax and example.
Differentiate between array and string with suitable examples.
Array of String
A two dimensional array of characters is called an array of strings. It is also known as a table of strings. It is very useful for solving string sorting problems. In other words, an array of strings can be declared and handled like a two dimensional array.
Syntax:
char string_name[MAX][SIZE]:
As in above syntax, char refers to character data type, string_name is the name of the string variable and in two dimensional array, [MAX] indicates the number of strings and [SIZE] indicates the maximum number characters associated with string.
Example
char name[5][10];
In this example, here is a two dimensional character array. The array is initialized with five character strings with maximum size ten.
Program to demonstrate table of strings
#include <stdio.h>
int main()
{
int i;
char name[5][30]={"Dipendra","Manisha","Suyog","Ram","Laxman"};
for(i=0;i<5;i++)
{
printf("%s\n",name[i]);
}
return 0;
}
String Handling Functions
The C library supports a large number of string handling functions that can be used to carry out many of the string manipulation. The header file #include<string.h> is used for string manipulation functions. Some of the common string manipulation functions are:
strlen() function
The strlen() function returns the length of a string which takes the string name as an argument.
Syntax:
l=strlen(str);
where, str is the name of string and l is the length of the string, returned by function.
Program 44 Write a program to find the length of a string.
#include <stdio.h>
#include <string.h>
int main()
{
char str[100];
int l;
printf("Enter any string = ");
scanf("%s",str);
l=strlen(str);
printf("Length of string is %d ", l);
return 0;
}
strrev() function
The strrev() function is used to reverse the given string.
Syntax:
strrev(str);
where str is string to be reversed.
Program 45 Write a program to reverse the given string.
#include <stdio.h>
#include<string.h>
int main()
{
char str[100];
printf("Enter any string = ");
scanf("%s",str);
strrev(str);
printf("Reverse of string is %s ",str);
return 0;
}
strcpy() function
The strcpy() function is used to copy one string into another string.
Syntax:
strcpy(destination,source) or
strcpy(strl,str2);
where strl,str2 are two strings.
The content of string str2 is copied on string strl.
Program 46 Write a program to copy one string into another string.
#include <stdio.h>
#include<string.h>
int main()
{
char str1[100],str2[100];
printf("Enter any string = ");
scanf("%s",str1);
strcpy(str2,str1);
printf("After copying the string is %s ", str2);
return 0;
}
strcat() function
The strcat() function is used to join or concatenate one string into another string.
Syntax:
strcat(strl,str2);
where str1,str2 are two strings. The content of string str2 is concatenated with string str1.
Program 47 Write a program to concatenate two strings into one string.
#include <stdio.h>
#include<string.h>
int main()
{
char str1[100],str2[100];
printf("Enter two strings = ");
scanf("%s%s",str1,str2);
strcat(str1,str2);
printf("After copying the string is %s ", str1);
return 0;
}
strcmp( ) function
The strcmp() function is used to compare two strings, character by character and stops comparison when there is a difference in the ASCII value or the end of any one string and returns ASCII difference of the character that is integer.
Syntax:
v=strcmp(strl,str2);
Where, str1 and str2 are two strings to be compared and v is a value returned by the function which is either zero(equal), positive value(descending) or negative value(ascending).
Program 48 Write a program to compare two strings
#include <stdio.h>
#include<string.h>
int main()
{
char str1[50],str2[50];
printf("Enter two strings : ");
scanf("%s%s",str1,str2);
int v=strcmp(str1,str2);
if(v==0)
printf("Both strings are same and have same ASCII value");
else if(v>0)
printf("%s comes after %s or %s has more ASCII value than %s",str1,str2,str1,str2);
else
printf("%s comes before %s or %s has more ASCII value than %s",str2,str1,str2,str1);
return 0;
}
strlwr() function
The strlwr() function is used to convert upper case characters of string into lower case characters.
Syntax:
strlwr (str);
where str is string to be converted into lower case characters.
Program 49 Write a program to convert the given string into lower case.
#include <stdio.h>
#include<string.h>
int main()
{
char str[50];
printf("Enter a string : ");
scanf("%s",str);
strlwr(str);
printf("Given string in lower case is %s ",str);
return 0;
}
strupr() function
The strupr() function is used to convert lower case characters of string into upper case characters.
Syntax:
strupr (str);
where str is string to be converted into upper case characters.
Program 50 Write a program to input string in array and display them.
#include <stdio.h>
#include<string.h>
int main()
{
char str[50];
printf("Enter a string : ");
scanf("%s",str);
strupr(str);
printf("Given string in upper case is %s ",str);
return 0;
}
Assignment 23
Define array of strings.
List five string handling functions with respective syntax.
Worked Out Examples
Program 51 Write a program to find the length of the given string without using built in function.
#include <stdio.h>
#include<string.h>
int main()
{
char str[50];
int i,length=0;
printf("Enter a string : ");
scanf("%s",str);
for(i=0;str[i]!='\0';i++)// no body so use semicolon
{
length++;
}
printf("Length of string is %d",length);
return 0;
}
Program 52 Write a program to input strings and sort them.
#include<stdio.h>
#include<string.h>
int main()
{
char string[100][100],temp[100];
int i,j,n;
printf("enter n as total strings\n");
scanf("%d",&n);
printf("enter strings\n");
for(i=0;i<n;i++)
{
printf(" string for location %d = ",i);
scanf("%s",string[i]);
}
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(strcmp(string[i],string[j])>0)
{
strcpy(temp,string[i]);
strcpy(string[i],string[j]);
strcpy(string[j],temp);
}
}
}
printf("sorted strings are\n");
for(i=0;i<n;i++)
{
printf("name =%s \n",string[i]);
}
return 0;
}
Exercise 4(F)
Define the term array.
Differentiate between one dimensional and two dimensional array.
Write down the importance of an array.
What is string? Explain any four string handling function with an example.
What do you mean by string manipulation? Explain about “strcpy()” and “strcat()”.
The End
4.7 Functions
Definition: A self –contained block of code that performs a particular task is called Function.
Advantages of functions:
1. It facilitates top down modular programming.
2. The length of a source program can be reduced by using functions at appropriate places.
3. It is easy to locate and isolate a faulty function for further investigations.
4. A function may be used by many other programs.
A function has 3 elements.
1. Function Declaration (Function Prototype)
2. Function Call
3. Function Definition
1. Function Declaration (Function Prototype)
A function declaration also known as Function Prototype consists of four parts:
Syntax:
Function_type Function_name(Formal Parameter list);
Example:
int sum(int a, int b);
or
int sum(int, int);
Points to note:
1. The parameter list must be separated by commas.
2. The parameter names do not need to be the same in the prototype declaration and the function definition.
3. The types must match the types of parameters in the function definition, in number and order.
4. Use of parameter names in the declaration is optional.
Function Parameter:
The parameters used in function declaration and function definition are called formal parameter and those used in function call is called actual parameter.
The formal and actual parameters must match exactly in type, order and number. Their names, however, do not need to match.
Examples: sum=add (a, b, c); // here a, b, c are called actual parameter.
int add(int x , int y, int z); // here x, y, z are formal parameter.
Function Return: Function can be organized into 2 types.
a) Function that has not a return value i.e. void function.
b) Function that has a returned value i.e. int function.
2. Function Call
A function can be called by simply using the function name followed by a list of actual parameters, if any, enclosed in parenthesis.
Static variables are created and initialized once, on the first call to the function. Subsequent calls to the function do not recreate or re-initialize the static variable.
Example:
c=sum (a,b);
3. Function Definition
The function definition is an independent program module that is specially written to implement the requirement of the function.
A function definition, also known as function implementation shall include the following elements.
a) Function type
b) Function Name
c) List of parameters
d) Local variable declarations
e) Function statements
f) A return statement
All the six elements are grouped into two parts, namely,
· Function Header(First three elements)
· Function Body ( Second three elements)
Syntax:
Function_type Function_Name(Formal Parameter list)
{
Local variable declaration;
Executable statement 1;
Executable statement 2;
………………….
………………….
return statement;
}
Note that a semicolon is not used at the end of the Function Header of Function definition.
Difference between Local Variable and Global variable [HSEB2068]
Local Variable | Global Variable |
1. Any variable declare within the specific function is called Local Variable. | 1. Any variable which is declared outside all the function is called Global Variable. |
2. Visibility: Visible inside the corresponding function only. | 2. Visibility: Visible throughout program. |
3. Accessible: Accessible to instruction of corresponding function. | 3. Accessible: Accessible to all the instruction to specified program. |
4. Life span: Created when control is transferred from calling to called function and destroyed when control returned to calling function. | 4. Life span: Life of program or until the program run. |
5. Duplicate is allowed. | 5. Global variable name should be unique. |
Difference between Library and User-defined Function [HSEB 2067]
Library Function | User –defined Function |
1. It is already defined functions. | 1. It is defined by the programmers as their need. |
2. It needs header file to use the functions. | 2. It requires the function declaration to use the functions. |
3. We can’t modify the library functions. | 3. We can modify the user –defined functions. |
4. It is called at run time. | 4. It is called at compile time. |
5. Example: printf( ); scanf ( ); etc. | 5. Example: sum( ); max( ); etc. |
1. WAP to find the sum of two numbers using function?[HSEB 2072-E,2073-D]
#include<stdio.h>
#include<conio.h>
int sum (int x, int y);
void main( )
{
int a,b,c;
printf("Enter any two numbers=");
scanf("%d%d",&a,&b);
c=sum (a,b);
printf("The sum of two numbers=%d",c);
getch( );
}
int sum (int x, int y)
{
int p;
p=x+y;
return p;
}
4.8 Structures and Unions
4.8.1
4.9 Pointers
jhdsbfjhdsbf
4.10 Working with Files
dkldfjgefjgk
source/references: computer science-II,buddha publication