Chapter 4
Chapter 4
CHAPTER 4
Array & String
Q.1 Write a C program to read 10 numbers from user and store them in an array.
Display Sum, Minimum and Average of the numbers.(PPS_WIN2019)
#include <stdio.h>
int main()
{
int i, num;
float total = 0.0, average;
printf ("Enter the value of N \n");
scanf("%d", &num);
int array[num];
/* Summation starts */
CKPCET, SURAT 1
PPS (3110003)
Q.2 Why null value is used in string? Justify your answer with example (PPS_WIN_18)
Because C uses null-terminated strings, which means that the end of any string is marked by the
ASCII value 0 (the null character), which is also represented in C as '\0'.In C, null value is used to
terminate strings so that text based functions can automatically find out end of string and dont have
to be supplied with additional parameter for string size.
Q.3 Describe following string functions in C – Language.
(1) strcpy( ) , (2) strcat( ) , (3) strlen( ) , (4) strcmp ( ) .(CPU_WIN_2019) (CPU_WIN_2017)
(CPU_WIN_2014)(CPU_SUM_2018)
1) strcpy()
This is the string copy function. It copies one string into another string.
Syntax:
strcpy(string1, string2);
The two parameters to the function, string1 and string2, are strings. The function will copy
the
string string1 into the string 1.
2) strcat()
3) strlen()
This is the string length function. It returns the length of the string passed to it as the
argument.
Syntax:
strnlen(string1)
The parameter string1 is the name of the string whose length is to be determined. The above
function will return the length of the string string1.
4) strcmp()
CKPCET, SURAT 2
PPS (3110003)
Q.4 Write a program to copy one string into another and count the number of characters
copied. .(CPU_WIN_2019)
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void main()
{
char str1[100], str2[100];
int i;
printf("\n\nCopy one string into another string :\n");
printf("-----------------------------------------\n");
printf("Input the string : ");
fgets(str1, sizeof str1, stdin);
/* Copies string1 to string2 character by character */
i=0;
while(str1[i]!='\0')
{
str2[i] = str1[i];
i++;
}
//Makes sure that the string is NULL terminated
str2[i] = '\0';
printf("\nThe First string is : %s\n", str1);
printf("The Second string is : %s\n", str2);
printf("Number of characters copied : %d\n\n", i);
}
CKPCET, SURAT 3
PPS (3110003)
Output:
Copy one string into another string :
-----------------------------------------
Input the string : This is a string to be copied.
CKPCET, SURAT 4
PPS (3110003)
CKPCET, SURAT 5
PPS (3110003)
Q. 7 What is string? Explain with example to find a character from string. (PPS_WIN_18)
A string is a collection of characters, stored in an array followed by null ('\0') character.
#include <stdio.h>
#include<string.h>
int main(void) {
char str[50],c;
int temp=0,i;
printf("\nEnter the string : ");
scanf("%s",str);
printf("\nEnter the char to find : ");
scanf(" %c",&c);
for(i=0;i<strlen(str)&&str[i]!=c;++i);
if(i<strlen(str))
printf("\nCharacter found,pos : %d",i);
else
printf("\nCharacter not found");
return 0;
}
Q.8 What is Array in C language? How is it different from Structure in C language? State
advantages and disadvantages of Array. .(CPU_WIN_2017)
Arrays Structures
Advantages
• It is better and convenient way of storing the data of same datatype with same size.
• It allows us to store known number of elements in it.
CKPCET, SURAT 6
PPS (3110003)
• It allows to store the elements in any dimensional array – supports multidimensional array.
Disadvantages
• It allows us to enter only fixed number of elements into it. We cannot alter the size of the
array once array is declared. Hence if we need to insert more number of records than
declared then it is not possible. We should know array size at the compile time itself.
• Inserting and deleting the records from the array would be costly since we add / delete the
elements from the array, we need to manage memory space too.
Q.9 Write a program to check whether a given string is palindrome or not.
.(CPU_WIN_2017)
#include <stdio.h>
#include <string.h>
int main(){
char string1[20];
int i, length;
int flag = 0;
printf("Enter a string:");
scanf("%s", string1);
length = strlen(string1);
for(i=0;i < length ;i++){
if(string1[i] != string1[length-i-1]){
flag = 1;
break;
}
}
if (flag) {
printf("%s is not a palindrome", string1);
}
else {
printf("%s is a palindrome", string1);
}
CKPCET, SURAT 7
PPS (3110003)
return 0;
}
OUTPUT:
Enter a string: wow
wow is a palindrome
Q.10 Discuss initialization of one-dimensional arrays with example(CPU_WIN_2016)
There are four different ways to initialize one-dimensional array in c programming.
1. Initialize array at the time of declaration
One way is to initialize one-dimentional array is to initialize it at the time of declaration. You can
use this syntax to declare an array at the time of initialization.
int a[5] = {10, 20, 30, 40, 50};
2. Initialize all elements of an array with 0 (zero)
If you don’t want to get all elements initialized with garbage, you can initialize them with value 0
(zero). You can do this with the help of this syntax.
int a[5] = {0};
3. Initialize to define the size of an array
it is also possible to define the size of an array by initializing elements of the array. You can use
this syntax for this.
4. Initialize array elements individually
Array is a group of variables, each variable is called element of the array. You can initialize all
elements separately as you initialize any other variable. See following syntax.
int a[5];
a[0] = 10;
a[1] = 20;
a[2] = 30;
a[3] = 40;
a[4] = 50;
Q.11 Write a program to count total words in text. (CPU_WIN_2016)
#include <stdio.h>
#include <string.h>
CKPCET, SURAT 8
PPS (3110003)
void main()
{
char s[200];
int count = 0, i;
printf("Enter the string:\n");
scanf("%[^\n]s", s);
for (i = 0;s[i] != '\0';i++)
{
if (s[i] == ' ' && s[i+1] != ' ')
count++;
}
printf("Number of words in given string are: %d\n", count + 1);
}
OUTPUT:
Enter the string:
hello world
Number of words in given string are: 2
Q.12 Write a program in c for multiplication of two matrix. (CPU_WIN_2015)
#include<stdio.h>
#include<stdlib.h>
int main(){
int a[10][10],b[10][10],mul[10][10],r,c,i,j,k;
system("cls");
printf("enter the number of row=");
scanf("%d",&r);
printf("enter the number of column=");
scanf("%d",&c);
printf("enter the first matrix element=\n");
CKPCET, SURAT 9
PPS (3110003)
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("enter the second matrix element=\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
scanf("%d",&b[i][j]);
}
}
printf("multiply of the matrix=\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
mul[i][j]=0;
for(k=0;k<c;k++)
{
mul[i][j]+=a[i][k]*b[k][j];
}
}
}
//for printing result
CKPCET, SURAT 10
PPS (3110003)
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
printf("%d\t",mul[i][j]);
}
printf("\n");
}
return 0;
}
Output:
enter the number of row=3
enter the number of column=3
enter the first matrix element=
111
222
333
enter the second matrix element=
111
222
333
multiply of the matrix=
666
12 12 12
18 18 18
Q.13 Write a program to reverse the input string. (CPU_WIN_2013)
#include <stdio.h>
#include <string.h>
CKPCET, SURAT 11
PPS (3110003)
int main()
{
char s[100];
printf("Enter a string to reverse\n");
gets(s);
strrev(s);
printf("Reverse of the string: %s\n", s);
return 0;
}
OUTPUT
Enter a string to reverse
Computer
Reverse of the string:
retupmoc
Q.14 What is string? Write a program to concatenate two strings without using built in
function. (CPU_WIN_2014)
A string is a sequence of characters terminated with a null character \0 .
#include <stdio.h>
int main()
{
// Get the two Strings to be concatenated
char str1[100] = "Hello", str2[100] = "World";
// Declare a new Strings
// to store the concatenated String
char str3[100];
int i = 0, j = 0;
printf("\nFirst string: %s", str1);
printf("\nSecond string: %s", str2);
CKPCET, SURAT 12
PPS (3110003)
CKPCET, SURAT 13
PPS (3110003)
strcpy(string1, string2);
The two parameters to the function, string1 and string2, are strings. The function will copy the
string string1 into the string 1.
2) strcat()
This is the string concatenate function. It concatenates strings.
Syntax:
strcat(string1, string2);
The two parameters to the function, string1 and string2 are the strings to be concatenated. The
above function will concatenate the string string2 to the end of the string string1.
3) strlen()
This is the string length function. It returns the length of the string passed to it as the argument.
Syntax:
strnlen(string1)
The parameter string1 is the name of the string whose length is to be determined. The above
function will return the length of the string string1.
4) strcmp()
This is the string compare function. It is used for string comparison.
Syntax:
strcmp(string1, string2);
The above function will return 0 if strings string1 and string2 are similar, less than 0 if
string1<string2 and greater than 0 if string1>string2.
CKPCET, SURAT 14
PPS (3110003)
CKPCET, SURAT 15
PPS (3110003)
Q. 17 What is string? How they declare and also define null character. (CPU_SUM_2015)
Strings are defined as an array of characters. The difference between a character array and a string
is the string is terminated with a special character '\0'.
CKPCET, SURAT 16
PPS (3110003)
Declaration of strings: Declaring a string is as simple as declaring a one dimensional array. Below
is the basic syntax for declaring a string.
char str_name[size];
In the above syntax str_name is any name given to the string variable and size is used define the
length of the string, i.e the number of characters strings will store. Please keep in mind that there
is an extra terminating character which is the Null character (‘\0’) used to indicate termination of
string which differs strings from normal character arrays.
Null Character: Null character is a character that has all its bits set to 0. A null character, therefore,
has a numeric value of 0, but it has a special meaning when interpreted as text. In some
programming languages, notably C, a null character is used to mark the end of a character string.
Q. 18 Write a program using pointer and function to determine the length of string
(CPU_SUM_2015)
#include<stdio.h>
int main() {
char str[20], *pt;
int i = 0;
printf("Pointer Example Program : Find or Calculate Length of String \n");
printf("Enter Any string [below 20 chars] : ");
gets(str);
pt = str;
while (*pt != '\0') {
i++;
pt++;
}
printf("Length of String : %d", i);
return 0;
}
Output:
Pointer Example Program : Find or Calculate Length of String
Enter Any string [below 20 chars] : FIND-STRING-LENGTH
Length of String : 18
CKPCET, SURAT 17
PPS (3110003)
strcpy()
This is the string copy function. It copies one string into another string.
Syntax:
strcpy(string1, string2);
The two parameters to the function, string1 and string2, are strings. The function will copy the
string string1 into the string 1.
strcat()
Syntax:
strcat(string1, string2);
The two parameters to the function, string1 and string2 are the strings to be concatenated. The
above function will concatenate the string string2 to the end of the string string1.
strlen()
This is the string length function. It returns the length of the string passed to it as the argument.
Syntax:
strnlen(string1)
The parameter string1 is the name of the string whose length is to be determined. The above
function will return the length of the string string1.
Example:
#include <iostream>
#include <cstring>
int main() {
CKPCET, SURAT 18
PPS (3110003)
char name3[10];
int len;
strcpy(name3, name1);
cout << "strcpy( name3, name1) : " << name3 << endl;
strcat(name1, name2);
cout << "strcat( name1, name2): " << name1 << endl;
len = strlen(name1);
return 0;
Output:
Strcat(name1,name2): Guru99John
Strlen(name1): 10
Arrays a kind of data structure that can store a fixed-size sequential collection of elements of the
same type. An array is used to store a collection of data, but it is often more useful to think of an
array as a collection of variables of the same type.
Instead of declaring individual variables, such as number0, number1, ..., and number99, you
declare one array variable such as numbers and use numbers[0], numbers[1], and ...,
CKPCET, SURAT 19
PPS (3110003)
All arrays consist of contiguous memory locations. The lowest address corresponds to the first
element and the highest address to the last element.
#include <stdio.h>
int main () {
int i,j;
return 0
Output:
Element[0] = 100
Element[1] = 101
CKPCET, SURAT 20
PPS (3110003)
Element[2] = 102
Element[3] = 103
Element[4] = 104
Element[5] = 105
Element[6] = 106
Element[7] = 107
Element[8] = 108
Element[9] = 109
Advantages of Array:
Q. 21 What is string? In how many ways can you accept data in a string? (CPU_SUM_2017)
In C programming, a string is a sequence of characters terminated with a null character \0. For
example:
When the compiler encounters a sequence of characters enclosed in the double quotation marks, it
appends a null character \0 at the end by default.
CKPCET, SURAT 21
PPS (3110003)
Remember that when you initialize a character array by listing all of its characters separately then
you must supply the '\0' character explicitly.
char str[4];
Q 22 Write a program to accept a string and count the number of vowels present in a string.
(CPU_SUM_2017)
#include <stdio.h>
#include <conio.h>
void main()
char a[100];
int len,i,vow=0;
clrscr();
gets(a);
len=strlen(a);
for(i=0;i<len;i++)
vow=vow+1;
CKPCET, SURAT 22
PPS (3110003)
getch();
Output:
#include <stdio.h>
int main() {
int i, n;
float arr[100];
scanf("%d", &n);
scanf("%f", &arr[i]);
arr[0] = arr[i];
CKPCET, SURAT 23
PPS (3110003)
return 0;
Output:
CKPCET, SURAT 24
PPS (3110003)
CKPCET, SURAT 25
PPS (3110003)
Q. 25 Write a program in ‘C’ to print the following pattern using loop statement.
(CPU_SUM_2018)
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
#include <stdio.h>
void main()
{
int i,j,rows;
CKPCET, SURAT 26
PPS (3110003)
4444
55555
int main()
{
char Str1[100];
int i;
printf("\n Please Enter a String that you want to Convert into Uppercase : ");
gets(Str1);
return 0;
CKPCET, SURAT 27
PPS (3110003)
#include <stdio.h>
int main()
{
/* Here we are taking a char array of size
* 100 which means this array can hold a string
* of 100 chars. You can change this as per requirement
*/
char str[100],i;
printf("Enter a string: \n");
scanf("%s",str);
return 0;
}
Find string length without using strlen() function.
#include <stdio.h>
int main()
{
/* Here we are taking a char array of size
* 100 which means this array can hold a string
* of 100 chars. You can change this as per requirement
*/
CKPCET, SURAT 28
PPS (3110003)
char str[100],i;
printf("Enter a string: \n");
scanf("%s",str);
return 0;
}
Q. 27 What is an array? Explain one dimensional and two dimensional array declarations
and initialization with suitable example. (PPS_SUM_2019)
Arrays a kind of data structure that can store a fixed-size sequential collection of elements of the
same type. An array is used to store a collection of data .
To declare an array in C, a programmer specifies the type of the elements and the number of
elements required by an array as follows −
type arrayName [ arraySize ];
One-dimensional array :
Conceptually you can think of a one-dimensional array as a row, where elements are stored one
after another.
Syntax: datatype array_name[size];
Example :
int num[100];
float temp[20];
char ch[50];
program :
#include<stdio.h>
int main()
{
int arr[5], i;
CKPCET, SURAT 29
PPS (3110003)
2-D array
The syntax declaration of 2-D array is not much different from 1-D array. In 2-D array, to declare
and access elements of a 2-D array we use 2 subscripts instead of 1.
int main()
{
int arr[ROW][COL], i, j;
for(i = 0; i < ROW; i++)
CKPCET, SURAT 30
PPS (3110003)
{
for(j = 0; j < COL; j++)
{
printf("Enter arr[%d][%d]: ", i, j);
scanf("%d", &arr[i][j]);
}
}
printf("\nEntered 2-D array is: \n\n");
for(i = 0; i < ROW; i++)
{
for(j = 0; j < COL; j++)
{
printf("%3d ", arr[i][j] );
}
printf("\n");
}
// signal to operating system everything works fine
return 0;
}
MCQ
(CPU_WIN_2019)
Q. int x[5] = {3,4,5}
What will be the value of x[4]?
(a) 0 (b) 4 (c) 5 (d) 3
CKPCET, SURAT 31
PPS (3110003)
CPU_WIN_2013
Q. Which of the following function is more appropriate for reading in a multiword string?
(A) printf(); (B) scanf(); (C) gets(); (D) puts();
CPU_WIN_2014
Q. Which of the following is used as a string termination character?
(a) 0 (b) \0 (c) /0 (d) None of these
CPU_SUM_2016
Q. Array index start at (CPU_SUM_2018)
(a) 1 (b) User Defined (c) 0 (d) None of above
CPU_SUM_2017
Q. The format string to accept a string is (CPU_SUM_2018)
(a)%c (b)%d (c)%f (d)%s
CKPCET, SURAT 32