C Program This is a C program using Strings, arrays(Only declare, MUST USE PURE
ID: 3690271 • Letter: C
Question
C Program
This is a C program using Strings, arrays(Only declare, MUST USE PURE POINTER NOTATION TO FILL ALL ARRAYS) and functions.
1. Code a function to generate a string (s1) which contains a random pattern of UPPER case letters 'A' - 'Z' (Length of string should always be 41 characters including a '' (null) character to end the string).
2. Code a function to allow the user to enter a string (s2 - minimum length 2 characters, max 20, must be validated to be smaller than s1 or return an error message) containing a random pattern of UPPER case letters A-Z and then a replacement character , c.
3. Code a user defined function, (strfilter(s1,s2,c)) that searches s1 and replaces any occurance of any of the characters contained in s2 with the character in c.
4. Output the generated string, the user entered string and the filtered string from the main program.
5. After output allow the user to enter another string if they chose and repeat the process (s1 should only be randomy generated once regardless of repeat or not. s2 and c are user generated for each repeat).
Exampe of output after user input:
s1 = {'FJKLSIOEFNAALKJVOIUPAWENFKLAALKYUFGIOEAPVNOA"}
s2 = {"AL"}
c = {"*"}
filtered s1 = {"FJK*SIOEFN***KJVOIUP*WENFK****KYUFGIOE*PVNO*"}
Things to Note:
1. After completion allow the user to enter yes or no (Y/N) to repeat the above process.
2. to generate the random Pattern for S1 use the function RAND. S1 should be generated only once regardless of if the user wants to repeat or not. if user repeats then the next session uses the same s1 as the last session.
3. String must terminate with '' (maximum for s1 includes this null character)
4. MUST use pure pointer notation to fill arrays.
5. Arrays can only be declared, everything else is done through pure pointer notation.
Explanation / Answer
#include<stdio.h>
#define SIZE 10 /*The number of record is 10*/
struct name{
char first[30];
char last[30];
};
struct datehire{
char day[2];
char month[2];
char year[4];
};
struct employee{
struct name allname;
char id[20];
char salary[20];
struct datehire alldate;
};
typedef struct employee Employee;
void choice();
void addrecord(Employee *);
void changerecord(Employee *);
void loada(Employee *);
void loadall(Employee *);
main(void){
choice();
return 0;
}
void choice(){ /*choice function that used to led the user to choice the function*/
Employee employee_list[SIZE];
int choice;
int rubbish;
do{
printf(" <--------Welcome to employee database system--------> ");
printf(" Please choose the number of function that you want-> ");
printf(" <1>Add Record/Records ");
printf(" <2>Change a Record ");
printf(" <3>Load A record to screen ");
printf(" <4>Load ALL record to screen ");
printf(" <5>Exit ");
printf(" Please enter:->");
scanf("%d",&choice);
switch(choice){
case 1:
addrecord(employee_list);
break;
case 2:
changerecord(employee_list);
break;
case 3:
loada(employee_list);
break;
case 4:
loadall(employee_list);
break;
case 5:
printf("Exited,thanks you to using our system! ");
printf("press any key and then press the enter to exit ");
scanf("%d",&rubbish);
break;
default:
printf("Error input!Please input the number between 1-5");
scanf("%d",&rubbish);
}
}while (choice!=5);
}
void addrecord(Employee *employee_list){/*add a record*/
int count=0;
int display=0;
printf("Enter employee name(First name)->");
scanf("%s",employee_list[count].allname.first);
printf("Enter employee name(Last name)->");
scanf("%s",employee_list[count].allname.last);
printf("Enter the the employee id->");
scanf("%s",employee_list[count].id);
printf("Enter the employee salary->");
scanf("%s",employee_list[count].salary);
printf("Please enter the date of hire->");
scanf("%s%s%s",&employee_list[count].alldate.day,&employee_list[count].alldate.month,&employee_list[count].alldate.year);
printf(" ");
printf("The record [%d] employee first name -> %s ",display+1,employee_list[count].allname.first);
printf("The record [%d] employee last name -> %s ",display+1,employee_list[count].allname.last);
printf("The record [%d] employee id ->%s ",display+1,employee_list[count].id);
printf("The record [%d] employee salary -> %s ",display+1,employee_list[count].salary);
printf("The record [%d] employee hire of date-> %s/%s/%s ",display+1,employee_list[count].alldate.day,employee_list[count].alldate.month,employee_list[count].alldate.year);
printf(" ");
count=count+1;
display=display+1;
}
void changerecord(Employee *employee_list){ /*change the existed record*/
int choice1;
char check;
printf("What record do you want to change?Enter the number of record please->");
scanf("%d",&choice1);
printf("You want to change record %d ",choice1);
--choice1;
printf("Enter employee name(First name) to change the orignal record: ");
scanf("%s",employee_list[choice1].allname.first);
printf("Enter employee name(Last name) to change the orignal: ");
scanf("%s",employee_list[choice1].allname.last);
printf("Enter the the employee id to change the orignal ");
scanf("%s",employee_list[choice1].id);
printf("Enter the employee salary to change the orignal ");
scanf("%s",employee_list[choice1].salary);
printf("Please enter the date of hire ");
scanf("%s%s%s",&employee_list[choice1].alldate.day,&employee_list[choice1].alldate.month,&employee_list[choice1].alldate.year);
printf("The orignal have changed,Do you want to check it?Y or N ");
scanf("%s",&check);
if((check=='Y')||(check=='y')){
printf("The record [%d] first name-> %s ",choice1+1,employee_list[choice1].allname.first);
printf("The record [%d] last name-> %s ",choice1+1,employee_list[choice1].allname.last);
printf("The record [%d] id-> %s ",choice1+1,employee_list[choice1].id);
printf("The record [%d] salary-> %s ",choice1+1,employee_list[choice1].salary);
printf("The record [%d] date of hire-> %s/%s/%s ",choice1+1,employee_list[choice1].alldate.day,employee_list[choice1].alldate.month,employee_list[choice1].alldate.year);
printf("End of change record");
printf(" ");
}
}
void loada(Employee *employee_list)
{
int choice1;
printf("What record do you want to load to screen?Enter the number of record please->");
scanf("%d",&choice1);
choice1-1;
printf("%s ",employee_list[choice1].allname.first);
printf("%s ",employee_list[choice1].allname.last);
printf("%s ",employee_list[choice1].id);
printf("%s ",employee_list[choice1].salary);
printf("%s/%s/%s ",employee_list[choice1].alldate.day,employee_list[choice1].alldate.month,employee_list[choice1].alldate.year);
}
void loadall(Employee *employee_list){
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.