Below is a code to that allows you to enter the first and last names of students
ID: 3828297 • Letter: B
Question
Below is a code to that allows you to enter the first and last names of students to generate email addresses for each name that is input. I need help to adjust this code to be able to have deleting and sorting capabilities.
Executable code :
#include <stdio.h>
#include <string.h>
#include <malloc.h>
typedef struct student{
char first_name[30];
char last_name[30];
char email[50];
int id;
}student;
student *enter(){
int i;
student *students = (student *) malloc(3 * sizeof(student));
char domain[] = "@domain.com";
for(i = 0; i < 3; ++i){
printf("Enter student %d details ", i+1);
printf("First name: ");
scanf("%s", students[i].first_name);
printf("Last name: ");
scanf("%s", students[i].last_name);
students[i].email[0] = 0;
strcat(students[i].email, students[i].first_name);
strcat(students[i].email, students[i].last_name);
strcat(students[i].email, domain);
students[i].id = i + 1;
}
return students;
}
void search(student *students){
char name[30];
int i;
printf("Enter last name to search: ");
scanf("%s", name);
for(i = 0; i < 3; ++i){
if(strcmp(students[i].last_name, name) == 0){
printf("First name: %s ", students[i].first_name);
printf("Email: %s ", students[i].email);
printf("Id: %d ", students[i].id);
}
}
}
int main(){
student *students = enter();
search(students);
return 0;
}
Explanation / Answer
int SortFunc(void* first_name, void* last_name) {
id *p1 = (id*)first_name;
id *p2 = (id*)last_name;
return strcmp(p1->last_name, p2->last_name);
}
void Sort (id student[]) {
qsort(student, counter, sizeof(id), &SortFunc);
}
void DeleteEntry (id student[])
{
int x = 0;
char deleteStudent[30];
char nullStr[30] = {""};
printf(" Enter name: ");
scanf("%s", deleteName); //place into temp string
for (x = 0; x < counter; x++)
{
if (strcmp(deleteStudent, student[x].Student) == 0)
{
for (x = 0; x < counter; x++)
{
if (strcmp(deleteStudent, student[x].last_name) == 0)
{
strcpy(student[x].first_name, nullStr);
printf("Contact removed from phonebook. ");
counter--;
break;
}
}
}
else printf("Invalid entry--try again. ");
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.