Академический Документы
Профессиональный Документы
Культура Документы
Character arrays
1. Assuming the variable string contains the value “The sky is the limit”, determine what
output of the following program segments will be.
Instruction Output
a) printf(“%s”,string); The sky is the limit
char s1[10]=”he”,s2[20]=”she”,s3[30],s4[30];
Output:
printf("%s\n",strcpy(s3,s1));
he
printf("%s\n",strcat(strcat(strcpy(s4,s1),"or"),s2)); heorshe
5
printf("%d\n%d\n",strlen(s2)+strlen(s3),strlen(s4));
7
3. Which of the following function is used to find the first occurrence of a given string in
another string?
A. strchr() B. strrchr()
C. strstr() D. strnset()
4. What will be the output of the program?
1) #include<stdio.h> 2) #include<stdio.h>
#include<string.h> int main( )
int main( ) {
{ Printf(5+”Good Morning\n”);
printf(“%d\n”,strlen(“123456”)); return 0;
return 0; }
} Answer: Morning
Answer: 6
3) #include<stdio.h> 4) int main( )
int main( ) {
{ printf(“india”,”BIX\n”);
static char s[25]=”The cocaine return 0;
man”; }
int i=0; Answer: india
char ch;
ch=s[++i];
printf(“%c”,ch);
ch=s[i++];
printf(“%c”,ch);
return 0;
}
Answer: hh
5) int main( ) 6) int main( )
{ {
int i; char str[ ]=”India\OBIX\0”;
char a[ ]=”\0”; printf(“%d\n”,sizeof(str));
if(printf(“%s”,a)) }
printf(“The string is Answer: 11
empty\n”);
else ( It counts \O as 1 character because of
printf(“The string is not escape sequence backslash and \0 as 1
empty\n”); character and sizeof counts the null
return 0; character of the string implicitly.
} Therefore result is
Answer: The string is not empty India(5)+\O(1)+BIX(3)+\0(1)+ null(1)
=11 )
(Null is not zero so it executes else part)