-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlaying with String
481 lines (387 loc) · 12.7 KB
/
Playing with String
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
#include<stdio.h>
#include<string.h>
#include<stdbool.h>
#define FAIL_VAL -99999
#define SUCCESS_VAL 99999
#define MAX_USER 20
#define MAX_LEN 200
char emailArr[MAX_USER][MAX_LEN];
char passArr[MAX_USER][MAX_LEN];
char nameArr[MAX_USER][MAX_LEN];
char addressArr[MAX_USER][MAX_LEN];
int user_count=0;
/* no 1 i have made these bool function because as given in the question. i have to do several checks
like my email contains @ or not my password should contains digit or not my password and emails are
valid or not so to do this task i have made these function there are different function
like which checks the different parameters and return a bool value means true or false */
/*here i have made a ischar which return a boolean value true or false
The work of this function is too check that character is a alphabet or not */
bool ischar(char c)
{
return ((c>='a' && c<='z'));
}
bool is_uppercase(char c)
{
return ((c>='A' && c<='Z'));
}
// here i have made a another boolean function which checks that the given mail is valid or not //
bool isvalidemail(char *s)
{
int pt=-1,at=-1,atcount=0;
/* if the first character of the given mail is not a alphabet then the mail is invalid
so i check this by using my function ischar */
if(!ischar(s[0]))
{
return 0;
}
/* if the first char of mail is alphabet then i will check the position of the @ and . in the given mail.
strlen( ) function counts the number of characters in a given string and returns the integer value.
It stops counting the character when null character is found. Because, null character indicates the end of the string in C */
for(int i=0;i<strlen(s);i++)
{
if(s[i]=='@')
{
at=i;
atcount++;
}
if(s[i]=='.')
{
pt=i;
}
if(is_uppercase(s[i]))
{
printf("\n\n\t All characters of your mail must be lowercase :) ");
return 0;
}
}
// if the position of the @ is greater then the . it means the email is invalid
// or if any of them @and . is not present in the mail then also the mail is invalid
// so to check this here i have write the if statement
if(at>pt || at==-1 || pt==-1 || atcount>1)
{
printf("\n\n\t Your mail must have contained one '@' and a dot '.' ");
printf("\n\n\t You have to write '@' sign before dot '.' sign.");
printf("\n\t Avoid '@' twice in your mail.");
printf("\n\t Avoid '.' sign as the last character of your mail.");
return 0;
}
//if the point . is placed at the end of our mail then also our mail id is incorrect so to check this one more if statement
// if statement is true then function will return 0 means false
if(s[pt]==s[strlen(s)-1])
{
printf("\n\n\t You can't write '.' sign as the last character of your mail.");
return 0;
}
// if everything is fine and okay and well place then the function will return the 1 value means true our mail is valid
return 1;
}
/*now same thing i have done here to check my password that it is in the correct format or not
so to check this i have to make several boolean function one of them is for that a character is a special character or not
second function is to check that character is uppercase character or not third one is to check that the character is lowercase or not
and last one is to check that a character is a digit or not */
bool isspecialchar(char c){
return ((c=='$' || c=='@' || c=='*' || c=='#'));
}
bool isuppercase(char c){
return ((c>='A' && c<='Z'));
}
bool islowercase(char c){
return ((c>='a' && c<='z'));
}
// here i have used the ASCII value of the numerical no like 0 have value 48 and 9 have 57
bool isdigit(char c){
return ((c>=48 && c<=57));//We are using ASCII value of the digit for "(char c)" type parameter
}
// now this function checks that my password is in correct manner or not
bool ispassword(char *s)
{
int digit=0,upc=0,lwc=0,spc=0;
// if password length is less then 8 then it will return 0
if(strlen(s)<8)
{
printf("\n\n\t Password should contain at least 8 characters. ");
return 0;
}
// else it will check the all parameters that all things are present or not
for(int i=0;i<strlen(s);i++)
{
if(isuppercase(s[i]))
{
upc++;
continue;
}
if(islowercase(s[i]))
{
lwc++;
continue;
}
if(isdigit(s[i]))
{
digit++;
continue;
}
if(isspecialchar(s[i]))
{
spc++;
continue;
}
}
// if any of the condition fails then it will return 0
if(lwc==0 || upc==0 || spc==0 || digit==0)
{
return 0;
}
// else return 1 means our password is in th correct format and valid
return 1;
}
// this function checks the duplicate entry
// means if a user sign up with already registered mail then it will create a problem for us in the s
// searching and the login
// so we discard the duplicate entries
bool iscopymail(char *s)
{
for(int i=0;i<user_count;i++)
{
if(!strcmp(s,emailArr[i]))
{
return 1;
}
}
return 0;
}
// so here is the sign up function
int signupUser(char email[], char pass[]){
//Write your code here
if(!isvalidemail(email))
{
printf("\n\n\t INVALID EMAIL FORMAT :) ");
printf("\n\t SIGNUP FAILED :) ");
return FAIL_VAL;
}
if(iscopymail(email))
{
printf("\n\n\t GIVEN MAIL IS ALREDAY REGISTER !!! ");
printf("\n\n\t PLEASE TRY WITH ANOTHER MAIL :) ");
printf("\n\n\t\t SIGNUP FAILED :) ");
return FAIL_VAL;
}
if(!ispassword(pass))
{
printf("\n\t PASSWORD IS NOT IN VALID FORMAT !!! ");
printf("\n\n\t The password must have contained both Uppercase and Lowercase letters,");
printf("\n\t Digits,Special characters like $ # * @ and at least 8 characters.");
printf("\n\n\t SIGNUP FAILED :) ");
return FAIL_VAL;
}
else
{
sprintf(emailArr[user_count],"%s",email);
sprintf(passArr[user_count],"%s",pass);
user_count++;
printf("\n\n\t SIGN UP SUCCESFULL !!! ");
}
return user_count;
}
// login f user email match with any of the register mail and the password match with corresponding mail
// then the login will successful
int loginUser(char email[], char pass[]){
//Write your code here
/*The strcmp() function is used to compare two strings two strings str1 and str2 .
If two strings are same then strcmp() returns 0 , otherwise, it returns a non-zero value.
This function compares strings character by character using ASCII value of the characters.*/
for(int i=0;i<user_count;i++)
{
if(!strcmp(email,emailArr[i]))
{
if(!strcmp(pass,passArr[i]))
{
printf("\n\n\t LOGIN SUCCESSFULL !!! ");
return i;
}
printf("\n\n\t INCORRECT PASSWORD :) ");
printf("\n\t LOGIN FAILED :) ");
return FAIL_VAL;
}
}
printf("\n\n\t Email is not registered or invalid mail :) ");
printf("\n\t LOGIN FAILED :) ");
return FAIL_VAL;
}
int buildUserProfile(char email[], char firstName[], char lastName[], char address[]){
//Write your code here
// here we find the mail if mail match
// with already register mail then data will update for that user
for(int i=0;i<user_count;i++)
{
if(!strcmp(email,emailArr[i]))
{
//sprintf() function is a file handling function in C programming language which is used to write formatted output to the string.
sprintf(addressArr[i],"%s",address);
sprintf(nameArr[i],"%s",firstName);
for(int k=strlen(firstName),j=0;j<=strlen(lastName);j++,k++)
{
if(j==0)
{
nameArr[i][k]=32; //ASCII value of 32 means a Space.
continue;
}
nameArr[i][k]=lastName[j-1];
}
printf("\n\n\t USER PROFILE SUCCESFULLY UPDATED !!! ");
return 1;
}
}
// if mail not found then it will return fail and show a error message
printf("\n\n\t Email not found :) ");
return FAIL_VAL;
}
// this is the search by name
// This case is an insensitive case means it allows to search with substring.
void searchByName(char name[]){
//Write your code here
int index[MAX_USER],count=0;
int a,n=0;
for(int i=0;i<user_count;i++)
{
a=0;
if(!strcmp(name,nameArr[i]))
{
count=1;
index[0]=i;
break;
}
for(int j=0;j<strlen(name);j++)
{
if(name[j]!=nameArr[i][j])
{
break;
}
a++;
}
if(a==strlen(name))
{
index[n]=i;
count++;
n++;
}
}
if(!count){
printf("\n\n\t NO user found with this name :) ");
return FAIL_VAL;}
printf("\n\n\t %d user found with this name :) ",count);
for(int i=0;i<count ;i++)
{
printf("\n\t %d :",i+1);
printf("\n\t EMAIL :- %s",emailArr[index[i]]);
printf("\n\t NAME :- %s",nameArr[index[i]]);
printf("\n\t ADRESS :- %s",addressArr[index[i]]);
printf("\n");
}
return SUCCESS_VAL;
}
void searchByAddress(char address[]){
//Write your code here
int index[MAX_USER],count=0;
int a,n=0;
for(int i=0;i<user_count;i++)
{
a=0;
if(!strcmp(address,addressArr[i]))
{
count=1;
index[0]=i;
break;
}
for(int j=0;j<strlen(address);j++)
{
if(address[j]!=addressArr[i][j])
{
break;
}
a++;
}
if(a==strlen(address))
{
index[n]=i;
count++;
n++;
}
}
if(!count){
printf("\n\n\t NO user found with this Address :) ");
return FAIL_VAL;}
printf("\n\n\t %d user found with this Address :) ",count);
for(int i=0;i<count ;i++)
{
printf("\n\t %d :",i+1);
printf("\n\t EMAIL :- %s",emailArr[index[i]]);
printf("\n\t NAME :- %s ",nameArr[index[i]]);
printf("\n\t ADRESS :- %s",addressArr[index[i]]);
printf("\n");
}
return SUCCESS_VAL;
}
int main()
{
while(1)
{
printf("*****************************************************************************************************\n");
printf("1. Sign Up User. 2. Login User. 3. Build User Profile. 4.Search By Name. 5. Search By Address. 6.Exit \n");
int choice;
scanf("%d",&choice);
if(choice==1){
char mail[MAX_LEN], pass[MAX_LEN];
printf("Give User Email :");
scanf("\n%[^\n]s",mail);
printf("Give User Password :");
scanf("\n%[^\n]s",pass);
signupUser(mail,pass);
}
if(choice==2){
char mail[MAX_LEN], pass[MAX_LEN];
printf("Give User Email :");
scanf("\n%[^\n]s",mail);
printf("Give User Password :");
scanf("\n%[^\n]s",pass);
loginUser(mail,pass);
}
if(choice==3){
char mail[MAX_LEN], fname[MAX_LEN], lname[MAX_LEN], address[MAX_LEN];
printf("Give User Email :");
scanf("\n%[^\n]s",mail);
printf("%s\n",mail);
printf("Give First Name :");
scanf("\n%[^\n]s",fname);
printf("%s\n",fname);
printf("Give Last Name :");
scanf("\n%[^\n]s",lname);
printf("%s\n",lname);
printf("Give Address :");
scanf("\n%[^\n]s",address);
printf("%s\n",address);
buildUserProfile(mail,fname,lname,address);
}
if(choice==4){
char name[MAX_LEN];
printf("Give User Name :");
scanf("\n%[^\n]s",name);
searchByName(name);
}
if(choice==5){
char address[MAX_LEN];
printf("Give User Address :");
scanf("\n%[^\n]s",address);
searchByAddress(address);
}
if (choice==6){
break;
}
printf("\n\n");
}
}
/*
Prepared By:
Mahmudur Rahman Tushar
United International University
Dept. Of CSE
*/