Explanation: In C, strings are terminated by the null character ‘\0’, which signifies the end of the string.
#include <stdio.h>
char str[] = "Programming";
printf("%c", str[7]);
Explanation: The index starts from 0 in C arrays. So, str[7] refers to the eighth character of the string “Programming”, which is ‘m’.
Explanation: The strcmp() function in C is used to compare two strings lexicographically. It returns 0 if both strings are equal, a positive value if the first string is greater, and a negative value if the second string is greater.
#include <stdio.h>
char str1[] = "Hello";
char str2[] = "World";
strcat(str1, str2);
printf("%s", str1);
Explanation: The strcat() function in C is used to concatenate two strings. However, in the given code, there is no space allocated in str1 to accommodate the concatenated string. This will result in a compile-time error.
Explanation: To read a string with spaces using scanf() in C, you should use the format specifier “%[^\n]”, which reads characters until it encounters a newline character ‘\n’.
Explanation: The strchr() function in C is used to find the first occurrence of a character in a string. It returns a pointer to the first occurrence of the character in the string or NULL if the character is not found.
#include <stdio.h>
char str[] = "Hello";
printf("%d", sizeof(str));
Explanation: sizeof(str) returns the size of the array ‘str’ in bytes, including the null character ‘\0’. The string “Hello” has 5 characters, so the size will be 5 + 1 (for the null character) = 6.
Explanation: The strcpy() function in C is used to copy one string to another. It copies the contents of the source string to the destination string including the null character.
Explanation: In C, a pointer to a string is typically declared using the syntax char* ptr, where ‘ptr’ is the name of the pointer variable.
#include <stdio.h>
char str1[] = "Hello";
char str2[] = "Hello";
if (str1 == str2)
printf("Strings are equal");
else
printf("Strings are not equal");
Explanation: In C, comparing strings using the == operator compares the addresses of the first characters of the strings, not their contents. Since str1 and str2 are different arrays, their addresses will be different, and the output will be “Strings are not equal”.
Explanation: All the given options represent valid ways to initialize a string in C. Option A initializes the string with a specified size and assigns a string literal. Option B initializes the string with individual characters. Option C initializes the string with individual characters and implicitly adds the null character at the end.
#include <stdio.h>
char str[10];
str[0] = 'H';
str[1] = 'e';
str[2] = 'l';
str[3] = 'l';
str[4] = 'o';
printf("%s", str);
Explanation: The given code assigns characters to the elements of the array ‘str’ but does not terminate the string with a null character. So, when printing the string using %s specifier in printf(), it will print “Hello” followed by garbage values until it encounters a null character.
Explanation: In C, strings are stored as arrays of characters, and the size of the array determines the size of the string. However, the size of the string is always one less than the length of the array to accommodate the null character ‘\0’ at the end.
#include <stdio.h>
char str[5] = "Hello";
printf("%s", str);
Explanation: The given code tries to initialize a character array ‘str’ with the string “Hello”, but the size of the array is not large enough to hold the null character ‘\0’. This leads to undefined behavior, as there is no space for the null character to terminate the string properly.
Explanation: Option A correctly declares and initializes a string pointer ‘str’ to point to the string literal “Hello”. Option B has incorrect syntax for declaring a pointer. Option C is not valid in C for string initialization. Option D initializes an array, not a pointer.
Explanation: In C, strings can be initialized using either double quotes (“) or curly braces ({ }). Both methods are valid and commonly used for string initialization.
#include <stdio.h>
char str[5] = {'H', 'e', 'l', 'l', 'o'};
printf("%s", str);
Explanation: The given code initializes the character array ‘str’ with individual characters but does not terminate the string with a null character ‘\0’. So, when printing the string using %s specifier in printf(), it will print “Hello” followed by garbage values until it encounters a null character.
Explanation: In C, strings are terminated by a null character ‘\0’, which is included in the array used to store the string. Therefore, the size of the string is always one less than the length of the array.
#include <stdio.h>
char* str = "Hello";
str[0] = 'h';
printf("%s", str);
Explanation: The given code tries to modify a string literal, which results in undefined behavior in C. Attempting to modify a string literal directly is not allowed and may result in a runtime error.
Explanation: Option A initializes a character array ‘str’ with a specified size of 5 and assigns the string “Hello” to it. The size of the array must be large enough to accommodate the string along with the null character ‘\0’.
Explanation: The strlen() function in C is used to find the length of a string, i.e., the number of characters in the string excluding the null character ‘\0’.
#include <stdio.h>
char str[] = "Programming";
int length = strlen(str);
printf("%d", length);
Explanation: The strlen() function returns the length of the string “Programming”, which is 10 characters, excluding the null character ‘\0’.
Explanation: The strcpy() function in C is used to copy one string to another. It copies the contents of the source string to the destination string including the null character.
#include <stdio.h>
char source[] = "Hello";
char destination[10];
strcpy(destination, source);
printf("%s", destination);
Explanation: The strcpy() function copies the contents of the source string “Hello” to the destination string, which results in “Hello” being printed when using printf() to output the destination string.
Explanation: The strcat() function in C is used to concatenate (append) the content of the source string to the destination string. It appends the content of the source string to the end of the destination string, overwriting the null character (‘\0’) at the end of the destination, and then adds a terminating null character.
#include <stdio.h>
char str1[] = "Hello";
char str2[] = "World";
strcat(str1, str2);
printf("%s", str1);
Explanation: The strcat() function concatenates the content of the source string “World” to the end of the destination string “Hello”. However, since the destination string “Hello” is not large enough to accommodate the concatenated string, it leads to undefined behavior and prints “HelloWorld” followed by garbage values.
Explanation: The strcmp() function in C is used to compare two strings lexicographically. It returns 0 if both strings are equal, a positive value if the first string is greater, and a negative value if the second string is greater.
#include <stdio.h>
char str1[] = "apple";
char str2[] = "banana";
int result = strcmp(str1, str2);
printf("%d", result);
Explanation: The strcmp() function compares the strings “apple” and “banana” lexicographically. Since “apple” comes before “banana” in dictionary order, the result is a negative value.
Explanation: The strchr() function in C is used to find the first occurrence of a character in a string. It returns a pointer to the first occurrence of the character in the string or NULL if the character is not found.
#include <stdio.h>
char str[] = "Hello";
char* ptr = strchr(str, 'l');
printf("%s", ptr);
Explanation: The strchr() function searches for the first occurrence of the character ‘l’ in the string “Hello” and returns a pointer to that location. So, ptr points to the substring “llo”, which is then printed using printf().
Explanation: The strrchr() function in C is used to find the last occurrence of a character in a string. It returns a pointer to the last occurrence of the character in the string or NULL if the character is not found.
#include <stdio.h>
char str[] = "programming";
char* ptr = strrchr(str, 'm');
printf("%s", ptr);
Explanation: The strrchr() function searches for the last occurrence of the character ‘m’ in the string “programming” and returns a pointer to that location. So, ptr points to the substring “mming”, which is then printed using printf().
Explanation: The strstr() function in C is used to find the first occurrence of a substring within a string. It returns a pointer to the first occurrence of the substring in the string or NULL if the substring is not found.
#include <stdio.h>
char str[] = "programming";
char* ptr = strstr(str, "gram");
printf("%s", ptr);
Explanation: The strstr() function searches for the first occurrence of the substring “gram” in the string “programming” and returns a pointer to that location. So, ptr points to the substring “gramming”, which is then printed using printf().
Explanation: The strtok() function in C is used to tokenize (split) a string into smaller strings (tokens) based on a delimiter. It returns a pointer to the next token in the string or NULL if no more tokens are found.
#include <stdio.h>
char str[] = "apple,banana,orange";
char* token = strtok(str, ",");
while (token != NULL) {
printf("%s\n", token);
token = strtok(NULL, ",");
}
Explanation: The strtok() function splits the string “apple,banana,orange” into tokens separated by ‘,’. Each token is then printed on a new line using printf() in the while loop until no more tokens are found.
Explanation: The atoi() function in C is used to convert a string to an integer. It parses the initial portion of the string as an integer and returns the result.
#include <stdio.h>
char str[] = "12345";
int num = atoi(str);
printf("%d", num);
Explanation: The atoi() function converts the string “12345” to an integer, which is then printed using printf().
Explanation: The atof() function in C is used to convert a string to a floating-point number. It parses the initial portion of the string as a floating-point number and returns the result.
#include <stdio.h>
char str[] = "3.14";
float num = atof(str);
printf("%f", num);
Explanation: The atof() function converts the string “3.14” to a floating-point number, which is then printed using printf().
Explanation: The strtol() function in C is used to convert a string to a long integer. It parses the initial portion of the string as a long integer and returns the result.
#include <stdio.h>
char str[] = "123456789";
long num = strtol(str, NULL, 10);
printf("%ld", num);
Explanation: The strtol() function converts the string “123456789” to a long integer, which is then printed using printf().
Explanation: The strtoul() function in C is used to convert a string to an unsigned long integer. It parses the initial portion of the string as an unsigned long integer and returns the result.
#include <stdio.h>
char str[] = "4294967295";
unsigned long num = strtoul(str, NULL, 10);
printf("%lu", num);
Explanation: The strtoul() function converts the string “4294967295” to an unsigned long integer, which is then printed using printf().
Explanation: The sprintf() function in C is used to convert an integer to a string. It formats and stores a series of characters and values in the string buffer.
#include <stdio.h>
int num = 12345;
char str[20];
sprintf(str, "%d", num);
printf("%s", str);
Explanation: The sprintf() function formats the integer number 12345 as a string and stores it in the character array ‘str’, which is then printed using printf().
Explanation: The sprintf() function in C is used to convert a floating-point number to a string. It formats and stores a series of characters and values in the string buffer.
#include <stdio.h>
double num = 3.14159;
char str[20];
sprintf(str, "%.2f", num);
printf("%s", str);
Explanation: The sprintf() function formats the floating-point number 3.14159 as a string with two decimal places and stores it in the character array ‘str’, which is then printed using printf().
Explanation: The strupr() function in C is used to convert a string to uppercase. It modifies the string in place, changing all lowercase letters to their uppercase equivalents.
#include <stdio.h>
char str[] = "Hello World";
strupr(str);
printf("%s", str);
Explanation: The strupr() function converts the string “Hello World” to uppercase, which is then printed using printf().
Explanation: In C, the size of the character array includes space for the null character (‘\0’). So, in a character array of size 10, only 9 characters can be stored along with the null character.
#include <stdio.h>
char arr[5] = {'H', 'e', 'l', 'l', 'o'};
printf("%s", arr);
Explanation: The array ‘arr’ is not null-terminated because it’s initialized without a null character. So, when printing with %s in printf(), it will print “Hello” followed by garbage values until it finds a null character.
Explanation: In C, the size of a character array determines the maximum number of characters it can hold, including the null character (‘\0’) if present.
#include <stdio.h>
char arr[10];
arr[0] = 'H';
arr[1] = 'e';
arr[2] = 'l';
arr[3] = 'l';
arr[4] = 'o';
printf("%s", arr);
Explanation: Similar to previous examples, the array ‘arr’ is not null-terminated, so when printing with %s in printf(), it will print “Hello” followed by garbage values until it finds a null character.
Explanation: fgets() function in C can be used to input a string with spaces into a character array. It reads characters from the standard input (stdin) and stores them into the character array until a newline character, the end-of-file, or the specified maximum number of characters minus one is reached.
#include <stdio.h>
char arr[5];
fgets(arr, sizeof(arr), stdin);
printf("%s", arr);
Explanation: The fgets() function reads characters from the standard input (stdin) and stores them into the character array ‘arr’. When printing with %s in printf(), it will print the input string followed by a newline character.
Explanation: The strlen() function in C is used to find the length of a character array (string), i.e., the number of characters in the string excluding the null character ‘\0’.
#include <stdio.h>
char arr[] = "Programming";
int length = strlen(arr);
printf("%d", length);
Explanation: The strlen() function returns the length of the string “Programming”, which is 10 characters, excluding the null character ‘\0’.
Explanation: Character arrays in C are commonly used to represent strings, which are sequences of characters terminated by a null character (‘\0’).
#include <stdio.h>
char arr[] = {'H', 'e', 'l', 'l', 'o'};
printf("%s", arr);
Explanation: The array ‘arr’ is not null-terminated because it’s initialized without a null character. So, when printing with %s in printf(), it will print “Hello” followed by garbage values until it finds a null character.
#include <stdio.h>
char str1[] = "Hello";
char* str2 = "Hello";
Explanation: In the first declaration, str1 is an array of characters initialized with the string literal “Hello”. In the second declaration, str2 is a pointer to a string literal “Hello”.
#include <stdio.h>
char str[] = "Hello";
str[0] = 'J';
printf("%s", str);
Explanation: The code modifies the first character of the string “Hello” from ‘H’ to ‘J’ and then prints the modified string.
#include <stdio.h>
char* str = "Hello";
str[0] = 'J';
printf("%s", str);
Explanation: The code tries to modify a string literal, which results in undefined behavior. String literals are stored in read-only memory, and attempting to modify them directly is not allowed.
#include <stdio.h>
char str[] = "Hello";
printf("%c", str[5]);
Explanation: The code tries to access the character at index 5 of the string “Hello”, which is the null character ‘\0’ terminating the string.
#include <stdio.h>
char str[5] = "Hello";
printf("%s", str);
Explanation: The size of the array ‘str’ is 5, but it is initialized with a string literal “Hello” of length 6 (including the null character). This leads to undefined behavior and may print “Hello” followed by garbage values.
Explanation: The strncat() function in C is used to safely concatenate two strings by ensuring that the destination buffer has enough space to accommodate the concatenated string.
#include <stdio.h>
char dest[10] = "Hello";
char src[] = " World";
strncat(dest, src, 3);
printf("%s", dest);
Explanation: The strncat() function concatenates the first 3 characters of the source string ” World” to the destination string “Hello”, resulting in “Hello Wor”.
Explanation: The strncpy() function in C is used to safely copy one string to another by ensuring that the destination buffer has enough space to accommodate the copied string.
#include <stdio.h>
char dest[5];
char src[] = "Hello";
strncpy(dest, src, 5);
printf("%s", dest);
Explanation: The strncpy() function copies the first 5 characters of the source string “Hello” to the destination string ‘dest’. Since ‘dest’ has a size of 5, it can only hold “Hell”.
Explanation: The strncpy() function in C is used to copy a specified number of characters from one string to another, ensuring that the destination buffer has enough space.
Explanation: The `gets()` function in C is used to read a string from the standard input (stdin).
Explanation: The `gets()` function in C does not perform any bounds checking, which can lead to buffer overflow vulnerabilities.
Explanation: The `puts()` function in C is used to print a string to the standard output (stdout), followed by a newline character.
#include <stdio.h>
char str[] = "Hello";
puts(str);
Explanation: The `puts()` function prints the string “Hello” to the standard output followed by a newline character.
Explanation: The `fgets()` function in C is used to read a string from the standard input (stdin), allowing the specification of the maximum number of characters to read and the destination buffer.
#include <stdio.h>
char str[10];
fgets(str, sizeof(str), stdin);
Explanation: The `fgets()` function reads characters from the standard input (stdin) and stores them into the character array ‘str’, up to the specified maximum number of characters or until a newline character is encountered.
Explanation: The `fputs()` function in C is used to print a string to the specified output stream.
#include <stdio.h>
char str[] = "Hello";
fputs(str, stdout);
Explanation: The `fputs()` function prints the string “Hello” to the standard output without appending a newline character.
Explanation: Unlike `gets()`, which does not perform bounds checking and is prone to buffer overflow vulnerabilities, `fgets()` allows specifying the maximum number of characters to read, thus providing a safer alternative.
Explanation: The `sscanf()` function in C is used to parse (read) formatted input from a string, similar to `scanf()` but reading from a string instead of standard input.
#include <stdio.h>
char str[] = "Age: 25, Height: 180cm";
int age, height;
sscanf(str, "Age: %d, Height: %dcm", &age, &height);
printf("Age: %d, Height: %dcm", age, height);
Explanation: The `sscanf()` function parses the string “Age: 25, Height: 180cm” according to the specified format and stores the extracted values into the variables ‘age’ and ‘height’, which are then printed using `printf()`.
Explanation: The `sprintf()` function in C is used to format and store a series of characters and values as a string, similar to `printf()`, but instead of printing to standard output, it stores the formatted string in a buffer.
#include <stdio.h>
int num = 12345;
char str[20];
sprintf(str, "%d", num);
printf("%s", str);
Explanation: The `sprintf()` function converts the integer ‘12345’ to a string and stores it in the character array ‘str’, which is then printed using `printf()`.
Explanation: The `strtok()` function in C is used to tokenize (split) a string into smaller strings (tokens) based on a delimiter character.
#include <stdio.h>
char str[] = "apple,banana,orange";
char* token = strtok(str, ",");
while (token != NULL) {
printf("%s\n", token);
token = strtok(NULL, ",");
}
Explanation: The `strtok()` function splits the string “apple,banana,orange” into tokens separated by ‘,’, and each token is printed on a new line using `printf()` in the while loop until no more tokens are found.
Explanation: The `strchr()` function in C is used to find the first occurrence of a character in a string and returns a pointer to that location.
#include <stdio.h>
char str[] = "Hello";
char* ptr = strchr(str, 'l');
printf("%s", ptr);
Explanation: The `strchr()` function searches for the first occurrence of the character ‘l’ in the string “Hello” and returns a pointer to that location. So, ptr points to the substring “llo”, which is then printed using `printf()`.
Explanation: The `strstr()` function in C is used to find the first occurrence of a substring within a string and returns a pointer to that location.
#include <stdio.h>
int main() {
char str1[20] = "Hello";
char str2[] = "World";
strcat(str1, str2);
printf("%s", str1);
return 0;
}
Explanation: The code concatenates the string “World” to the end of the string “Hello” using the `strcat()` function, resulting in “HelloWorld” which is then printed using `printf()`.
#include <stdio.h>
int main() {
char str1[20] = "Programming";
char str2[20];
strcpy(str2, str1);
printf("%s", str2);
return 0;
}
Explanation: The code copies the content of the string “Programming” to the string `str2` using the `strcpy()` function, resulting in “Programming” which is then printed using `printf()`.
#include <stdio.h>
int main() {
char str1[20] = "Hello";
char str2[] = "World";
strncat(str1, str2, 3);
printf("%s", str1);
return 0;
}
Explanation: The code concatenates the first 3 characters of the string “World” to the end of the string “Hello” using the `strncat()` function, resulting in “HelloWor” which is then printed using `printf()`.
#include <stdio.h>
int main() {
char str[] = "apple,banana,orange";
char* token = strtok(str, ",");
while (token != NULL) {
printf("%s\n", token);
token = strtok(NULL, ",");
}
return 0;
}
Explanation: The code tokenizes the string “apple,banana,orange” by ‘,’ using the `strtok()` function and prints each token on a new line until no more tokens are found.
#include <stdio.h>
int main() {
char str[] = "abcde";
char* ptr = strchr(str, 'c');
printf("%s", ptr);
return 0;
}
Explanation: The code searches for the first occurrence of the character ‘c’ in the string “abcde” using the `strchr()` function and prints the substring “cde” from that location onwards.
#include <stdio.h>
int main() {
char str1[] = "apple";
char str2[] = "banana";
int result = strcmp(str1, str2);
printf("%d", result);
return 0;
}
Explanation: The code compares the strings “apple” and “banana” using the `strcmp()` function, which returns -1 because “apple” comes before “banana” in lexicographical order.
#include <stdio.h>
int main() {
char str[] = "Hello";
char* ptr = strstr(str, "ell");
printf("%s", ptr);
return 0;
}
Explanation: The code searches for the first occurrence of the substring “ell” in the string “Hello” using the `strstr()` function and prints the substring “ell” from that location onwards.
#include <stdio.h>
int main() {
char str1[20] = "Hello";
char str2[] = "World";
strncat(str1, str2, 2);
printf("%s", str1);
return 0;
}
Explanation: The code concatenates the first 2 characters of the string “World” to the end of the string “Hello” using the `strncat()` function, resulting in “HelloW” which is then printed using `printf()`.
#include <stdio.h>
int main() {
char str1[] = "Programming";
char str2[] = "Programming";
int result = strcmp(str1, str2);
printf("%d", result);
return 0;
}
Explanation: The code compares the strings “Programming” and “Programming” using the `strcmp()` function, which returns 0 because both strings are identical.
#include <stdio.h>
int main() {
char str[] = "Hello";
char* ptr = strstr(str, "abc");
if (ptr == NULL)
printf("Substring not found");
else
printf("Substring found at position: %ld", ptr - str);
return 0;
}
Explanation: The code searches for the substring “abc” in the string “Hello” using the `strstr()` function. Since “abc” is not present in “Hello”, `ptr` will be NULL, and “Substring not found” will be printed.
#include <stdio.h>
int main() {
char str[] = "Hello";
char* ptr = strchr(str, '\0');
printf("%ld", ptr - str);
return 0;
}
Explanation: The code searches for the null character (‘\0’) in the string “Hello” using the `strchr()` function. Since ‘\0’ marks the end of the string, `ptr` will point to the null character, and the difference between `ptr` and the start of the string (`str`) will be 5.
#include <stdio.h>
int main() {
char str[] = "Hello";
printf("%d", (int)strlen(str));
return 0;
}
Explanation: The code uses the `strlen()` function to find the length of the string “Hello”, which is 5 characters. The `%d` format specifier in `printf()` is used to print an integer value.