UNIT 3 (C Programming)-1
UNIT 3 (C Programming)-1
CHAPTER – 3
Derived data types in C
Arrays
A normal variable can hold only one value. Suppose we may need to process a group of data
items that are having the same data type such as integer, float etc. in such situations we need one
special data structure that is “arrays”.
Definition:
Arrays
dimensional
Array Initialization:
Where
10 20 30 40 50
Example programs
/* program to accept n integers and store them in an array and also print them */
void main()
{
int a[10], i ,n ;
printf(“\n enter the size of an array”);
scanf(“%d”,&n);
printf ( “ \n enter elements to an array”);
for ( i=0; i<n ; i++)
scanf ( “%d” , &a[i]);
printf ( “ \n elements of an array are”);
for ( i=0; i<n ; i++)
printf ( “%d” , a[i]);
}
/* program to accept 2 integer arrays and find sum of the corresponding elements in the
array */
void main()
{
int a[10],b[10,c[10, i ,n,sum[10] ;
printf(“\n enter the size of an array”);
scanf(“%d”,&n);
printf ( “ \n enter elements to the first array”);
for ( i=0; i<n ; i++)
scanf ( “%d” , &a[i]);
printf ( “ \n enter elements to the second array”);
for ( i=0; i<n ; i++)
scanf ( “%d” , &b[i]);
for(i=0 ; i<n ; i++ )
sum[i] = a[i] + b[i] ;
printf ( “ \n sum elements of an array are”);
for ( i=0; i<n ; i++)
printf ( “%d” , sum[i]);
}
This array can be consisting of 2 subscripts. The simplest and most commonly used
arraysare 2 dimensional arrays.
This array required row size and column size. The declaration of 2D array will be
Syntax:
Syntax:
Initialization of 2D array
The list of values assigned to array elements in order.
Multiplication of matrices
Suppose the order of 1 st matrix is mxn,
The matrix multiplication is possible only, when the columns of 1 st matrix must be equals to the
rows of 2nd matrix.
m x n = p x q
equal
void main()
{
int a[10][10], b[10][10], mul[10][10], i, j ,k,n;
clrscr();
printf(“\n enter the order of matrix”);
scanf(“%d”,&n);
printf ( “ \n enter elements to the first array”);
for ( i=0; i<n ; i++)
for (j=0; j<n ; j++)
scanf ( “%d” , &a[i][j]);
printf ( “ \n enter elements to the second array”);
for ( i=0; i<n ; i++)
for (j=0; j<n ; j++)
scanf ( “%d” , &b[i][j]);
for ( i=0; i<n ; i++)
for (j=0; j<n ; j++)
{
mul[i][j]=0;
for(k=0;k<n;k++)
mul[i][j] = mul[i][j] + a[i][k] * b[k][j];
}
getch();
}
STRINGS
STRINGS:
It is a packed character array, which is terminated by NULL character. In the declaration statement,
character array and strings are one and the same. To differentiate character array and string is only after
inputting the data. A character array, which is end with a NULL character is called string otherwise is a
character array.
DECLARATION:
Where, Str is the user defined string variable name, size indicates the number of character accommodated in
the string.
Reserves a block of 20 bytes to the name, each elements can holds one character and its contents can
accessed by its subscripts.
Allocation
String initialization :
String can be initialized by two methods, character constant and string constant.
i. character constant :
In this method user can specified NULL character ate the end of the string.
scanf ( ) function
gets ( ) function
getchar ( ) function
scanf ( ) function:
The function scanf with %s format specifier is needed to read the string from the
terminal.
In this & symbol is not required for reading variables. Because array itself allocates address for
that it cannot have & symbol.
B A N G A L O R E \0
Ex: suppose if we input “NEW DELHI”, then storage looks like
N E W \0 ? ? ? ? ? ?
The function scanf ( ) has a drawback it just terminates as soon as it finds blank space.
In the above example scanf reads only new but not delhi because after the word new one blank
space is appeared. Then scanf understand the string will be ended.
void main()
{
char str[30];
scanf(“%s”, str);
getch(); }
gets ( ) function:
this unformatted input function. Using this we can read one string at a time.
This is the most convenient method of reading a string . this is a library
function and it isavailable in the <stdio.h> header file.
It reads characters from the keyboard until a new line character is encountered and
then appendsa null character to the string automatically.
gets ( str );
void main()
{
char str[30];
gets (str);
getch();
getchar ( ) function:
This function is unformatted input function. It read only one character at a time.
Ex:
void main()
{
char str[40], ch;
int i=0;
printf(“\n enter a string “);
printf ( ) function
puts ( ) function
putchar ( ) function
printf ( ) function:
The function printf with %s format specifier is needed to print the string on the
terminal.
puts ( ) function:
this unformatted output function. Using this we can print one string at a time.
puts ( str );
gets(str);
puts(str);
getch();
}
putchar ( ) function:
This function is unformatted output function. It print only one character at a
time. We canuse this function repeatedly to output a string of characters stored in
an array using a loop.
Ex:
void main( )
{
char str[8]= {“skyward”};
int i=0;
for(i=0;i<7;i++)
putchar(str[i]);
putchar(“\n”);
String operations: C-Library supports a large number of string-handling that can be used to
carry out may of the string manipulations such as,
These are the built in functions which are used to manipulate the null terminated strings.
A R MAHESHA HOD Dept. of Computer Science, GFGC Tumakuru Page 11
Programming in C I Sem BCA
Note: To use all the string functions in C you must include string.h library header file in the
program.
1. strlen () function: This function counts and returns the number of characters in a string. The
length does not include the NULL character.
Where n is integer variables which receive the value from the strlen function
2. strcpy () function: This function is used to make the duplicate copy of existing string.
Note: C language does not allow you to assign the string to another string directly by
assignment operator.
3. strcat () function : This function used to combine two strings into single string. In
other words add the content of second string to the end of the first string. It is also
called as merge two strings.
Syntax: strcat(str1,str2);
strcpy(str2, “Karnataka”);
4. strcmp () function : This function is used to compare two strings with case sensitive, which
returns an integer. If the integer value is equal to zero, indicate both the strings are equal. If value
is +ve , first string is greater than second string. If value is –ve, first string is smaller then second
string.
6. strcmpi () function: This function is same as strcmp, which compares two strings but not case
sensitive, which returns an integer. If the integer value is equal to zero, indicate both the strings
are equal. If value is +ve , first string is greater than second string. If value is –ve, first string is
smaller then second string.
7. strlwr () function : This function converts all alphabets in a string from uppercase to lower
case.
Syntax: strlwr(str);
8. strupr () function : This function converts all alphabets in a string from lower case to upper
case
Syntax: strupr(str);
Syntax: strrev(str);
isalnum(‘B’); 1
toascii(‘2’); 50
#include<stdio.h>
void main()
char str1[20],str2[20],str3[20];
int length;
clrscr();
gets(str1);
puts(str1);
gets(str2);
puts(str2);
length=strlen(str1);
strlwr(str1);
strupr(str2);
strcpy(str1,str2);
getch();
#include <stdio.h>
#include <string.h>
int main()
{
char a[100], b[50];
strcat(a,b);
return 0;
}
#include<stdio.h>
#include<string.h>
int main() {
int i, j = 0;
gets(str);
i = 0;
j = strlen(str) - 1;
while (i < j) {
temp = str[i];
str[i] = str[j];
str[j] = temp;
i++;
j--;
return (0);
#include<stdio.h>
void main()
char str1[20],str2[20];
int res;
gets(str1);
gets(str2);
res=strcmp(str1,str2);
if(res==0)
else if(res<0)
else
getch();
Example 5 Program to find the length of string without using built in function
#include<stdio.h>
void main()
char str[20];
int i,len=0;
clrscr();
gets(str);
for(i=0;str[i]!='\0';i++)
len++;
getch();
//to count the number of upper and lower case characters in a string
#include<stdio.h>
void main()
char str[20];
int i,uppercount=0,lowercount=0;
clrscr();
printf("enter a sytring\n");
gets(str);
for(i=0;str[i]!='\0';i++)
if(str[i]>='A'&&str[i]<='Z')
uppercount++;
if(str[i]>='a'&&str[i]<='z')
lowercount++;
getch();
#include<stdio.h>
#include<conio.h>
void main()
char str1[20],str2[20];
clrscr();
printf("enter a string\n");
gets(str1);
strcpy(str2,str1);
strrev(str2);
if(strcmp(str1,str2)==0)
else
getch();
Questions:
10. Write a c-program to convert all lower case letters to upper case letters and
vice versa in a given string.
12. Write a program to find length of a given string without using built-in function.
13. What is the syntax of string concatenation.