C PROGRAMMING(VSEC102)
Assignment No.-4
Q. 1)Define array. List its types
ANS)
Array is a collection of similar datatype elements stored at contiguous memory locations.
Arrays are the derived data type in C programming language which can store the primitive type of data such as int, char,
double, float, etc
Types of Array:
a)One Dimensional
one variable name using only one subscript is called as one dimensional array.
Example:
int number[5];
Initializing an array while declaration
int a[5] = {10, 20, 30, 40, 50};
Accessing Array Elements
To access array elements you have to use index or subscript and array name.
Example:
marks[5] = 30; /*will store value 30 into 6th element*/
b)Two dimensional array
Two dimensional array is a collection of similar type of data elements arranged in the form of rows & columns.
Example:
int arr[3][3];
c)Multi-dimensional array
An array with more than one dimension is called as multidimensional array.
Example
int arr [3][4];
float arr1[2][4][3];
Q.2)Difference between char array and integer array
ANS)
Q.3)Define structure. Give one example of structure declaration.
ANS)
Structure is a collection of variables of similar or different data types which is represented by a single name.
It is a user-defined data type Members of structure are accessed using the dot operator (.) and structure variable
Example:
struct bill
{
int consumer_id;//structure member
char address[50];//structure member
float amount;//structure member
}b; //b is a structure variable
To access amount of structure bill we will write
b.amount=100;
Q.4)Explain how strings are represented in C
ANS)
In C, strings are represented as arrays of characters terminated by a null character (\0). The null character marks the end
of the string, allowing functions like strlen() and printf() to determine where the string ends.
Strings can be manipulated using standard library functions, such as strcpy(), strcat(), and strlen().
Example:
char text[9] = "WELCOME";
char text[9] = {'W', 'E', 'L', 'C', 'O', 'M', 'E'};
char text[9] = {'W', 'E', 'L', 'C', 'O', 'M', 'E', '\0'};
char name[15]=“JOHNSONP”;
Q. 5)Write a C program to count the total number of vowels and consonants in a string
ANS)
#include<stdio.h>
#include<ctype.h>
int main()
{
char str[100];
int vowels=0,consonants=0,i=0;
printf("Enter a string:");
gets(str);
while(str[i]!='\0')
{
char ch=tolower(str[i]);
if(ch>='a'&&ch<='z')
{
if(ch=='a' || ch=='e' || ch=='i' || ch=='o' || ch=='u')
{
vowels++;
}
else
{
consonants++;
}
}
i++;
}
printf("Total vowels: %d\n",vowels);
printf("Total consonants: %d\n",consonants);
}
Output
Enter a string:sheikh kashif ahmed
Total vowels: 6
Total consonants: 11