Advertisement
Guest User

Untitled

a guest
Apr 8th, 2020
238
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.69 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4.  
  5. #define NR_ROWS 10
  6. #define NR_COLUMNS 10
  7.  
  8. #define WORD_L  32
  9.  
  10. typedef char WORD [WORD_L];
  11.  
  12. int main(){
  13.    
  14.     //Declares and initializes variables
  15.     int row_count, col_count;
  16.     WORD str_array[NR_ROWS][NR_COLUMNS];
  17.     char c;
  18.     int terminate_flag = 1, pos_m, pos_n, numb_of_lines = 0, counter = 0, temp_counter = 0;
  19.     char str_input[1000], temp_str[32];
  20.    
  21.     //Defines and open text files
  22.     FILE *input_file, *output_file;
  23.     input_file = fopen("input7_3.txt", "r");
  24.     if(input_file == NULL){
  25.         printf("The input file does not exist or cannot be opened.\n");
  26.         exit(1);
  27.     }
  28.    
  29.     output_file = fopen("output7_3.txt", "w");
  30.     if(input_file == NULL){
  31.         printf("The output file cannot be created or cannot be opened.\n");
  32.         exit(1);
  33.     }
  34.    
  35.     //Initializes the array as null strings
  36.     for(row_count = 0; row_count < 10; row_count++){
  37.         for(col_count = 0; col_count < 10; col_count++){
  38.             strcpy(str_array[row_count][col_count], "");
  39.         }
  40.     }
  41.    
  42.     //=============================
  43.    
  44.     //Inroduces the program
  45.     printf("\n");
  46.    
  47.     //=============================
  48.    
  49.     printf("Note: The counting is starting from 0 like an array.\n");
  50.    
  51.     row_count = 0;
  52.     col_count = 0;
  53.     while (fscanf (input_file, "%s", str_array[row_count][col_count]) != EOF) {
  54.         // removes the space after the first word of the line
  55.         c = fgetc (input_file);
  56.         col_count++;
  57.         // read all the remaining words of the current line
  58.         while (c != '\n' && c != EOF) {
  59.             fscanf (input_file, "%s", str_array[row_count][col_count]);
  60.             //printf("%d, %d: %s\n", row_count, col_count, str_array[row_count][col_count]);
  61.             c = fgetc (input_file);
  62.             //printf("%c\n", c);
  63.             col_count++;
  64.         }
  65.         strcpy(str_array[row_count][col_count], "");
  66.         //printf("T %d, %d: %s\n", row_count, col_count, str_array[row_count][col_count]);
  67.         //str_array[row_count][col_count]
  68.         numb_of_lines++;
  69.         row_count++;
  70.         col_count = 0;
  71.     } // while
  72.     numb_of_lines;
  73.    
  74.     printf("%c\n", str_array[4][8]);
  75.    
  76.     for(row_count = 0; row_count < numb_of_lines; row_count++){
  77.         printf("Line %d: ", row_count);
  78.         col_count = 0;
  79.         while(strcmp(str_array[row_count][col_count], "\0")){
  80.             //printf("%d  ", col_count);
  81.             printf("%s ", str_array[row_count][col_count]);
  82.             col_count++;
  83.         }
  84.         col_count = 0;
  85.         printf("\n");
  86.     }
  87.    
  88.     printf("I - Inserts a new line.\n");
  89.     printf("D - deletes all lines between m and n\n");
  90.     printf("R - Replaces 2 lines between m and n\n");
  91.     printf("E - Terminates editing\n");
  92.    
  93.     while(terminate_flag){
  94.         printf("Please give an input of I, D, R, or E: ");
  95.         scanf("%c", &c);
  96.         fflush(stdin);
  97.         switch(c){
  98.             case'i':
  99.             case'I':{
  100.                 printf("Input the line m that the line will be inserted after: ");
  101.                 scanf("%d", &pos_m);
  102.                 printf("Input the line you wish to input: ");
  103.                 scanf("%s", str_input);
  104.                
  105.                 if(numb_of_lines == 9){
  106.                     numb_of_lines = 8;
  107.                 }
  108.                 fflush(stdin);
  109.                 //If it is set to be after position m, line 0 can never be changed
  110.                 for(row_count = numb_of_lines; row_count >= pos_m; row_count--){
  111.                     for(col_count = 0; col_count < 10; col_count++){
  112.                         strcpy(str_array[row_count + 1][col_count], str_array[row_count][col_count]);
  113.                         strcpy(str_array[row_count][col_count], "\0");
  114.                     }
  115.                         printf("\n");
  116.                 }
  117.                 for(counter = 0; counter > strlen(str_input); counter++){
  118.                     if(str_input[counter] != '0' || str_input[counter] != '\0'){
  119.                         temp_str[temp_counter];
  120.                         temp_counter++;
  121.                     }
  122.                 }
  123.                 numb_of_lines++;
  124.                 break;
  125.             }
  126.             case'd':
  127.             case'D':{
  128.                 printf("Input the line m: ");
  129.                 scanf("%d", &pos_m);
  130.                 fflush(stdin);
  131.                 printf("Input the line n: ");
  132.                 scanf("%d", &pos_n);
  133.                 fflush(stdin);
  134.                 for(row_count = pos_n; row_count >= pos_m; row_count--){
  135.                     for(col_count = 0; col_count < 10; col_count++){
  136.                         strcpy(str_array[row_count][col_count], "\0");
  137.                     }
  138.                         printf("\n");
  139.                 }
  140.                 break;
  141.             }
  142.             case'r':
  143.             case'R':{
  144.                 printf("Input the line m: ");
  145.                 scanf("%d", &pos_m);
  146.                 fflush(stdin);
  147.                 printf("Input the line n: ");
  148.                 scanf("%d", &pos_n);
  149.                 fflush(stdin);
  150.                
  151.                 break;
  152.             }
  153.             case'e':
  154.             case'E':{
  155.                 terminate_flag = 0;
  156.                 break;
  157.             }
  158.             default: printf("Invalid input.\n");
  159.         }
  160.        
  161.         for(row_count = 0; row_count < numb_of_lines; row_count++){
  162.             printf("Line %d: ", row_count);
  163.             col_count = 0;
  164.             while(strcmp(str_array[row_count][col_count], "\0")){
  165.                 //printf("%d  ", col_count);
  166.                 printf("%s ", str_array[row_count][col_count]);
  167.                 col_count++;
  168.             }
  169.             col_count = 0;
  170.             printf("\n");
  171.         }
  172.        
  173.     }
  174.  
  175.    
  176.    
  177.     //======================================
  178.    
  179.     //Closes the files
  180.     fclose(input_file);
  181.     fclose(output_file);
  182.    
  183.     return 0;// Exits the program
  184. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement