Advertisement
Guest User

Untitled

a guest
Apr 8th, 2020
302
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.97 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;
  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.     //Initializes the array as null strings
  30.     for(row_count = 0; row_count < 10; row_count++){
  31.         for(col_count = 0; col_count < 10; col_count++){
  32.             strcpy(str_array[row_count][col_count], "");
  33.         }
  34.     }
  35.    
  36.     //=============================
  37.    
  38.     //Inroduces the program
  39.     printf("This program manipulates a 2D array of strings\n");
  40.    
  41.     //=============================
  42.    
  43.     printf("Note: The counting is starting from 0 like an array.\n");
  44.    
  45.     row_count = 0;
  46.     col_count = 0;
  47.     while (fscanf (input_file, "%s", str_array[row_count][col_count]) != EOF) {
  48.         // removes the space after the first word of the line
  49.         c = fgetc (input_file);
  50.         col_count++;
  51.         // read all the remaining words of the current line
  52.         while (c != '\n' && c != EOF) {
  53.             fscanf (input_file, "%s", str_array[row_count][col_count]);
  54.             //printf("%d, %d: %s\n", row_count, col_count, str_array[row_count][col_count]);
  55.             c = fgetc (input_file);
  56.             //printf("%c\n", c);
  57.             col_count++;
  58.         }
  59.         strcpy(str_array[row_count][col_count], "");
  60.         //printf("T %d, %d: %s\n", row_count, col_count, str_array[row_count][col_count]);
  61.         //str_array[row_count][col_count]
  62.         numb_of_lines++;
  63.         row_count++;
  64.         col_count = 0;
  65.     } // while
  66.     numb_of_lines;
  67.    
  68.     printf("%c\n", str_array[4][8]);
  69.    
  70.     for(row_count = 0; row_count < numb_of_lines; row_count++){
  71.         printf("Line %d: ", row_count);
  72.         col_count = 0;
  73.         while(strcmp(str_array[row_count][col_count], "\0")){
  74.             //printf("%d  ", col_count);
  75.             printf("%s ", str_array[row_count][col_count]);
  76.             col_count++;
  77.         }
  78.         col_count = 0;
  79.         printf("\n");
  80.     }
  81.    
  82.     printf("I - Inserts a new line.\n");
  83.     printf("D - deletes all lines between m and n\n");
  84.     printf("R - Replaces 2 lines between m and n\n");
  85.     printf("E - Terminates editing\n");
  86.    
  87.     while(terminate_flag){
  88.         printf("Please give an input of I, D, R, or E: ");
  89.         scanf("%c", &c);
  90.         fflush(stdin);
  91.         switch(c){
  92.             case'i':
  93.             case'I':{
  94.                 printf("Input the line m that the line will be inserted after: ");
  95.                 scanf("%d", &pos_m);
  96.                 fflush(stdin);
  97.                 printf("Input the line you wish to input: ");
  98.                 scanf("%[^\n]s", str_input);
  99.                 fflush(stdin);
  100.                
  101.                 if(numb_of_lines == 9){
  102.                     numb_of_lines = 8;
  103.                 }
  104.                 fflush(stdin);
  105.                 //If it is set to be after position m, line 0 can never be changed
  106.                 for(row_count = numb_of_lines; row_count >= pos_m; row_count--){
  107.                     for(col_count = 0; col_count < 10; col_count++){
  108.                         strcpy(str_array[row_count + 1][col_count], str_array[row_count][col_count]);
  109.                         strcpy(str_array[row_count][col_count], "\0");
  110.                     }
  111.                 }
  112.                 col_count = 0;
  113.                 for(counter = 0; counter < strlen(str_input); counter++){
  114. //                  printf("%d ", counter);
  115. //                  printf("%c", str_input[counter]);
  116.                     if(str_input[counter] == ' '){
  117.                         printf("T1 \n\n%s\n\n", temp_str);
  118.                         strcpy(str_array[pos_m][col_count], temp_str);
  119.                         temp_counter = 0;
  120.                         col_count++;
  121.                     }
  122.                     else if(str_input[counter] == '\0'){
  123.                         temp_str[temp_counter] = str_input[counter];
  124.                         temp_counter = 0;
  125.                         temp_counter++;
  126.                     }
  127.                     else{
  128.                         printf("T2 \n\n%s\n\n", temp_str);
  129.                         strcpy(str_array[pos_m][col_count], temp_str);
  130.                         break;
  131.                     }  
  132.                 }
  133.                 numb_of_lines++;
  134.                 break;
  135.             }
  136.             case'd':
  137.             case'D':{
  138.                 printf("Input the line m: ");
  139.                 scanf("%d", &pos_m);
  140.                 fflush(stdin);
  141.                 printf("Input the line n: ");
  142.                 scanf("%d", &pos_n);
  143.                 fflush(stdin);
  144.                 for(row_count = pos_n; row_count >= pos_m; row_count--){
  145.                     for(col_count = 0; col_count < 10; col_count++){
  146.                         strcpy(str_array[row_count][col_count], "\0");
  147.                     }
  148.                 }
  149.                 break;
  150.             }
  151.             case'r':
  152.             case'R':{
  153.                 printf("Input the line m: ");
  154.                 scanf("%d", &pos_m);
  155.                 fflush(stdin);
  156.                 printf("Input the line n: ");
  157.                 scanf("%d", &pos_n);
  158.                 fflush(stdin);
  159.                
  160.                 break;
  161.             }
  162.             case'e':
  163.             case'E':{
  164.                 terminate_flag = 0;
  165.                 break;
  166.             }
  167.             default: printf("Invalid input.\n");
  168.         }
  169.        
  170.         for(row_count = 0; row_count < numb_of_lines; row_count++){
  171.             printf("Line %d: ", row_count);
  172.             col_count = 0;
  173.             while(strcmp(str_array[row_count][col_count], "\0")){
  174.                 //printf("%d  ", col_count);
  175.                 printf("%s ", str_array[row_count][col_count]);
  176.                 col_count++;
  177.             }
  178.             col_count = 0;
  179.             printf("\n");
  180.         }
  181.        
  182.     }
  183.  
  184.    
  185.    
  186.     //======================================
  187.    
  188.     //Closes the files
  189.     fclose(input_file);
  190.    
  191.     return 0;// Exits the program
  192. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement