He Ha
He Ha
Due date of
submission: Date of Submission:
Assessment Rubrics
Experimental Result 30
Viva questions 10
Late Submission 10
1
Activity Sheet-5
STRINGS
AIM:
1.Write a c proggrame to sort list of names in alphabetical order.
PROGRAM ANALYSIS:
1. The program takes a list of names as input from the user and stores them in a
2D character array. It then sorts the names in alphabetical order using the
Bubble Sort algorithm, where each name is compared using the function, and
swapping is done using if necessary. After sorting, the program displays the
names in ascending order. The sorting process has a time complexity of O(n²),
making it efficient for small datasets but less optimal for larger lists. The
program effectively demonstrates string manipulation and sorting techniques in
C.
2
an alphabetic character. If the username passes all these checks, it is considered
valid; otherwise, it is deemed invalid. The program uses the standard library
functions isalnum and isalpha to perform character validation efficiently. This
ensures the program meets all the required username validation conditions.
3. The program begins by reading the user’s input password. It first checks
whether the password is at least 8 characters long. Next, the program iterates
through each character to verify the presence of at least one uppercase letter,
one lowercase letter, one digit, and one special character from the set
(!@#$%^&*). If all these conditions are satisfied, the password is evaluated as
strong; otherwise, it is considered weak. This step-by-step analysis ensures that
all essential security criteria are met for robust password creation.
ALGORITHM:
a. Algorithm to Sort Names Alphabetically in C
b. Start
c. Take input for the number of names (N) from the user
d. Declare a 2D character array to store N names
e. Take input for each name and store it in the array
f. Use a sorting algorithm (Bubble Sort or any other)
g. Compare two adjacent names using strcmp()
h. If they are in the wrong order, swap them using strcpy()
i. Repeat until all names are sorted in alphabetical order
i. Display the sorted list of names
j. End
2.
a. Start the program.
b. Input: User enters John123.
c. Length check: strlen("John123") = 8 (valid, between 6 and 15 characters).
d. Character validation: J, o, h, n, 1, 2, 3 are all alphanumeric characters.
e. First character check: The first character J is a letter.
3
f. Since all conditions are satisfied, "Valid username" is printed.
g. End the program.
3.
a. start the program.
b. input the password from the user.
c. check if the password is at least 8 characters long.
d. if the password is shorter than 8 characters, output "weak password" and end
the program.
e. initialize flags for uppercase, lowercase, digit, and special character.
f. loop through each character in the password.
g. check if the character is an uppercase letter and update the uppercase flag if
true.
h. check if the character is a lowercase letter and update the lowercase flag if true.
i. check if the character is a digit and update the digit flag if true.
j. check if the character is one of the special characters (!@#$%^&*) and update
the special flag if true.
k. after processing all characters, verify that all flags are true.
l. if all flags are true, output "strong password"; otherwise, output "weak
password."
m. end the program.
CODE:
1.#include <stdio.h>
4
while (names[i][k] == names[j][k] && names[i][k] != '\0' &&
names[j][k] != '\0') {
k++; }
if (names[i][k] > names[j][k]) {
for (int l = 0; l < 50; l++) {
temp[l] = names[i][l];
names[i][l] = names[j][l];
names[j][l] = temp[l];
} } } }}
int main() {
int n;
char names[10][50];
printf("Enter number of names (max 10): ");
scanf("%d", &n);
getchar(); // Consume newline character
printf("Enter names:\n");
for (int i = 0; i < n; i++) {
scanf("%s", names[i]);
}
sortNames(names, n);
printf("\nSorted names:\n");
for (int i = 0; i < n; i++) {
printf("%s\n", names[i]);
}
return 0;
}
2.
#include <stdio.h>
#include <ctype.h>
#include <string.h>
5
int isValidUsername(char *username) {
int length = strlen(username);
if (length < 6 || length > 15) {
return 0; }
if (!isalpha(username[0])) {
return 0; }
for (int i = 0; i < length; i++) {
if (!isalnum(username[i])) {
return 0;
}} return 1;}
int main() {
char username[20];
printf("Enter a username: ");
scanf("%s", username);
if (isValidUsername(username)) {
printf("Valid username!\n");
} else {
printf("Invalid username!\n");
} return 0;}
3. #include <stdio.h>
#include <string.h>
#include <ctype.h>
int strongPassword(char *password) {
int length = strlen(password);
int upper = 0, lower = 0, digit = 0, special = 0;
char specialCharacters[] = "!@#$%^&*";
if (length < 8) {
return 0;}
for (int i = 0; i < length; i++) {
6
if (isupper(password[i])) upper = 1;
if (islower(password[i])) lower = 1;
if (isdigit(password[i])) digit = 1;
if (strchr(specialCharacters, password[i])) special = 1; }
return upper && lower && digit && special;}
int main() {
char password[50];
printf("Enter a password: ");
scanf("%s", password);
if (strongPassword(password)) {
printf("Strong password!\n");
} else {
printf("Weak password!\n");
}
return 0;}
RESULTS:-
1.
2.
7
3.