0% found this document useful (0 votes)
548 views

Tutorial 9 Solutions

1. The document discusses character arrays and string functions in C. It provides sample code snippets and their expected outputs. 2. Key functions discussed include strcpy(), strcat(), strlen(), and strstr(). strcpy() copies one string to another, strcat() concatenates two strings, strlen() returns the length of a string, and strstr() finds the first occurrence of a substring. 3. Examples demonstrate using character arrays and strings, formatting string outputs, concatenating and copying strings, calculating string lengths, and searching for substrings. The correct answers are provided for each code snippet.

Uploaded by

Tejas Iyengar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
548 views

Tutorial 9 Solutions

1. The document discusses character arrays and string functions in C. It provides sample code snippets and their expected outputs. 2. Key functions discussed include strcpy(), strcat(), strlen(), and strstr(). strcpy() copies one string to another, strcat() concatenates two strings, strlen() returns the length of a string, and strstr() finds the first occurrence of a substring. 3. Examples demonstrate using character arrays and strings, formatting string outputs, concatenating and copying strings, calculating string lengths, and searching for substrings. The correct answers are provided for each code snippet.

Uploaded by

Tejas Iyengar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Tutorial 9 Solution

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

b) printf(“%25.10s”,string); BBBBBBBBBBBBBBBThe sky is

c) printf(“%s”,&string[0]); The sky is the limit

d) for(i=0;string[i]!='.';i++) The sky is the limit.....keeps on printing some


printf(“%c”,string[i]); Garbage value

e) for(i=0;string[i]!=’\0’;i++) Prints integer value associated with each


printf(“%d\n”,string[i]); character

2. Assume that s1,s2 and s3 are declared as follows:

char s1[10]=”he”,s2[20]=”she”,s3[30],s4[30];

What will be the output of the following statements executed in sequence?

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)

You might also like