Advertisement
houwenliee56

UTS ALGO NO 2

Nov 10th, 2016
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.42 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<string.h>
  4. #include<ctype.h>
  5.  
  6. int main(void) {
  7.     int max_user;
  8.     char cardinal[10][10] = {"1st", "2nd", "3rd", "4th", "5th", "6th", "7th", "8th", "9th", "10th"};
  9.     char username[10][50];
  10.     char* temp;
  11.     char password[10][50];
  12.     char newpassword[10][50];
  13.     char email[10][50];
  14.     char *p;
  15.  
  16.     do { //Maximum users
  17.         printf("Input max user (3-10) : "); scanf("%d", &max_user); fflush(stdin);
  18.  
  19.     }while (max_user > 10 || max_user < 3);
  20.  
  21.     bool unique;
  22.    
  23.  
  24.     for (int i = 0; i < max_user; i++) {
  25.        
  26.         do {
  27.             unique = true; //Check if the username inputted has been inputted before
  28.             printf("Input the %s username : ", cardinal[i]); scanf("%s", &username[i]); fflush(stdin);
  29.             if(i!=0) {
  30.                 for (int a = 0; a < i; a++) {
  31.                     if(strcmp(username[i], username[a]) == 0) {
  32.                         unique = false;
  33.                         break;
  34.                     }
  35.                 }
  36.             }
  37.         }while(unique == false);   
  38.  
  39.         do { //Password needs to be at least 5 characters
  40.             printf("Input Password (Min 5 characters) : ");
  41.             scanf("%s", &password[i]); fflush(stdin);
  42.  
  43.         }while(strlen(password[i]) < 5);
  44.        
  45.         printf("\nPassword converted ...\n");
  46.         int n = strlen(password[i]);
  47.        
  48.         for (int a = 0; a < n; a++) {
  49.             if (a == 0) { //First and last character are changed into upper case
  50.                 password[i][a] = toupper(password[i][a]);
  51.             }else if(a == n-1) {
  52.                 password[i][a] = toupper(password[i][a]);
  53.             }else { //Other characters are changed into lower case
  54.                 password[i][a] = tolower(password[i][a]);
  55.             }
  56.                
  57.             switch(password[i][a]) { //Changing password
  58.             case 'O' :
  59.                 password[i][a] = '0'; break;
  60.             case 'o' :
  61.                 password[i][a] = '0'; break;
  62.             case 'I' :
  63.                 password[i][a] = '1'; break;
  64.             case 'i' :
  65.                 password[i][a] = '1'; break;
  66.             case 'S' :
  67.                 password[i][a] = '$'; break;
  68.             case 's' :
  69.                 password[i][a] = '$'; break;
  70.             case 'T' :
  71.                 password[i][a] = '7'; break;
  72.             case 't' :
  73.                 password[i][a] = '7';
  74.  
  75.             }
  76.  
  77.        
  78.         }
  79.        
  80.         strcat(password[i], "!"); //Adding a "!" to password array
  81.        
  82.         //By now, password array has fundamentally changed, so program will ask user to
  83.         // input new password, which will be compared to password array
  84.  
  85.         printf("New password is %s\n\n", password[i]);
  86.         do {
  87.             printf("Now please enter your new password : ");
  88.             scanf("%s", newpassword[i]); fflush(stdin);
  89.         }while(strcmpi(newpassword[i], password[i]) != 0);
  90.  
  91.         bool pass;
  92.         do {
  93.             pass = false; //Initializing boolean value of pass
  94.             printf("Nice! Please enter your email : ");
  95.             scanf("%s", email[i]); fflush(stdin);
  96.            
  97.             int x = strlen(email[i]);
  98.             char* p = &email[i][(x - 10)];
  99.             //The output of strstr is a pointer, so we initialize value of p as
  100.             // "the adress" of email[i][x - 10]. 10 Because @gmail.com as well as
  101.             // @yahoo.com contains 10 character
  102.  
  103.             if (p == strstr(email[i], "@gmail.com") || p == strstr(email[i], "@yahoo.com")) {
  104.                 //Here you can see we compare p to strstr, and if inputted email successfully
  105.                 //goes through this selection, then boolean value of pass becomes true
  106.                 //These all will be done again and again if inputted email fails to go through
  107.                 //this selection
  108.                 pass = true;
  109.             }
  110.  
  111.                                
  112.         }while (pass == false);
  113.  
  114.         system("cls");
  115.     }
  116.  
  117.     printf("%-10s %-20s\n\n", "Name", "Email");
  118.     for(int i = 0 ; i < max_user; i++) {
  119.         printf("%-10s %-20s\n", username[i], email[i]);
  120.     }
  121.     printf("Thank you for using this program ");
  122.  
  123.  
  124.     getchar();
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement