0% found this document useful (0 votes)
38 views

This Study Resource Was

The C program allows a user to manage a list of 100 student names using a menu with the following options: 1. Add a student 2. Remove a student 3. Search for a student 4. Print the list in ascending order 5. Quit the program The program uses arrays and string manipulation functions to store names, search the list, sort it, and remove items.

Uploaded by

Minh Lê Khải
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views

This Study Resource Was

The C program allows a user to manage a list of 100 student names using a menu with the following options: 1. Add a student 2. Remove a student 3. Search for a student 4. Print the list in ascending order 5. Quit the program The program uses arrays and string manipulation functions to store names, search the list, sort it, and remove items.

Uploaded by

Minh Lê Khải
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

/*

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

char* lTrim(char s[])


{
int i=0;
while (s[i]==' ') i++;

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

char* trim (char s[])


vi y re

{
rTrim(lTrim(s));
char *ptr=strstr(s, " ");
while (ptr!=NULL)
{
ed d

strcpy (ptr, ptr+1);


ar stu

ptr = strstr(s," ");


}
return s;
}
is
Th

char* nameStr (char s[])


{
trim(s);
strlwr(s);
int L =strlen(s);
sh

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;
}

int isFull(char list[MAXN][21], int *pn) {


return ((*pn) == MAXN);
}

int isEmpty(char list[MAXN][21], int *pn) {


return ((*pn) == 0);

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

void search(char list[MAXN][21], int *pn)


{
int i;
printf("Searching for : ");
char hs[21];
sh

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

printf("Name[%d] : %s \n", i, list[i]);


aC s

}
vi y re

system("pause");
system("cls");
}
void halt()
{
ed d

printf("This program coded by Duong\n");


ar stu

printf("Thank you for watching\n");


}

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)

You might also like