Module5 Questions Answers
Module5 Questions Answers
in
3. Array uses subscripts or '[ ]' (square Structure uses the '.' (dot operator) to access
brackets) to access the elements the elements.
4. The size of an array is fixed The size of a structure is not fixed.
5. Traversing through and searching for Traversing through and searching for
elements in an array is quick and easy. elements in a structure is slow and complex.
6. An array is always stored in contiguous A structure may or may not be stored in
memory locations. contiguous memory locations.
7. Array elements are accessed by their Structure elements are accessed by their
index number using subscripts. names using dot operator.
8. Array is pointer as it points to the first Structure is not a pointer.
element of the collection.
9. Ex: int marks [5] = {20, 30, 40, 50, 60}; struct student
{
char name;
int mark;
float fees;
}
3. Define structure. How structure variables are declared & initialized? Give examples.
A structure is a user defined data type that can store related information (even of different
data types) together. It is similar to records and can be used to store information about an
entity.
Structure Declaration
A structure is declared using the keyword ‘struct’ followed by the structure name. All the
variables of the structure are declared within the structure. A structure type is generally
declared by using the following syntax:
struct struct-name
{
data_type var-name;
data_type var-name;
…………………………
};
Example: To define a structure for a student, then the related information would be:
roll_number, name, course, and fees. This structure can be declared as:
struct student
{
int roll_no;
char name[20];
char course[20];
float fees;
}
Initialization of Structures
Initializing a structure means assigning some constants to the members of the structure. A
structure can be initialized in the same way as other data types are initialized.
The initializers are enclosed in braces and are separated by commas. The general syntax to
initialize a structure variable is given as follows:
struct struct_name
{
data_type member_name1;
data_type member_name2;
data_type member_name3;
…………………………………
} struct_var {constant1, constant2, = constant 3, …..... };
Example: We can initialize s student structure by writing:
struct student
{
int roll_no;
char name[20];
char course[20];
float fees;
} stud1 = { 01, “RAJA”, “MCA”, 45000 };
4. Explain with suitable examples the how the members of a structure are accessed.
A structure member variable is generally accessed using a . (dot) operator. The syntax of
accessing a structure or a member of a structure can be given as follows:
struct_var . member_name
The dot operator is used to select a particular member of the structure.
For example, to assign value to the individual data members of the structure variable
stud1, we may write
stud1.roll_no = 01;
stud1.name = "Rahul";
stud1.course = "BCA";
stud1.fees = 45000;
To input values for data members of the structure variable stud1, we may write
scanf ( "%d", &stud1.r_no );
scanf ( "%s", stud1.name );
Similarly, to print the values of structure variable stud1, we may write
printf ( "%s", stud1.course );
printf ( "%f", stud1.fees );
Memory is allocated only when we declare variables of the structure.
Once the variables of a structure are defined, we can perform a few operations on them. For
example, we can use the assignment operator '=' to assign the values of one variable to
another.
5. List all string handling functions. Explain any four with syntax and examples.
The different string handling functions are:
strcat, strncat, strchr, strrchr, strcmp, strncmp, strcpy, strncpy, strle, strstr, strspn,
strcspn.
strcat Function strncat Function
Syntax: Syntax:
char *strcat ( char *str1, const char *str2 ); char *strncat ( char *str1, const char *str2,
size_n );
The strcat function appends the string The strncat function appends the string
pointed to by str2 to the end of the string pointed to by str2 to the end of the string
pointed to by str1. The terminating null pointed to by str1 up to n characters long.
character of str1 is overwritten. The process The terminating null character of str1 is
stops when the terminating null character of overwritten. Copying stops when n
str2 is copied. The argument str1 is characters are copied or the terminating null
returned. character of str2 is copied.
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
int main ( ) int main ( )
{ {
char str1[50] = "Programming"; char str1[50] = "Programming";
char str2[ ] = "In C"; char str2[ ] = "In C";
strcat ( str1, str2 ); strncat ( str1, str2, 2 );
printf ( "Resulting string is: %s", str1 ); printf ( "Resulting string is: %s", str1 );
return 0; return 0;
} }
Output: Output:
Resulting string is: Programming In C Resulting string is: Programming In
return 0; return 0;
} }
Output: Output:
Two strings are not identical Two strings are identical
enough to store the contents of str2. copied to str1 until n characters are copied.
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
int main() int main()
{ {
char str1[10], str2[10]= "HELLO"; char str1[10], char str2[10]= "HELLO";
strcpy ( str1, str2 ); strncpy ( str1, str2, 3 );
printf ( “ Resulting string is: %s”, str1); printf ( “ Resulting string is: %s”, str1);
return 0; return 0;
} }
Output: Output:
Resulting string is: HELLO Resulting string is: HEL
temp = *b;
b = *a;
*a = temp;
printf ( "After Swapping: x = %d \n y = %d\n", x, y);
return 0;
}
8. Develop a C program to concatenate two strings without using built-in function.
#include <stdio.h>
int main ( )
{
char str1[100], str2[100], str3[100];
int i = 0, j = 0;
printf ( "Enter the first string: " );
gets ( str1 );
printf ( "Enter the second string: " );
gets ( str2 );
while (str1[1] != '\0' )
{
str3[ j ] = str1[ i ];
i++;
j++;
}
i=0;
while ( str2[ i ] != '\0 ')
{
str3[ j ] = str2[ i ];
i++;
j++;
}
str3[ j ] = '\0';
printf ( "The concatenated string is: " );
puts( str3 );
return 0;
}
9. Write a program to append (copy) a string to another string without using built-in
function.
#include <stdio.h>
int main ( )
MOHAMMED SALEEM | Asst. Prof., Dept. of E & C, PACE 7
[INTRODUCTION TO C PROGRAMMING - MODULE 5 [email protected]
{
char dest[100], source[50];
int i = 0, j = 0;
printf ( "Enter the source string: " );
gets ( source );
printf ( "Enter the destination string: " );
gets ( dest );
while ( dest[i] != '\0' )
i++;
while ( source[j] != '\0' )
{
dest[ i ] = source[ j ];
i++;
j++;
}
dest[ i ] = '\0';
printf ( "After appending, the destination string is: " );
puts( dest );
return 0;
}
10. Write a program to reverse the given string.
#include <stdio.h>
int main ( )
{
{
char str[100], temp;
int i = 0, j = 0;
printf ( "Enter the string: " );
gets ( source );
j = strlen(str) – 1;
while ( i < j )
{
temp = str[ j ];
str[ j ] = str[ i ];
str[ i ] = temp;
i++;
j--;
}
printf ( "The reversed string is: " );
puts( str );
return 0;
}
11. Write a C program to read a sentence & count the number of words in the
sentence.
#include <stdio.h>
int main ( )
{
char str[200];
int i = 0, count = 0;
printf ( "Enter the sentence: ");
gets( str );
while ( str[ i ] != '\0')
{
if ( str[ i ] == ‘ ‘ && str[ i+1 ] != ‘ ‘ )
count++;
i++;
}
printf ( "The total count of words is: %d", count+1);
return 0;
}
12. Write a C program to implement structure to read, write and compute average
marks and the students scoring above and below the average marks for a class of N
students.
#include<stdio.h>
struct student
{
char usn[10];
char name[10];
float m1, m2, m3;
float avg, total;
};
void main( )
{
struct student s[20];
int n, i;
printf ( "Enter the number of students:" );
}
sum = 0;
for ( i=0; i<n; i++ )
{
sum = sum + *(x+i);
}
printf ( "sum = %f\n", sum );
mean = sum/n;
sum = 0;
for ( i=0; i<n; i++ )
{
sum = sum + (*(x+i)-mean) * (*(x+i)-mean);
}
variance = sum/n;
deviation = sqrt( variance );
printf ( "mean(Average) = %f \n", mean);
printf ( "variance = %f \n", variance );
printf ( "standard deviation = %f\n", deviation );
}
14. Write a program to read and print the names of n students of a class.
#include <stdio.h>
int main ( )
{
char names[5][15];
int i, n;
printf ("Enter the number of students: \n" );
scanf ( "%d", &n );
for ( i=0; i<n; i++ )
{
printf ( “Enter the name of student %d:, i+1);
gets ( names[i] );
}
printf ( “The names of students are: \n”);
for ( i=0; i<n; i++ )
puts ( names[i] );
return 0;
}