Strings and Standard Functions II-177
{
char ans[8];
int i;
clrscr();
for(i=1;i<=3;i++)
{
printf(" What is the currency of India?");
scanf("%s",ans);
fflush(stdin);
if(stricmp(ans,"Rupees")==0)
{
printf(" Answer is Correct.");
exit(1);
}
else
if (i<3)
printf(" Try Again ! ");
}
clrscr();
printf("NCurrency of India is Rupees.");
}
OUTPUT:
What is the currency of India?Rupee
Try Again !
What is the currency of India?Ruppea
Try Again !
What is the currency of India?Rupees
Answer is Correct.
19. Use of functions such as strcmp, strcat, and strlen
void main()
{
char str1[20]="C is", str2[20]="easy language";
int x,y,z;
clrscr();
printf(" Source String:-%s",str1);
printf(" Destination String:-%s",str2);
y=strcmp(str2,str1);
if(y==0)
{
z=strlen(str1)==strlen(str2);
printf(" Length of both the strings:- %d" ,z);
}
M08_ITL-ESL4791_02_SE_C08.indd 177 12/22/2012 5:03:31 PM
II-178 Programming Concepts
else
{
strcat(str1,str2);
printf(" Final string is :-%s",str1);
x=strlen(str1);
printf(" Length of the final string:- %d" ,x);
}
getche();
}
OUTPUT:
Source String:-C is
Destination String:-easy language
Final string is :-C is easy language
Length of the final string:- 16
Use of memcmp() function 
20. Find length of strings with memcmp() function
#include <string.h>
#include <conio.h>
void main()
{
char text[30]="LATUR",text1[20]="UDGIR",chp;
clrscr();
printf(" First text is:-%s",text);
printf(" Second text is :-%s",text1);
chp=memcmp(text,text1,5);
if (chp!=0)
printf(" Text length of first string is same as of
second");
else
printf(" Text length of the strings are not same ");
getche();
}
OUTPUT:
First text is:-LATUR
Second text is:-UDGIR
Text length of first string is same as of second
M08_ITL-ESL4791_02_SE_C08.indd 178 12/22/2012 5:03:31 PM
Strings and Standard Functions II-179
Find the bug/s in the following program/s.
String operations and display 
1.
void main()
{
char name[6]={'S','A','N','J','A','Y'}; clrscr();
puts(name);
}
Bug: String is larger than the array capacity.
2.
void main()
{
char name[7]={'S','A','N','J','A','Y'};
clrscr();
printf(" %.0s",name);
}
Bug: The zero before s restricts the output.
3.
#include <stdio.h>
void main()
{
char *name={"Have a Nice Day"},c;
clrscr();
for(c=0;c<15;c++)
printf("%s",name[c]);
}
Bug: Format string %c is required.
4.
#include <string.h>
void main()
{
char a1[]={"KOLKATTA"};
int i;
clrscr();
while(a1[i++]!='')
printf("%c ",a1[i]);
getche();
}
Bug: 'i' is to be initialized with some value say i= -1.
M08_ITL-ESL4791_02_SE_C08.indd 179 12/22/2012 5:03:31 PM
II-180 Programming Concepts
5.
#include <string.h>
void main()
{
char a1[6]={'NAGPUR'};
clrscr();
printf("%c ",a1);
getche();
}
Bug: Array initialization is incorrect and %s is to used instead of %c.
6.
#include <string.h>
void main()
{
char *a[]="ROYAL";
clrscr();
printf("%s %u",*(a),&*a);
getche();
}
Bug: ROYAL must be enclosed within curly braces.
7.
#include <string.h>
void main()
{
char a1[6]={"CHENNAI"};
int i;
clrscr();
for(i=0;i<7;i++)
printf("%c ",a1[i]);
++i;
getche();
}
Bug: char array is to be initialized with 7 instead of 6.
M08_ITL-ESL4791_02_SE_C08.indd 180 12/22/2012 5:03:31 PM
Strings and Standard Functions II-181
8.
void main()
{
char text[15]="MOUNTAIN";
int i;
clrscr();
for(i=7;i>=0;i--)
printf("%.8d",text[i]);
getche();
}
Bug: Replace 'c' instead of 'd' in printf statement.
9.
#include <string.h>
void main()
{
char *a2[10]={"MADHYAPRADESH"};
clrscr();
printf("%s",*&(a2));
getche();
}
Bug: '&' operator to be deleted.
strcpy()Function
10.
void main()
{
char str1[15]='snowy', str2[15]='sunny';
clrscr();
printf("n Source String-:%s",str1);
printf("n Destination String-:%s",str2);
strncpy(str2,str1,3);
printf("nDestination String -:%s",str2);
getche();
}
Bug: Double quotation mark is needed in char statement.
M08_ITL-ESL4791_02_SE_C08.indd 181 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