Quiz-Nov.23-2023

pdf

School

Humber College *

*We aren’t endorsed by this school

Course

IPC144

Subject

Computer Science

Date

Dec 6, 2023

Type

pdf

Pages

10

Uploaded by DeaconStarGerbil23

Report
1- What will be the output of the following code? // C Program to illustrate the strcat function #include <stdio.h> int main() { char dest[50] = "This is an" ; char src[50] = " example" ; printf ( "dest Before: %s\n" , dest); // concatenating src at the end of dest strcat (dest, src); printf ( "dest After: %s" , dest); return 0; }
2- What will be the output of the following code? / C program to demonstrate the strlen() function #include <stdio.h> #include <string.h> int main() { // Declare and initialize a character array 'str' with // the string "GeeksforGeeks" char str[] = "GeeksforGeeks" ; // Calculate the length of the string using the strlen() // function and store it in the variable 'length' size_t length = strlen (str); // Print the length of the string printf ( "String: %s\n" , str); printf ( "Length: %zu\n" , length); return 0; } 3- What will be the output of the following code? int main() { // Define a string 'str1' and initialize it with "Geeks" char str1[] = "Geeks" ; // Define a string 'str2' and initialize it with "For" char str2[] = "For" ; // Define a string 'str3' and initialize it with "Geeks" char str3[] = "Geeks" ; // Compare 'str1' and 'str2' using strcmp() function and // store the result in 'result1' int result1 = strcmp (str1, str2); // Compare 'str2' and 'str3' using strcmp() function and // store the result in 'result2' int result2 = strcmp (str2, str3); // Compare 'str1' and 'str1' using strcmp() function and // store the result in 'result3' int result3 = strcmp (str1, str1);
// Print the result of the comparison between 'str1' and // 'str2' printf ( "Comparison of str1 and str2: %d\n" , result1); // Print the result of the comparison between 'str2' and // 'str3' printf ( "Comparison of str2 and str3: %d\n" , result2); // Print the result of the comparison between 'str1' and // 'str1' printf ( "Comparison of str1 and str1: %d\n" , result3); return 0; }
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
4- What will be the output of the following code? // C program to illustrate the use of strcpy() #include <stdio.h> #include <string.h> int main() { // defining strings char source[] = "GeeksforGeeks" ; char dest[20]; // Copying the source string to dest strcpy (dest, source); // printing result printf ( "Source: %s\n" , source); printf ( "Destination: %s\n" , dest); return 0; }
5- What will be the output of the following code? #include <stdio.h> #include <string.h> int main() { char str[] = "GeeksforGeeks" ; char ch = 'e' ; // Search for the character 'e' in the string // Use the strchr function to find the first occurrence // of 'e' in the string char * result = strchr (str, ch); // Character 'e' is found, calculate the index by // subtracting the result pointer from the str pointer if (result != NULL) { printf ( "The character '%c' is found at index %ld\n" , ch, result - str); } else { printf ( "The character '%c' is not found in the " "string\n" , ch); } return 0; }
6- What will be the output of the following code? // C program to demonstrate the strstr() function #include <stdio.h> #include <string.h> int main() { // Define a string 's1' and initialize it with // "GeeksforGeeks" char s1[] = "GeeksforGeeks" ; // Define a string 's2' and initialize it with "for" char s2[] = "for" ; // Declare a pointer 'result' to store the result of // strstr() char * result; // Find the first occurrence of 's2' within 's1' using // strstr() function and assign the result to 'result' result = strstr (s1, s2); if (result != NULL) { // If 'result' is not NULL, it means the substring // was found, so print it printf ( "Substring found: %s\n" , result); } else { // If 'result' is NULL, it means the substring was // not found, so print appropriate message printf ( "Substring not found.\n" ); } return 0; }
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
7- What will be the output of the following code? // C program to demonstrate the strstr() function #include <stdio.h> #include <string.h> int main() { // Define a string 's1' and initialize it with // "GeeksforGeeks" char s1[] = "GeeksforGeeks" ; // Define a string 's2' and initialize it with "for" char s2[] = "for" ; // Declare a pointer 'result' to store the result of // strstr() char * result; // Find the first occurrence of 's2' within 's1' using // strstr() function and assign the result to 'result' result = strstr (s1, s2); if (result != NULL) { // If 'result' is not NULL, it means the substring // was found, so print it printf ( "Substring found: %s\n" , result); } else { // If 'result' is NULL, it means the substring was // not found, so print appropriate message printf ( "Substring not found.\n" ); } return 0; }
8- What will be the output of the following code? // C program to demonstrate the strtok() function #include <stdio.h> #include <string.h> int main() { char str[] = "Geeks,for.Geeks" ; // Delimiters: space, comma, dot, // exclamation mark const char delimiters[] = ",." ; // Tokenize the string char * token = strtok (str, delimiters); while (token != NULL) { printf ( "Token: %s\n" , token); token = strtok (NULL, delimiters); } return 0; }
9- What will be the output of the following code? #include <stdio.h> #include <string.h> int main () { char str[] = "This is an example for strtok"; char *token = strtok(str, " "); while (token != NULL) { printf("%s\n", token); token = strtok(NULL, " "); } return 0; }
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
10- What will be the output of the following code? #include <stdio.h> #include <string.h> int main() { char src[] = "This is an example for strncpy"; char dest[20]; strncpy(dest, src, 5); printf("The copied string is: %s\n", dest); return 0; }