0% found this document useful (0 votes)
18 views4 pages

C Program for Student List Management

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)
18 views4 pages

C Program for Student List Management

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

/*

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 [Link] on 06-30-2021 08:30:29 GMT -05:00

[Link]
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 [Link] on 06-30-2021 08:30:29 GMT -05:00

[Link]
}
}
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 [Link] on 06-30-2021 08:30:29 GMT -05:00

[Link]
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 [Link] on 06-30-2021 08:30:29 GMT -05:00

[Link]
Powered by TCPDF ([Link])

Common questions

Powered by AI

The program uses functions isFull and isEmpty to check list conditions before performing operations. isFull returns true if the list has reached the maximum number of entries (100), and isEmpty returns true if the list has no entries. These checks prevent invalid operations like adding when full or removing/searching/printing when empty, thus maintaining data integrity .

When removing a student, the program shifts all elements after the deletion point one position to the left. This effectively overwrites the deleted name and ensures that no gaps are left in the array, maintaining continuity in the list and minimizing memory fragmentation .

The program includes a check after the switch-case structure to ensure that the user input is within valid bounds (1-5). If an invalid input is detected, it prompts the user with a message '1 to 5 only!' to reenter a valid choice, thus reinforcing input validation and protecting against unintended operations .

Encapsulating functionalities into separate functions enhances modularity, making the program easier to understand, maintain, and debug. This separation allows for focused logic within each function, reducing complexity and improving readability. It also facilitates reusability of code, where each function can be tested independently, thereby promoting robust software development practices .

The program utilizes the function nameStr to format strings to title case, ensuring that the first letter and every letter following a space is capitalized. This ensures uniformity in storing and comparing names, thus avoiding discrepancies due to different cases during search and storage operations .

The hardcoded limit of 100 student names constrains the program's ability to manage large datasets, making it unsuitable for use in larger educational institutions with more students. This limitation requires users to keep track of the list size, and it restricts scalability unless the code is modified to use dynamic memory allocation or a larger fixed size .

The program uses the functions lTrim and rTrim to remove leading and trailing spaces from the student names. lTrim iterates over spaces at the beginning of the string and shifts characters left if necessary, while rTrim terminates the string at the last non-space character position .

The search functionality allows users to find names by checking for substrings in the stored names, leveraging the nameStr function to ensure consistent case formatting. However, its effectiveness is limited by its linear search nature, which is inefficient for large datasets, and its lack of advanced search capabilities, like regular expressions or partial matches, which could enhance user flexibility and speed .

The program implements a basic bubble sort algorithm to order student names in ascending order. It compares each pair of adjacent entries and swaps them if they are not in order, repeating this until the entire list is sorted, guaranteeing that all elements are in non-descending sequence by the end of execution .

The getUserChoice function displays a menu to the user with options to add, remove, search, print the list, or quit the program. It reads the user's choice, ensuring that only valid options (1-5) are processed in the subsequent switch-case logic to guide program execution according to user input .

You might also like