II-172 Programming Concepts
int i,j;
clrscr();
printf("Sorted List of Cities. ");
for(i=65;i<=122;i++)
{
for(j=0;j<5;j++)
{
if(place[j][0]==i)
printf(" %s",place[j]);
}
}
getche();
}
OUTPUT:
Sorted list of cities.
Bangalore
Chennai
Delhi
Mumbai
Nanded
9. String copy and size of strings
#include <string.h>
void main()
{
char a[33]={"Software people are intelligent"};
char b[60]={"Highly knowledgeable people can design
hardware circuits"};
clrscr();
strcpy(a,b);
printf(" %s ",a);
printf(" %d %d",sizeof(a),sizeof(b));
getche();
}
OUTPUT:
Highly knowledgeable people can design hardware circuits
33 60
M08_ITL-ESL4791_02_SE_C08.indd 172 12/22/2012 5:03:31 PM
Strings and Standard Functions II-173
10. Copying without strcpy()
void main()
{
char main[10]="abcd",dup[10];
int i;
clrscr();
for(i=0;i<10;i++)
dup[i]=main[i];
printf("Origional String : %s",main);
printf(" Duplicate String : %s",dup);
getche();
}
OUTPUT:
Original string : abcd
Duplicate string : abcd
String copy up to cer tain length with strncpy() function
11. String copy up to certain length
void main()
{
char main[20]="Amit", photocopy[20];
int n =3;
clrscr();
strncpy(photocopy,main,n);
printf("Origional String : %s",main);
printf(" Duplicate String : %s",photocopy);
getche();
}
OUTPUT:
Original string : Amit
Duplicate string : Ami
M08_ITL-ESL4791_02_SE_C08.indd 173 12/22/2012 5:03:31 PM
II-174 Programming Concepts
Programs on different string functions:strcpy(),strlwr(),strupr, etc 
12. Use different string functions: strcpy(), strlwr(), strupr
#include <stdio.h>
#include <conio.h>
void main()
{
char a[50]={"Computer Education leads to Prosperity and fame"};
char b[50]; clrscr(); strcpy(b,a); printf("%s %s",a,b);
strupr(b);
printf(" %s",b);
strlwr(b);
printf(" %s",b);
strrev(b);
printf(" %s",b);
getche();
}
OUTPUT:
Computer Education leads to Prosperity and fame
Computer Education leads to Prosperity and fame
COMPUTER EDUCATION LEADS TO PROSPERITY AND FAME
computer education leads to prosperity and fame
emaf dna ytirepsorp ot sdael noitacude retupmoc
String length with and without strlen() function
13. Program to find the length of the string.
void main()
{
char text[20];
int len;
clrscr();
printf("type text below. ");
gets(text);
len=strlen(text);
printf("lenth of the strng is=%d",len);
}
OUTPUT:
My name is=Romali
Length of the string is=6
M08_ITL-ESL4791_02_SE_C08.indd 174 12/22/2012 5:03:31 PM
Strings and Standard Functions II-175
14. Length of a string without function
void main()
{
char group[15]={'I','n','d','i','a'};
int len=0,i;
clrscr();
for(i=0;group[i]!='';i++)
len++;
printf("Length of the string is=%d",len);
getch();
}
OUTPUT:
Length of the string is=5
15. Program to find the length of the strings without function
void main()
{
char str1[20]={'M','U','T','H','A'},
str2[20]={'M','U','L','H','A'};
int diff,i=0,j=0;
clrscr();
while(str1[i]!='')
i++;
printf(" Length of the first string =%d",i);
while(str2[j]!='')
j++;
printf(" Length of the second string =%d",j);
if(i==j)
printf(" Length of the strings are same");
else
printf(" Length of the strings are not same ");
getch();
}
OUTPUT:
Length of the first string =5
Length of the second string =5
Length of the strings are same
M08_ITL-ESL4791_02_SE_C08.indd 175 12/22/2012 5:03:31 PM
II-176 Programming Concepts
String comparison with and without strcmp() function
16. Program on comparison of two strings
void main()
{
char str1[20]={'C','o','m','p','u','t','e','r'},
str2[20]={'C','o','m','p','u','t','e','r'};
clrscr();
if( (strcmp(str1,str2))==0)
printf("Length of the strings are same ");
else
printf("Length of the strings are not same ");
getch();
}
OUTPUT:
Length of the strings are same
17. Program to compare the two strings
void main()
{
char str1[20]={'G','o','d','a','w','a','r','i'},
str2[20]={'S','a','r','a','s','w','a','t','i'};
int diff;
clrscr();
diff=(strcmp(str1,str2));
if(diff==0)
printf(" strings are same ");
else
printf(" strings are not same ");
getch();
}
OUTPUT:
strings are not same
18. Program on stricmp() function
#include <stdio.h>
#include <conio.h>
#include <process.h>
void main()
M08_ITL-ESL4791_02_SE_C08.indd 176 12/22/2012 5:03:31 PM
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset