In: Computer Science
**Text file**
Kmart, 1450 Summit Avenue, Oconomowoc WI
Sears, 2050 Southgate Rd, Colorado Spgs CO
-----------------------------------------------------------------
Write a function, void getName(char strName[], const char strLine[]), that accepts a string (strLine) encoded in the same format as a line of code in the example file above, and returns the name in strName.
Write a function, void getAddress(char strAddress[], const char strLine[]), that accepts a string (strLine) encoded in the same format as a line of code in the example file above, and returns the address in strAddress.
Write a function, void getCity(char strCity[], const char strLine[]), that accepts a string (strLine) encoded in the same format as a line of code in the example file above, and returns the city in strCity.
Write a function, void getState(char strState[], const char strLine[]), that accepts a string (strLine) encoded in the same format as a line of code in the example file above, and returns the 2-character state abbreviation in strState.
*Please no hard-coded
* C language
ANSWER :
I have included my code and screenshots in this answer. In case, there is any indentation issue due to editor, then please refer to code screenshots to avoid confusion.
--------------------he4_fns.c--------------------
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void getName(char strName[], const char strLine[]);
//declarations
void getAddress(char strAddress[], const char strLine[]);
void getCity(char strCity[], const char strLine[]);
void getState(char strState[], const char strLine[]);
int main()
{
FILE *inpfile;
inpfile = fopen("input.txt","r"); //open the input file
char line[150];
char name[20], address[100], city[50], state[5]; //to store the
data which is read
while(fgets(line, 1000, inpfile)) //read input file line by
line
{
getName(name, line); //extract name
getAddress(address, line); //extract address
getCity(city, line); //extract city
getState(state, line); //extract state
printf("\n Name = %s", name); //print data
printf("\n Address = %s", address);
printf("\n City = %s", city);
printf("\n State = %s\n", state);
}
return 0;
}
void getName(char strName[], const char strLine[]) //extracts
name from line
{
char line[150];
strcpy(line, strLine); //copy the strLine into line
char *word = strtok( line, ","); //read first word until ',' is
encountered
strcpy(strName, word); //copy word into strName
}
void getAddress(char strAddress[], const char strLine[])
//extracts address from line
{
char line[150];
strcpy(line, strLine); //copy the strLine into line
char *word = strtok( line, ","); //read first word until ',' is
encountered
word = strtok(NULL, ","); //read second word until another ',' is
encountered
strcpy(strAddress, word); //second word contains address
}
void getCity(char strCity[], const char strLine[]) //extracts
city from line
{
char line[150];
strcpy(line, strLine); //copy the strLine into line
char *word = strtok( line, ","); //read first word until ',' is
encountered
word = strtok(NULL, ","); //read second word until another ',' is
encountered
word = strtok(NULL, "\n"); //read second word until '\n' is
encountered
strcpy(strCity, word);
strCity[strlen(strCity)-3] = '\0'; //remove state code from
city
}
void getState(char strState[], const char strLine[]) //extracts
state from line
{
if(strLine[strlen(strLine)-1] == '\n')
{
strState[0] = strLine[strlen(strLine)-3]; //the last 2 chars in
line is state code
strState[1] = strLine[strlen(strLine)-2];
strState[2] = '\0';
}
else
{
strState[0] = strLine[strlen(strLine)-2];
strState[1] = strLine[strlen(strLine)-1];
strState[2] = '\0';
}
}
--------------------Screenshots he4_fns.c---------------
Hope it helps... please give an upvote. it's very important to me... thank you:)