C Programming - Strings
- Strings - General Questions
- Strings - Find Output of Program
- Strings - Point Out Correct Statements
- Strings - Yes / No Questions
#include<stdio.h>
#include<string.h>
int main()
{
char str1[20] = "Hello", str2[20] = " World";
printf("%s\n", strcpy(str2, strcat(str1, str2)));
return 0;
}
Step 1: char str1[20] = "Hello", str2[20] = " World"; The variable str1 and str2 is declared as an array of characters and initialized with value "Hello" and " World" respectively.
Step 2: printf("%s\n", strcpy(str2, strcat(str1, str2)));
=> strcat(str1, str2)) it append the string str2 to str1. The result will be stored in str1. Therefore str1 contains "Hello World".
=> strcpy(str2, "Hello World") it copies the "Hello World" to the variable str2.
Hence it prints "Hello World".
#include<stdio.h>
int main()
{
char p[] = "%d\n";
p[1] = 'c';
printf(p, 65);
return 0;
}
Step 1: char p[] = "%d\n"; The variable p is declared as an array of characters and initialized with string "%d".
Step 2: p[1] = 'c'; Here, we overwrite the second element of array p by 'c'. So array p becomes "%c".
Step 3: printf(p, 65); becomes printf("%c", 65);
Therefore it prints the ASCII value of 65. The output is 'A'.
#include<stdio.h>
#include<string.h>
int main()
{
printf("%d\n", strlen("123456"));
return 0;
}
The function strlen returns the number of characters in the given string.
Therefore, strlen("123456") returns 6.
Hence the output of the program is "6".
#include<stdio.h>
int main()
{
printf(5+"Good Morning\n");
return 0;
}
printf(5+"Good Morning\n"); It skips the 5 characters and prints the given string.
Hence the output is "Morning"
#include<stdio.h>
#include<string.h>
int main()
{
char str[] = "India\0\BIX\0";
printf("%s\n", str);
return 0;
}
A string is a collection of characters terminated by '\0'.
Step 1: char str[] = "India\0\BIX\0"; The variable str is declared as an array of characters and initialized with value "India"
Step 2: printf("%s\n", str); It prints the value of the str.
The output of the program is "India".