STRING FUNCTIONS - WORKSHEET 1 - KEY

Consider the following declarations.

char	name[25];
char	street[25];
char	city[15];
char	state[?];
char	fullname[?];
char	address[?];
  1. What size does the array, state[], need to be in ordered to hold the standard 2 letter abbreviation? 3, 2 letters and the null character

  2. What size does the array, address[], need to be in order to hold the data from city[] and state[] with a comma and one space in between? 19, 14 letters + comma + space + 2 letters + null character

  3. Assume the name[] array is formatted as, lastname, comma, space, firstname. (like Ross, Betsy) What size does fullname[] need to be in order to fit the data from name[], reformatted as first name, space, last name? 24

  4. Write code to rearrange the data from name[] into fullname[] so that it reads first name, space, last name. see below

  5. Write code to find the size of the string in city[]. size = strlen(city);

  6. Move data from city[] and state[] to address[] so there is a comma and one space in between. see below

  7. Assume the street[] array has an address like 1175 N. Main Street. Move the numeric part of the address (1175) to a char array variable called streetnum. Assume there will be a maximum of 5 digits. Declare the variable. see below

4)
char	*commaptr;
commaptr = strchr(name, ',');		find the comma
*commaptr = '\0';		Change comma to null character
				so last name portion is now a string.
strcpy(fullname, commaptr+2);	First name starts 2 past comma.
strcat(fullname," ");		For strcat to work, the space must be
                                a string, not a character.
strcat(fullname, name)
6) 
strcpy(address, city);
strcat(address, " ");		second string has one space
strcat(address, state);
7)
char	streetnum[6];
strncpy(streetnum, street, 5)	if you know there are 5 digits
	OR
ptr = strchr(street, ' ');	second argument is a character
*ptr = '\0';
strcpy(streetnum,street);