Complete the code to compare two strings using the correct function.
#include <stdio.h> #include <string.h> int main() { char str1[] = "apple"; char str2[] = "apple"; if ([1](str1, str2) == 0) { printf("Strings are equal.\n"); } else { printf("Strings are not equal.\n"); } return 0; }
The strcmp function compares two strings and returns 0 if they are equal.
Complete the code to check if two strings are different.
#include <stdio.h> #include <string.h> int main() { char a[] = "cat"; char b[] = "dog"; if ([1](a, b) != 0) { printf("Strings are different.\n"); } else { printf("Strings are the same.\n"); } return 0; }
strcmp returns a non-zero value when strings differ.
Fix the error in the code to correctly compare strings ignoring case.
#include <stdio.h> #include <string.h> #include <ctype.h> int main() { char s1[] = "Hello"; char s2[] = "hello"; if ([1](s1, s2) == 0) { printf("Strings are equal ignoring case.\n"); } else { printf("Strings are different.\n"); } return 0; }
strcasecmp compares strings ignoring case differences.
Fill both blanks to create a dictionary-like structure that maps words to their lengths if length is greater than 3.
#include <stdio.h> #include <string.h> int main() { char *words[] = {"apple", "cat", "banana", "dog"}; int lengths[4]; for (int i = 0; i < 4; i++) { if ([1](words[i]) [2] 3) { lengths[i] = strlen(words[i]); printf("%s: %d\n", words[i], lengths[i]); } } return 0; }
Use strlen to get the length of the word and check if it is greater than 3.
Fill all three blanks to create a filtered dictionary of words with length greater than 4, mapping uppercase words to their lengths.
#include <stdio.h> #include <string.h> #include <ctype.h> void to_upper(char *dest, const char *src) { int i = 0; while (src[i]) { dest[i] = [1](src[i]); i++; } dest[i] = '\0'; } int main() { char *words[] = {"apple", "cat", "banana", "dog"}; char upper[20]; int lengths[4]; for (int i = 0; i < 4; i++) { if (strlen(words[i]) [2] 4) { to_upper(upper, words[i]); lengths[i] = strlen(words[i]); printf("%s: %d\n", [3], lengths[i]); } } return 0; }
Use toupper to convert characters to uppercase, check if length is greater than 4, and print the uppercase word.