Professional Documents
Culture Documents
1. Array is a static data structure ie. Memory is allocated during comiplation time. Once
memory is allocated during comiplation time it can’t be changed during execution
time.
2. We must know in advance regarding size of the array.
3. An array contains elements of the same data type i.e. array can’t stores different data
type elements.
4. Array elements are stored in adjacent memory locations. So insertion and deletion
operations are time consuming because we need to shift the elements one position to
the left or right repectively.
5. If we declare size of the array as ‘n’ then we can access elements from 0 to n-1. If we
try to access nth element or (n+1)th element then compiler doesn’t displays error
message. The process of checking limits of the array is known as “Bound Checking”
and C language doesn’t performs bound checking.
Applicatios of Arrays:-
Strings
A string can be defined as a collection of characters enclosed in double quotes.
char array_name[size];
For array_name identifier rules are to be followed and array_name is nothing but string.
Inititalization of a string:-
2
When the string is initialized then size of the array is equals to number of characters + one(1).
char a[10]=”hai”;
1000 1001 1002 1003 1004 1005 1006 1007 1008 1009
H a i \0
1.scanf( )
2.gets( )
Scanf(“%s”,&a);
Drawbacks of scanf( ):- scanf( ) can’t reads blank space character. For example let string is “
ram babu” then scanf( ) reads only first word i.e. ram.
2.gets( ):- It is useful to read a line of text . gets( ) reads blank space character also.
gets(a);
1.printf( )
2.puts( )
printf(“%s”,a);
3
Drawbacks of printf( ):- After printing string then the cursor will be at its next location only.
If we want to print string in new line then we must use \n character.
2.puts( ):- It is useful to display a line of text . After displaying the string then the cursor will
be placed in the new line.
puts(a);
To manipulate the strings we uses string handling functions and these functions are available
in the header file called “string.h”.
1.strlen( )
2.strcpy( )
3.strcat( )
4.strrev( )
5.strcmp( )
6.strlwr( )
7.strupr( )
1.strlen( ):- It is used to calculate length of a string. It returns an integer value which
specifies length of the string.
Syntax:-
#include<stdio.h>
#include<coio.h>
#include<string.h>
int main()
{
4
char a[10];
int len;
clrscr();
printf("\nEnter a string\n");
gets(a);
len=strlen(a);
return 0;
Result:-
Input:-
Enter a string
Ramu
Output:-
length of string is 4
#include<stdio.h>
#include<conio.h>
int main()
char a[10];
int i,len=0;
clrscr();
printf("\nEnter a string\n");
gets(a);
for(i=0;a[i]!='\0';i++)
len++;
5
return 0;
Result:-
Input:-
Enter a string
Ramu
Output:-
length of string is 4
Syntax:- strcpy(destination,source);
#include<stdio.h>
#include<conio.h>
#include<string.h>
int main()
char a[10],b[10];
clrscr();
printf("\nEnter a string\n");
gets(a);
strcpy(b,a);
return 0;
}
6
Result:-
Input:-
Enter a string
Ramu
Output:-
#include<stdio.h>
#include<conio.h>
int main()
char a[10],b[10];
int i;
clrscr();
printf("\nEnter a string\n");
gets(a);
for(i=0;a[i]!='\0';i++)
b[i]=a[i];
b[i]='\0';
return 0;
Result:-
Input:-
Enter a string
Ramu
Output:-
7
Syntax:- strcat(s1,s2);
When the above function is executed then content of string s2 will be appended at the end of
string s1.
#include<stdio.h>
#include<conio.h>
int main()
char a[10],b[10];
int i;
clrscr();
gets(a);
gets(b);
strcat(a,b);
return 0;
Result:-
Input:-
Ram
Babu
8
Output:-
#include<stdio.h>
#include<conio.h>
#include<string.h>
int main()
char a[10],b[10];
int i,j;
clrscr();
gets(a);
gets(b);
for(i=strlen(a),j=0;b[j]!='\0';i++,j++)
a[i]=b[j];
a[i]='\0';
return 0;
Result:-
Input:-
Ram
Babu
9
Output:-
Syntax:-
#include<stdio.h>
#include<conio.h>
#include<string.h>
int main()
char a[10];
clrscr();
printf("\nEnter a string\n");
gets(a);
strrev(a);
return 0;
Result:-
Input:-
Enter a string
ramu
Output:-
#include<stdio.h>
10
#include<conio.h>
int main()
char a[10];
int i,len=0;
clrscr();
printf("\nEnter a string\n");
gets(a);
for(i=0;a[i]!='\0';i++)
len++;
for(i=len-1;i>=0;i--)
printf("%c",a[i]);
return 0;
Result:-
Input:-
Enter a string
ramu
Output:-
umar
5.strcmp( ):-
#include<stdio.h>
11
#include<conio.h>
#include<string.h>
int main()
char a[10],b[10];
int i;
clrscr();
gets(a);
gets(b);
i=strcmp(a,b);
if(i==0)
else
return 0;
Result:-
Input:-
Ram
Ram
Output:-
#include<stdio.h>
#include<conio.h>
#include<string.h>
int main()
char a[10],b[10];
int i;
clrscr();
gets(a);
gets(b);
i=0;
i++;
else
return 0;
Result:-
Input:-
Ram
Babu
13
Output:-
6.strlwr( ):- It is useful to convert upper case characters of the string to lower case
characters.
Syntax:- strlwr(str);
Ex1:-C program to convert upper case characters to lower case characters using library
function:-
#include<stdio.h>
#include<conio.h>
#include<string.h>
int main()
char a[10];
int i,j;
clrscr();
printf("\nEnter a string\n");
gets(a);
strlwr(a);
printf("\nAfter Converting upper case characters to lower case characters the resultant
string is %s",a);
return 0;
Result:-
Input:-
RaMeSh
Output:-
14
After Converting upper case characters to lower case characters the resultant string is
ramesh
Ex2:-C program to convert upper case characters to lower case characters With out
using library function:-
#include<stdio.h>
#include<conio.h>
#include<string.h>
int main()
char a[10];
int i,j;
clrscr();
printf("\nEnter a string\n");
gets(a);
for(i=0;a[i]!='\0';i++)
a[i]=a[i]+32;
a[i]='\0';
printf("\nAfter Converting upper case characters to lower case characters the resultant
string is %s",a);
return 0;
7.strupr( ):- It is useful to convert lower case characters of the string to upper case
characters.
Syntax:- strupr(str);
Ex1:-C program to convert lower case characters to upper case characters using library
function:-
15
#include<stdio.h>
#include<conio.h>
#include<string.h>
int main()
char a[10];
int i,j;
clrscr();
printf("\nEnter a string\n");
gets(a);
strupr(a);
printf("\nAfter Converting lower case characters to upper case characters the resultant
string is %s",a);
return 0;
Result:-
Input:-
RaMeSh
Output:-
After Converting lower case characters to upper case characters the resultant string is
RAMESH
Ex2:-C program to convert lower case characters to upper case characters Without
using library function:-
#include<stdio.h>
#include<conio.h>
#include<string.h>
int main()
16
char a[10];
int i,j;
clrscr();
printf("\nEnter a string\n");
gets(a);
for(i=0;a[i]!='\0';i++)
a[i]=a[i]-32;
a[i]='\0';
return 0;
Like numbers we can also perform arithmetic operations on characters. Every character has
an ASCII value.
ASCII value of A is 65
ASCII value of Z is 90
ASCII value of a is 97
ASCII value of 0 is 48
ASCII value of 9 is 57
printf(“\n%c”,x);
17
printf(“\n%d”,x);
Output:- B
66
Addition:-
Ex1:- char x;
x=’a’+3;
printf(“\n%c”,x);
printf(“\n%d”,x);
Output:- d
100
y=’2’;
x=y+3;
printf(“\nx=%d\tx=%c\ty=%d\ty=%c”,x,x,y,y);
Subtraction:-
Ex:- char x;
x=’z’-1;
printf(“\nx=%d”,x);
printf(“\nx=%c”,x);
Output:- x=121
x=y
Multiplication:-
Ex:- char x;
x=’B’*1;
printf(“\nx=%d”,x);
printf(“\nx=%c”,x);
18
Output:- x=68
x=B
Division:-
Ex:- char x;
x=’r’/2;
printf(“\nx=%d”,x);
printf(“\nx=%c”,x);
Output:- x=57
x=9
Incrementation:-
y=’a’;
x=y++;
printf(“\nx=%d\tx=%c\ty=%d\ty=%c”,x,x,y,y);
#include<stdio.h>
#include<conio.h>
#include<string.h>
int main()
char a[10],b[10];
int i;
clrscr();
printf("\nEnter a string\n");
19
gets(a);
strcpy(b,a);
if(strcmp(strrev(a),b)==0)
printf("Palindrome string");
else
return 0;
Result:-
Input:-
Enter a string
malayalam
Output:-
Palindrome string
#include<stdio.h>
#include<conio.h>
#include<string.h>
int main()
char a[10];
int i,j,low,high,flag=0,len=0;
clrscr();
printf("\nEnter a string\n");
gets(a);
for(i=0;a[i]!='\0';i++)
20
len++;
low=0;
high=len-1;
while(low<=high)
if(a[low]!=a[high])
flag=1;
break;
low++;
high--;
if(flag==1)
else
printf("palindrome string");
return 0;
Result:-
Input:-
Enter a string
ramu
Output:-
Ex2:-C program to count number of characters, number of digits and number of vowels
and number of spaces(words) in it.
#include<stdio.h>
#include<conio.h>
int main()
char a[20];
int i,nc=0,nv=0,nd=0,ns=0;
clrscr();
printf("\nEnter a string\n");
gets(a);
for(i=0;a[i]!='\0';i++)
nc++;
if(a[i]=='a'||a[i]=='e'||a[i]=='i'||a[i]=='o'||a[i]=='u'||a[i]=='A'||a[i]=='E'||a[i]=='I'||
a[i]=='O'||a[i]=='U')
nv++;
nd++;
ns++;
printf("\nNumber of characters=%d",nc);
printf("\nNumber of vowels=%d",nv);
printf("\nNumber of digits=%d",nd);
printf("\nNumber of spaces=%d",ns);
return 0;
}
22
Result:-
Input:-
Enter a string
Ouput:-
Number of characters=17
Number of vowels=5
Number of digits=3
Number of spaces=4