This Study Resource Was
This Study Resource Was
Write a C-program that helps user managing a list of 100 student names using the
following menu:
1- Add a student
2- Remove a student
3- Search a student
4- Print the list in ascending order
5- Quit
*/
#include <stdio.h>
#include <windows.h>
#include <string.h>
#include <ctype.h>
#define MAXN 100
#define MAXCHOICE 5
m
er as
if (i>0) strcpy(&s[0],&s[i]);
return s;
co
}
eH w
char* rTrim (char s[])
o.
{
int i=strlen(s)-1; rs e
ou urc
while (s[i]==' ') i--;
s[i+1]='\0';
return s;
}
o
aC s
{
rTrim(lTrim(s));
char *ptr=strstr(s, " ");
while (ptr!=NULL)
{
ed d
int i;
for (i=0;i<L;i++)
if (i==0||i>0 && (s[i-1]==' ')) s[i] = toupper (s[i]);
return s;
This study source was downloaded by 100000826901668 from CourseHero.com on 06-30-2021 08:30:29 GMT -05:00
https://www.coursehero.com/file/55825628/WS7-1cpp/
char getUserChoice()
{
int c;
printf("1-Add a student\n");
printf("2-Remove a student\n");
printf("3-Search a student\n");
printf("4-Print the list in ascending order\n");
printf("5-Quit\n");
printf("Choice = ");
fflush(stdin);
scanf("%c", &c);
return c;
}
m
er as
}
co
void add(char list[MAXN][21], int *pn) {
eH w
char hs[21];
int i, existed;
o.
do {
rs e
printf("Add a student : ");
ou urc
fflush(stdin);
scanf("%20[^\n]", hs);
existed = 1;
for ( i = 0; i < *pn; i++)
o
if (strcmp(hs,list[i]) == 0)
aC s
{
vi y re
printf("Name existed!Retype!\n");
existed = 0;
i = *pn - 1;
}
} while (!existed);
ed d
strcpy(list[*pn], hs);
ar stu
(*pn)++;
printf("Added!\n");
system("pause");
system("cls");
is
}
Th
fflush(stdin);
scanf("%20[^\n]", &hs);
for (i = 0; i < *pn; i++)
{
nameStr(hs);
nameStr(list[i]);
char * ptr = strstr(list[i], hs);
if (ptr != '\0') printf("RESULT : Name[%d] : %s\n", i, list[i]);
This study source was downloaded by 100000826901668 from CourseHero.com on 06-30-2021 08:30:29 GMT -05:00
https://www.coursehero.com/file/55825628/WS7-1cpp/
}
}
void removed(char list[MAXN][21], int *pn)
{
search(list, pn);
printf("Which Name you want to removed?(input a number) : ");
int del, i;
scanf("%d", &del);
if (del >= 0 && del < *pn) {
for (i = del + 1; i < *pn; i++)
strcpy(list[i-1], list[i]);
printf("Removed!\n");
(*pn)--;
} else printf("UnRemoved!\n");
system("pause");
system("cls");
}
void print(char list[MAXN][21], int *pn)
{
int i, j;
for (i = 0 ; i < *pn-1; i++)
m
er as
for (j = *pn-1; j > i; j--)
if (strcmp(list[j] , list[j-1]) < 0)
co
{
eH w
char t[21];
strcpy(t, list[j]);
o.
strcpy(list[j], list[j-1]);
rs e
strcpy(list[j-1], t);
ou urc
}
for (i = 0; i < (*pn); i++)
{
nameStr(list[i]);
o
}
vi y re
system("pause");
system("cls");
}
void halt()
{
ed d
main()
is
{
char userChoice;
Th
char list[MAXN][21];
int n = 0;
do
{
userChoice = getUserChoice();
sh
switch(userChoice)
{
case '1':
if (isFull(list, &n)) printf("Impossible to add!\n");
else add(list, &n);
break;
case '2':
if (isEmpty(list, &n)) printf("Impossible to remove!\n");
This study source was downloaded by 100000826901668 from CourseHero.com on 06-30-2021 08:30:29 GMT -05:00
https://www.coursehero.com/file/55825628/WS7-1cpp/
else removed(list, &n);break;
case '3':
if (isEmpty(list, &n)) printf("Nothing to search!\n");
else search(list, &n);
system("pause");
system("cls");
break;
case '4':
if (isEmpty(list, &n)) printf("Nothing to print!\n");
else print(list, &n);
break;
case '5':
halt();
break;
}
if (userChoice < '1' || userChoice >'5') printf("1 to 5 only!\n");
} while (userChoice != MAXCHOICE);
}
m
er as
co
eH w
o.
rs e
ou urc
o
aC s
vi y re
ed d
ar stu
is
Th
sh
This study source was downloaded by 100000826901668 from CourseHero.com on 06-30-2021 08:30:29 GMT -05:00
https://www.coursehero.com/file/55825628/WS7-1cpp/
Powered by TCPDF (www.tcpdf.org)