Advertisement
Guest User

Untitled

a guest
May 8th, 2017
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.09 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <ctype.h>
  4. // this program is a login feature of an application
  5. int menu (void); //it lets the user choose if he/she want to login, signin or exit
  6. void login (void); //user login and change password /username
  7. void sign_in(void);
  8. void exitpro(void); //Exit the program
  9. int user_age (char dob[]); //To count the age of the users
  10. double compare(char arr1[], char arr2[], int line); //Find the percentage of similarity between two strings
  11. void encryptpass(char arr[]); //Encrypt a string by finding its Ceaser cipher
  12. void decryptpass(char arr[]); //Decrypt a string
  13. void sortbyage (void); //Sorts the users by their age
  14. void swapping(char str1[], char str2[]); //Swaps to strings
  15.  
  16. /*Declaring global variables since we're using the same input parameters in most of the functions*/
  17. char usernames[100][40], fnames[100][40], lnames[100][40], dobs[100][12], passwords[100][13];
  18. int users=4, ages[20];
  19.  
  20. int main(){
  21.  
  22. int choice, i;
  23.  
  24. FILE *fptr; //Declaring the file we are using as an input file
  25. fptr=fopen("database.txt","r"); //Opening the file to read from it
  26.  
  27. for(i=0; i<users; i++){
  28. fscanf(fptr, "%s", usernames[i]); //Transfering the first string of each line into an array
  29. fscanf(fptr, "%s", fnames[i]); //Transfering the second string of each line into an array
  30. fscanf(fptr, "%s", lnames[i]); //Transfering the third string of each line into an array
  31. fscanf(fptr, "%s", dobs[i]); //Transfering the fourth string of each line into an array
  32. fscanf(fptr, "%s", passwords[i]); //Transfering the fifth string of each line into an array
  33. encryptpass(passwords[i]);//We must encrypt the password for the sake of safety.
  34. }
  35.  
  36. choice=menu(); //Calling the menu so the user chooses whether to login, signin or exit
  37.  
  38. for(i=0;choice!=0;i++){
  39. if (choice==1)//Once the user presses 1, he's calling the function login.
  40. login();
  41. else
  42. sign_in();//The user has chosen 2 he then procedes to sign it.
  43.  
  44. choice=menu();//The answer here is either 1 2 or 0 in order to determine the user's choice.
  45. }
  46.  
  47. exitpro(); //Once the choice is 0 the function exit is called to exit the program
  48.  
  49. return 0;
  50. }
  51.  
  52. //The function Menu displays the choices the user has which are either logingin by returning 1, signingin which returns 2, or exiting by pressing on 0.
  53. int menu (void){
  54. int choice;
  55. printf("///Welcome to your\\\\\\ \n\nPress 1 to log in\nPress 2 to sign in \nPress 0 to exit\n ");
  56. scanf("%d", &choice);
  57. return choice;
  58. }
  59.  
  60. /*The login function checks if the username and the password entered by the user match the information in the database (minimum of 80% for the username).
  61. If a match is found, the user chooses to either change his username or password or log out and go back to the login page
  62. If the program finds no match, the user is asked again to re enter both username and paswword to go another time through the same login process*/
  63. void login (void){
  64.  
  65. char username[15], password[13];
  66. double occurence;
  67. int i, j, flagy=0, flagn=0, choice, countup=0, countlow=0, countdigit=0, countspe=0, flag1=0, flag2=0, age, flag=0;
  68.  
  69. do{
  70. do{
  71. printf("\n\2\3\2\3\2 Log in \2\3\2\3\2\n");
  72. printf("\n");
  73. printf("Please enter your unsername \n ");
  74. scanf("%s", username);
  75. for(i=0; i<users; i++){
  76. occurence=compare(username, usernames[i], i);
  77. if (occurence>=80){
  78. flagy=1;
  79. break;
  80. }
  81. }
  82. printf("Please enter your password\n ");
  83. scanf("%s", password);
  84. password[12]='\0';
  85. decryptpass(passwords[i]);
  86. if (strcmp(password, passwords[i])==0){
  87. }
  88. encryptpass(passwords[i]);
  89. }while(flagy==0 || flagn==0);
  90.  
  91. printf("\n///Welcome, you have successfully logged in.\\\\\\\n");
  92.  
  93.  
  94. age=user_age(dobs[i]);
  95.  
  96. printf("\nFirst name: %s\n", fnames[i]);
  97. printf("Last name: %s\n", lnames[i]);
  98. printf("Age: %d\n", age);
  99.  
  100.  
  101. printf("\nPress 1 if you want to change your username\nPress 2 if you want to change your password\nPress 3 if you want to log out\n ");
  102. scanf("%d", &choice);
  103.  
  104. switch(choice){
  105. case 1:
  106. do{
  107. flag2=0;
  108. printf("\nPlease enter Username\n");
  109. scanf("%s", username);
  110. for(j=0; j<users; j++)
  111. occurence=compare(username, usernames[j], i);//The program compares between the new username and the ones in the database.
  112. if(occurence>=80){
  113. printf("User already exists.\n");//if there's a match starting from 80% the user is asked to enter another one.
  114. flag2=1;
  115. break;
  116. }
  117. }while(flag2==1);
  118. if(flag2==0){
  119. strcpy(usernames[i],username);
  120. printf("Your username has been successfully changed.\n");//This means that no match was found and that the user has been able to change his username.
  121. }
  122. break;
  123.  
  124. case 2:
  125. do{
  126. countup=0;
  127. countlow=0;
  128. countdigit=0;
  129. countspe=0;
  130. flag=0;
  131. printf("Please enter a password of 12 characters which contains 3 lower case letters, 3 upper case letters, 3 digits and 3 special characters different from '@', '\' and ';' > ");
  132. scanf("%s", password);
  133. for(j=0; j<12; j++){
  134. if(isupper(password[j]))
  135. countup++;
  136. else if(islower(password[j]))
  137. countlow++;
  138. else if(isdigit(password[j]))
  139. countdigit++;
  140. else countspe++;
  141. if (password[j]=='@' || password[j]==';' || password[j]=='\\' )
  142. flag=1;
  143. }
  144. }while(countup!=3 || countlow!=3 || countdigit!=3 || countspe!=3 || flag==1);
  145. encryptpass(password);
  146.  
  147. strcpy(passwords[i],password);
  148. printf("Password successfully changed");
  149. break;
  150. }
  151. }while(choice==3);
  152. }
  153.  
  154.  
  155. void sign_in (void){
  156.  
  157. char username[15], password[13];
  158. int countup=0, countlow=0, countdigit=0, countspe=0, flag=0, i, j;
  159. double occurence;
  160.  
  161. printf("\n");
  162. printf("\2\3\2\3\2 Sign in \2\3\2\3\2\n");
  163. printf("\n");
  164.  
  165. printf("Please enter username > ");
  166. scanf("%s", username);
  167.  
  168. for(i=0; i<users; i++){ //If the new username has at least 80% similarity with any of the other existing usernames, the user is asked to enter a new one
  169. occurence=compare(username, usernames[i], i);
  170. while(occurence>=80){
  171. printf("User already exists. Please enter a new username > ");
  172. scanf("%s", username);
  173. occurence=compare(username, usernames[i], i);
  174. }
  175. }
  176. strcpy(usernames[users],username);//This will allow the program to save the new username
  177.  
  178. printf("Please enter first name > ");
  179. scanf("%s", fnames[users]);
  180. printf("Please enter last name > ");
  181. scanf("%s", lnames[users]);
  182. printf("Please enter date of birth in the following format XXjanXXXX > ");
  183. scanf("%s", dobs[users]);
  184.  
  185. do{
  186. countup=0;
  187. countlow=0;
  188. countdigit=0;
  189. countspe=0;
  190. flag=0;
  191. printf("Please enter a password of 12 characters which contains 3 lower case letters, 3 upper case letters, 3 digits and 3 special characters different from '@', '\\' and ';' > ");
  192. scanf("%s", password);
  193.  
  194. for(j=0; j<12; j++){
  195. if(isupper(password[j]))
  196. countup++;
  197. else if(islower(password[j]))
  198. countlow++;
  199. else if(isdigit(password[j]))
  200. countdigit++;
  201. else countspe++;
  202. if (password[j]=='@' || password[j]==';' || password[j]=='\\' )
  203. flag=1;
  204. }
  205. }while(countup!=3 || countlow!=3 || countdigit!=3 || countspe!=3 || flag==1);
  206.  
  207. printf("\nThe new user has been created.\n\nUsername: %s\nPassword: %s\n", username, password);
  208.  
  209. encryptpass(password);//The password must get encrypted to avoid any security breach.
  210. strcpy(passwords[users],password);//This will allow the program to save the new password.
  211. users+=1;
  212. }
  213.  
  214. /*The exit function will save every change and addes account to an output file.
  215. The users information will be displayed in the output file sorted by age from the oldest to the youngest.
  216. The passwords are also encrypted.*/
  217. void exitpro(void){
  218. int i;
  219. FILE *output;
  220. printf("Thank you for using our program.\n\n");
  221. output=fopen("output.txt","w");//We open the output file where the information will be directly written.
  222. sortbyage();//We call the function to sort by age.
  223. for(i=0; i<users; i++){
  224. fprintf(output,"%s %s %s %s %s\n", usernames[i], fnames[i], lnames[i], dobs[i], passwords[i]);//All the sorted information gets printed in the output file.
  225. }
  226. fclose(output);//Now that we're done, the file gets closed.
  227. }
  228.  
  229. //This function has been used several times to find the percentage of similarity between the new/entered usernames and the ones already in the databse.
  230. double compare(char arr1[], char arr2[], int line){
  231. int i, j,len1, len2;
  232. double occurence, count=0;
  233. len1=strlen(arr1);
  234. len2=strlen(arr2);
  235.  
  236. if (len1!=len2)//The two passwords must be of the same length to start comparing them.
  237. return 0;
  238. else {
  239. for(i=0; i<len1; i++){
  240. for(j=0; j<len2; j++){
  241. if (arr1[i]==arr2[j]){//Here we compare character by character and each time a match is found, the count increases by 1.
  242. count++;
  243. j=0;//j must be re-initialized so that each character from the first word will be compared to the second word starting from the first letter.
  244. i++;
  245. }
  246. }
  247. }
  248. }
  249.  
  250. occurence=(count/len1)*100.0;
  251. return occurence;//This function returns the occurence as a double based on the length of the word.
  252. }
  253.  
  254. /*The function count age computes the age of the user who's successfully logged in*/
  255. int user_age (char bth[]){
  256.  
  257. char tempd[50],tempm[50],tempy[10],dec[3]="dec";
  258. int day,year,n1,k;
  259. int day1,year1,tempm1;
  260.  
  261.  
  262. n1=strlen(bth);
  263. strcpy(tempy,&bth[n1-4]);
  264. strncpy(tempd,&bth[n1-9],2);
  265. strncpy(tempm,&bth[n1-7],3);
  266. tempm[3]='\0';
  267. sscanf(tempy,"%d \n",&year);
  268. sscanf(tempd,"%d \n",&day);
  269. year1= 2015-year;
  270. k=strncmp(dec,tempm,3);
  271. if(k==0 && day>=1)
  272. year1--;
  273.  
  274. printf("Your age is %d\n",year1);
  275.  
  276.  
  277. return year1;
  278.  
  279. }
  280.  
  281. //Encrypting solves many security issues. In our program, it adds 15 to the ASCII of each letter the user enters and gives us the character that matches to that new ASCII.
  282. void encryptpass(char arr[]){
  283. int i;
  284.  
  285. for(i=0; i<12; i++)
  286. arr[i]=arr[i]+15;
  287. }
  288.  
  289. //Decyrpting deducts 15 from the ASCII of each character and finds the matching character.
  290. void decryptpass(char arr[]){
  291. int i;
  292.  
  293. for(i=0; i<12; i++)
  294. arr[i]=arr[i]-15;
  295. }
  296.  
  297. /*This is the function called in the exit that'll sort ever single user information and any change occured
  298. based on their year of birth from the oldest to the youngest*/
  299. void sortbyage (void){
  300. int i, j;
  301. i=0;
  302. j=0;
  303. do{ //Here we're usng the bubble sort concept to swap the information between the two strings if the year of the first is lesser than the second.
  304. do{
  305.  
  306. if(user_age(dobs[j])<user_age(dobs[j+1])){
  307. swapping(usernames[j], usernames[j+1]);
  308. swapping(fnames[j], fnames[j+1]);
  309. swapping(lnames[j], lnames[j+1]);
  310. swapping(dobs[j], dobs[j+1]);
  311. swapping(passwords[j], passwords[j+1]);
  312. }j++;
  313.  
  314. }while(j<users-i-1);
  315.  
  316. i++;
  317. }while(i<users);
  318. }
  319.  
  320. //The swap function is a basic function that'll swap the information based on the desired conditions in the sortbyage function.
  321. void swapping(char str1[], char str2[]){
  322. char t[20];
  323.  
  324. strcpy(t,str1);
  325. strcpy(str1,str2);
  326. strcpy(str2,t);
  327. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement