Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Given the string variables pres , first , and last char *last, *first, *middle;

ID: 3694997 • Letter: G

Question

Given the string variables pres , first , and last char *last, *first, *middle; char pres[20] = "Adams, John Quincy"; char pres_copy[20]; strcpy(pres_copy, pres); last = strtok(pres_copy, ", "); first = strtok(NULL, ", "); middle = strtok(NULL, ", "); show what would be displayed by this code fragment. strncpy(first, pres, 2); first[2] = ''; printf("%s", first); printf(" %s", strcpy(last, &pres[7])); strncpy(first, &pres[7], 2); first[2] = ''; strncpy(last, &pres[14], 2); last[2] = ''; printf(" %s%s ", first, last);

Explanation / Answer

//Given the string variables
//pres , first , and last
#include <stdio.h>
#include <string.h>
int main()
{
char *last, *first, *middle;
char pres[20] = "Adams, John Quincy"; //pres holds the string.
char pres_copy[20]; //pres_copy is also a character array.
strcpy(pres_copy, pres); //The string in pres will be copied to pres_copy.
last = strtok(pres_copy, ", "); //The string that appears before a comma(,) will be tokenized and assigned to last. So, last = "Adams"
first = strtok(NULL, ", "); //Reads the next token from the same string into first. So, first = "John"
middle = strtok(NULL, ", "); //Reads the next token from the same string into middle. So, middle = "Quincy"
//show what would be displayed by this code fragment.
strncpy(first, pres, 2); //Copies atmost 2 characters from pres into first. So, first will now hold, "Ad"
first[2] = '';    //first[2] will be assigned a null terminator.
printf("%s", first); //So, this string will print "Ad".
printf(" %s", strcpy(last, &pres[7])); //Copies the string pres, from 8th character into last. So, last will now hold, "John Quincy"
strncpy(first, &pres[7], 2); //Copies atmost 2 characters from the same kind of previous string into first. So, now first will hold "Jo"
first[2] = '';    //Makes first terminate with a null character.
strncpy(last, &pres[14], 2);    //Copies atmost 2 characters from from 15th character of pres i.e., "in" into last.
last[2] = ''; //Makes last terminate with a null character.
printf(" %s%s ", first, last); //Prints first and last together. So, the output of this string is: "Join"
}

So, the output of this code is: Ad John Quincy Join

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote