Advertisement
sp1d3o

Untitled

Oct 30th, 2022
1,016
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.26 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <stdbool.h>
  5. #include <ctype.h>
  6.  
  7. const char key[10][5] = {
  8.         {'0', '+', 0, 0, 0},
  9.         {'1', 0, 0, 0, 0},
  10.         {'2', 'a', 'b', 'c', 0},
  11.         {'3', 'd', 'e', 'f', 0},
  12.         {'4', 'g', 'h', 'i', 0},
  13.         {'5', 'j', 'k', 'l', 0},
  14.         {'6', 'm', 'n', 'o', 0},
  15.         {'7', 'p', 'q', 'r', 's'},
  16.         {'8', 't', 'u', 'v', 0},
  17.         {'9', 'w', 'x', 'y', 'z'} };
  18.  
  19. //the 2 very important arrays into which the number and name are loaded
  20. char buffer_1[100];
  21. char buffer_2[100];
  22.  
  23. //definicion of 2 functions that will be used other then main
  24. bool check(char* data, char* pattern);
  25. int get_line(char* buffer, unsigned int size);
  26. bool checkFromThis(char* data, char* pattern);
  27.  
  28. int main(int argc, char* argv[]){
  29.  
  30.     //declaration of variable which will be used for estetic and semi-functional purposes
  31.     int count = 0;
  32.     int result_1;
  33.     int result_2;
  34.  
  35.     while (true) {
  36.         // printf("lll\n");
  37.         //here are 2 variable which determine which scenario will be played out, in them specific function is called
  38.         result_1 = get_line(buffer_1, sizeof(buffer_1));
  39.         result_2 = get_line(buffer_2, sizeof(buffer_2));
  40.  
  41.         //this switch decides weather the program is done or it has any mistakes in the input, if the result_1 is 0 it will go through unharmmed
  42.         switch (result_1) {
  43.             case 1:
  44.                 if (count == 0){
  45.                     printf("not found\n");
  46.                 }
  47.                 return 0;
  48.             case 2:
  49.                 //if the input is too long it will not be printed
  50.                 continue;
  51.         }
  52.  
  53.         //this exacly the same as the previous switch but it is for the second result which could be either a number or name
  54.         switch (result_2) {
  55.             case 1:
  56.                 if (count == 0){
  57.                     printf("not found\n");
  58.                 }
  59.                 return 0;
  60.             case 2:
  61.                 continue;
  62.         }
  63.  
  64.         //this line checks for any and all missing information
  65.         if (buffer_1[0] == '\n' || buffer_1[0] == '\0' || buffer_2[0] == '\n' || buffer_2[0] == '\0') {
  66.             continue;
  67.         }
  68.  
  69.         //in this condition it checks if the buffer meets the pattern in any way
  70.         //if either buffer meets the demands it will continue in
  71.  
  72.         if (check(buffer_1, argv[1]) || check(buffer_2, argv[1])) {
  73.  
  74.             //this condition checks for the order of the contact and number and if necessity requires, it will swap them upon printing
  75.             if ((buffer_2[0] >= '0') && (buffer_2[0] <= '9')) {
  76.                 printf("%s, %s\n", buffer_1, buffer_2);
  77.  
  78.                 //the variable count is here in order to capture the number of matches
  79.                 count++;
  80.             }
  81.             else {
  82.                 printf("%s, %s\n", buffer_2, buffer_1);
  83.                 count++;
  84.             }
  85.         }
  86.     }
  87.     (void)argc;
  88. }
  89.  
  90. bool check(char* data, char* pattern){
  91.     if(data == NULL || pattern == NULL || data == 0 || pattern == 0)
  92.         return true;
  93.     for(unsigned int i = 0; i < strlen(data) - strlen(pattern); i++){
  94.         if(checkFromThis(&data[i], pattern))
  95.             return true;
  96.     }
  97.     return false;
  98. }
  99.  
  100. bool checkFromThis(char* data, char* pattern){
  101.     char helper = tolower(data[0]);
  102.     if(pattern == NULL || data  == NULL || data[0] == 0 || pattern[0] == 0)
  103.         return true;
  104.     for(int i = 0; i < 5; i++){
  105.         if((helper == key[pattern[0] - '0'][i])){
  106.             return checkFromThis(&data[1], &pattern[1]);
  107.         }
  108.     }
  109.     return false;
  110. }
  111.  
  112. int get_line(char* buffer, unsigned int size) {
  113.  
  114.     //the next line check weather or not the program reach the end of the file, if so it will return the number 1
  115.     if (fgets(buffer, size, stdin) == NULL) {
  116.         return 1;
  117.     }
  118.    
  119.     //the next line check for the length of input into the array to determine if it is viable for considarion in the pattern search
  120.     //if the condition is met the program will go in and decide which function output does it use
  121.     if ((strlen(buffer) + 1) < size) {
  122.        
  123.  
  124.         //the next is here specificly for the last line in the file, so that it would know to include it in the pattern decision
  125.         if (!(buffer[strlen(buffer) - 1] == '\n' || buffer[strlen(buffer) - 2] == '\r')) {
  126.             return 0;
  127.         }
  128.  
  129.         if (buffer[strlen(buffer) - 1] == '\n') {
  130.             buffer[strlen(buffer) - 1 ] = 0;
  131.             if (buffer[strlen(buffer) - 1] == '\r') {
  132.                 buffer[strlen(buffer) - 1] = 0;
  133.                 return 0;
  134.             }
  135.             return 0;
  136.         }
  137.         return 0;
  138.     }
  139.     //this very specific variable is basicly a trash can for the chracters which come after the too long input, so that the cursor muves to the next line
  140.     char buffer_x[100];
  141.  
  142.     //this while reads the rest of the line in case it is too long to meet project's requirements
  143.     while ((fgets(buffer_x, sizeof(buffer_x), stdin) != NULL) && (strlen(buffer_x) == sizeof(buffer_x) - 1) && (buffer_x[sizeof(buffer_x) - 1] != '\n')) {}
  144.  
  145.     //at this point in the code there is no other way then retunr the number 2 so that it writes that the input is too long
  146.     return 2;
  147.  
  148. }
Advertisement
Comments
  • CovboyMate
    1 year
    # text 0.11 KB | 0 0
    1. https://steemit.com/money/@moneystudio/gain-usd250-per-day-online-right-now-very-unique-extremely-easy-hq-method
Add Comment
Please, Sign In to add comment
Advertisement