You are on page 1of 22

1

Limitations or Drawbacks of Arrays:-

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:-

1. Arrays are used to implement matrices.


2. Arrays are used to implement data structures such as stacks, queues, trees, graphs,
heaps, hash table and so on.
3. Arrays are used to implement searching techniques like linear search and binary
search.
4. Arrays are used to implement sorting techniques like bubble sort, selection sort,
insertion sort, merge sort, quick sort, bucket sort, shell sort and so on.
5. Arrays are used in real world applications such as bus reservation system, banking
services, online movie ticket boooking and so on.
6. Arrays are used to implement CPU scheduling algorithms.

Strings
A string can be defined as a collection of characters enclosed in double quotes.

A string is nothing but a character array.

Ex:- “ramu” , “$*-“ , “123” , “ra12$?”

A string must terminates or ends with NULL character (‘\0’).

Syntax for declaring a string:-

char array_name[size];

Ex:- char name[30];

For array_name identifier rules are to be followed and array_name is nothing but string.

Inititalization of a string:-
2

Ex1:- char a[10]=”hello”;

Ex2:- char a[10]={‘h’,’a’,’i’};

Ex3:- char a[ ]=”ramu”;

The length of string is the number of characters in the string.

When the string is initialized then size of the array is equals to number of characters + one(1).

Representing a string in memory:-

char a[10]=”hai”;

1000 1001 1002 1003 1004 1005 1006 1007 1008 1009

H a i \0

Reading a string:- We can read a string mainly using 2 functions.

1.scanf( )

2.gets( )

1.scanf( ):- It is used to read a string.

Ex:- char a[10];

Scanf(“%s”,&a);

Format specifier (%s) specifies string.

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.

Ex:- char a[10];

gets(a);

Displaying a string:- We can display a string mainly using 2 functions.

1.printf( )

2.puts( )

1.printf( ):- It is used to print a string.

Ex:- char a[10];

printf(“%s”,a);
3

Format specifier (%s) specifies string.

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.

Ex:- char a[10];

puts(a);

String handling functions or String manipulation functions:-

To manipulate the strings we uses string handling functions and these functions are available
in the header file called “string.h”.

The important string handling functions are:

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:-

strlen(str); where str is a string.

Ex1:- C program to calculate length of a string using library function

#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);

printf("\nlength of string is %d",len);

return 0;

Result:-

Input:-

Enter a string

Ramu

Output:-

length of string is 4

Ex2:- C program to calculate length of a string without using library function

#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

printf("\nlength of string is %d",len);

return 0;

Result:-

Input:-

Enter a string

Ramu

Output:-

length of string is 4

2.strcpy( ) :- It is used to copy the contents of source string to destination string.

Syntax:- strcpy(destination,source);

It copies the contents of source string to destination string.

Ex1:- C program to copy a string using library function

#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);

printf("\nCopied string is %s",b);

return 0;

}
6

Result:-

Input:-

Enter a string

Ramu

Output:-

Copied string is Ramu

Ex2:- C program to copy a string with out using library function

#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';

printf("\nCopied string is %s",b);

return 0;

Result:-

Input:-

Enter a string

Ramu

Output:-
7

Copied string is Ramu

3.strcat( ):- It is used to concatenate 2 strings.

Syntax:- strcat(s1,s2);

When the above function is executed then content of string s2 will be appended at the end of
string s1.

Ex1:- C program to concatenate 2 strings using library function

#include<stdio.h>

#include<conio.h>

int main()

char a[10],b[10];

int i;

clrscr();

printf("\nEnter first string\n");

gets(a);

printf("\nEnter second string\n");

gets(b);

strcat(a,b);

printf("\nConcatenated string is %s",a);

return 0;

Result:-

Input:-

Enter First string

Ram

Enter second string

Babu
8

Output:-

Concatenated string is RamBabu

Ex2:- C program to concatenate 2 strings without using library function

#include<stdio.h>

#include<conio.h>

#include<string.h>

int main()

char a[10],b[10];

int i,j;

clrscr();

