Strings
Strings
10] In the following code which function would get called, the user-def
ined strcpy() or the one in the standard library ?
main()
{
char tr1[] = "keep India Beautiful ...emigrate!");
char str2[40];
strcpy(str2,str1);
printf("\n %s",str2);
}
strcpy(char *t,char *s)
{
while(*s)
{
*t = *s;
t++;
s++;
}
*t = "\0";
}
11] Can you compact the code in strcpy() into one line ?
12] Can you compact the code in strcpy() into one line ?
main()
{
char *str[] = {"Frogs","Do","Not","Die.","They","Crock!"};
printf("%d %d",sizeof(str),sizeof(str[0]));
}
13] How would you find the length of each string in the program (12) abo
ve ?
***********************************************************************
************************* ANSWERS *******************************
***********************************************************************
1] C
2] B
3] C
5] printf("\\n");
6] B
7] B
9] main()
{
char str1[15] = "United";
char *str2 = "Front";
char *str3;
str3 = strcat(str1,str2);
printf("\n %s",str3);
}
12] 12 2
13] main()
{
char *str[] = {"Frogs","Do","Not","Die","They","Croak!");
int i;
for(i = 0;i <= 5;i++)
printf("%s %d",str[i],strlen(str[i]));
}
14] Here 'a' is an array big enough to hold the message and
the '\0' following the message. Individual characters within the array can be
changed but the address of the array would remain same.
On the other hand, 'p' is a pointer, initialized to point to
a string constant. The pointer 'p' may be nodified to point to another string,
but if you attempt to modify the string at which 'p' is pointing the resu
lt is undefined.