0% found this document useful (0 votes)
22 views9 pages

Shannu

The document outlines a laboratory assignment for a Computer Programming course, focusing on string manipulation in C. It includes tasks for sorting names, validating usernames, and evaluating password strength, along with assessment rubrics and sample code implementations. The results and inferences highlight the effectiveness of the programs in demonstrating string handling and validation techniques.

Uploaded by

ponnapalliharish
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views9 pages

Shannu

The document outlines a laboratory assignment for a Computer Programming course, focusing on string manipulation in C. It includes tasks for sorting names, validating usernames, and evaluating password strength, along with assessment rubrics and sample code implementations. The results and inferences highlight the effectiveness of the programs in demonstrating string handling and validation techniques.

Uploaded by

ponnapalliharish
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

Department of Electronics and Communication Engineering

23CCE182 - Computer Programming Laboratory


STRINGS

Nam e of the Student K.SHANMUKHA REDDY

Registration Number CH.EN.U4CCE24042

Due date of
Date of Submission:
submission:

Assessment Rubrics

Description Mark Allotted Mark


Secured
Aim and Problem analysis 20

Code and Algorithm 40


Experimental Result 30

Viva questions 10

Late Submission 10

Total Marks 100

Signature of Lab in-charge


Activity Sheet-5

1
STRINGS

AIM:
1.Write a c proggrame to sort list of names in alphabetical order.

2.Write a c programme to validate a username based on the following rules


a. User must have 6-15 characters
b. It only contains (a-z,A-Z,0-9)
c. Start with a letter.

3.Write a program to evaluate the strength of a password based on the following


criteria:
a. At least 8 characters long.
b.Contains at least one uppercase letter.
c.Contains at least one lowercase letter.
d.Contains at least one digit.
e.Contains at least one special character (!@#$%^&*).

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.The provided C program validates a username based on three specific rules.


First, it checks if the length of the username is between 6 and 15 characters.
Second, it verifies that the username contains only alphanumeric characters
(letters and numbers). Lastly, the program ensures that the username starts with
an alphabetic character. If the username passes all these checks, it is considered

2
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.

APPARTUS REQUIRED: Turbo C++

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.
f. Since all conditions are satisfied, "Valid username" is printed.

3
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>

void sortNames(char names[][50], int n) {


char temp[50];
for (int i = 0; i < n - 1; i++) {
for (int j = i + 1; j < n; j++) {
int k = 0;
while (names[i][k] == names[j][k] && names[i][k] != '\0' &&
names[j][k] != '\0') {

4
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>

int isValidUsername(char *username) {

5
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++) {
if (isupper(password[i])) upper = 1;
if (islower(password[i])) lower = 1;

6
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.

3.

7
INFERENECE:
1.This program successfully sorts a list of names alphabetically using a manual
character-by-character comparison. It demonstrates the basics of string
manipulation, sorting techniques, and array handling in C.

2. This program effectively enforces username validation rules to ensure


usernames are within a proper format. By checking length, first character, and
allowed characters, it helps maintain security and consistency in username
creation. The implementation is simple and efficient for basic validation.

3. The program correctly identifies whether a password is strong based on the


specified criteria: length, uppercase, lowercase, digit, and special character.
The program could handle spaces in the password more effectively by trimming
or notifying the user of invalid characters.

8
9

You might also like