printf("\nEnter first string\n");

gets(a);

printf("\nEnter second string\n");

gets(b);

for(i=strlen(a),j=0;b[j]!='\0';i++,j++)

a[i]=b[j];

a[i]='\0';

printf("\nConcatenated string is %s",a);

return 0;

Result:-

Input:-

Enter First string

Ram

Enter second string

Babu
9

Output:-

Concatenated string is RamBabu

4.strrev( ):- It is useful to reverse a given string.

Syntax:-

strrev(str); where str is a string.

Ex1:- C program to reverse a string using library function

#include<stdio.h>

#include<conio.h>

#include<string.h>

int main()

char a[10];

clrscr();

printf("\nEnter a string\n");

gets(a);

strrev(a);

printf("\nReverse of a string is %s",a);

return 0;

Result:-

Input:-

Enter a string

ramu

Output:-

Reverse of a string is umar

Ex2:- C program to reverse a string without using library function

#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( ):-

 It is used to compare 2 strings.


 strcmp( ) returns an integer value.
 strcmp( ) returns 0(zero) when both strings are equal.
 strcmp( ) returns positive value if string1>string2.
 strcmp( ) returns negative value if string1<string2.

Ex1:- C program to compare 2 strings using library function

#include<stdio.h>
11

#include<conio.h>

#include<string.h>

int main()

char a[10],b[10];

int i;

clrscr();

printf("\nEnter first string\n");

gets(a);

printf("\nEnter second string\n");

gets(b);

i=strcmp(a,b);

if(i==0)

printf("\nBoth strings are equal");

else

printf("\nBoth strings are not equal");

return 0;

Result:-

Input:-

Enter First string

Ram

Enter second string

Ram

Output:-

Both strings are equal

Ex2:- C program to compare 2 strings with out using library function


12

#include<stdio.h>

#include<conio.h>

#include<string.h>

int main()

char a[10],b[10];

int i;

clrscr();

printf("\nEnter first string\n");

gets(a);

printf("\nEnter second string\n");

gets(b);

i=0;

while(a[i]==b[i] && a[i]!='\0' && b[i]!='\0')

i++;

if(a[i]=='\0' && b[i]=='\0')

printf("\nBoth strings are equal");

else

printf("\nBoth strings are not equal");

return 0;

Result:-

Input:-

Enter First string

Ram

Enter second string

Babu
13

Output:-

Both strings are not equal

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:-

Enter First string

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++)

if(a[i]>=65 && a[i]<=90)

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:-

Enter First string

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++)

if(a[i]>=97 && a[i]<=122)

a[i]=a[i]-32;

a[i]='\0';

printf("\nAfter Converting lower case characters to upper case characters the


resultant string is %s",a);

return 0;

Arithmetic Operations on characters:-

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 z is 122

ASCII value of 0 is 48

ASCII value of 9 is 57

Ex1:- char x=’B’;

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

Ex2:- char x,y;

y=’2’;

x=y+3;

printf(“\nx=%d\tx=%c\ty=%d\ty=%c”,x,x,y,y);

Output:- x=53 x=5 y=50 y=2

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:-

Ex:- char x,y;

y=’a’;

x=y++;

printf(“\nx=%d\tx=%c\ty=%d\ty=%c”,x,x,y,y);

Output:- x=97 x=a y=98 y=b

Ex1: C program to check whether the given string is palindrome or not:-

Using library functions:-

#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

printf("Not a palindrome string");

return 0;

Result:-

Input:-

Enter a string

malayalam

Output:-

Palindrome string

With out Using library functions:-

#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)

printf("Not a palindrome string");

else

printf("palindrome string");

return 0;

Result:-

Input:-

Enter a string

ramu

Output:-

Not a Palindrome string


21

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++;

if(a[i]>=48 && a[i]<=57)

nd++;

if(a[i]==' '|| a[i]=='\t')

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

Ramu is a 654 boy

Ouput:-

Number of characters=17

Number of vowels=5

Number of digits=3

Number of spaces=4

You might also like