Advertisement
Guest User

Untitled

a guest
Apr 8th, 2020
236
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.94 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.                     if(str_input[counter] != ' '){
  116.                         printf("%c", str_input[counter]);
  117.                         temp_str[temp_counter] = str_input[counter];
  118.                         temp_counter++;
  119.                     }
  120.                     else if(str_input[counter] == ' '){
  121.                         printf("T1 \n\n%s\n\n", temp_str);
  122.                         strcpy(str_array[pos_m][col_count], temp_str);
  123.                         temp_counter = 0;
  124.                         col_count++;
  125.                     }
  126.                     else{
  127.                         printf("T2 \n\n%s\n\n", temp_str);
  128.                         strcpy(str_array[pos_m][col_count], temp_str);
  129.                         break;
  130.                     }  
  131.                 }
  132.                 numb_of_lines++;
  133.                 break;
  134.             }
  135.             case'd':
  136.             case'D':{
  137.                 printf("Input the line m: ");
  138.                 scanf("%d", &pos_m);
  139.                 fflush(stdin);
  140.                 printf("Input the line n: ");
  141.                 scanf("%d", &pos_n);
  142.                 fflush(stdin);
  143.                 for(row_count = pos_n; row_count >= pos_m; row_count--){
  144.                     for(col_count = 0; col_count < 10; col_count++){
  145.                         strcpy(str_array[row_count][col_count], "\0");
  146.                     }
  147.                 }
  148.                 break;
  149.             }
  150.             case'r':
  151.             case'R':{
  152.                 printf("Input the line m: ");
  153.                 scanf("%d", &pos_m);
  154.                 fflush(stdin);
  155.                 printf("Input the line n: ");
  156.                 scanf("%d", &pos_n);
  157.                 fflush(stdin);
  158.                
  159.                 break;
  160.             }
  161.             case'e':
  162.             case'E':{
  163.                 terminate_flag = 0;
  164.                 break;
  165.             }
  166.             default: printf("Invalid input.\n");
  167.         }
  168.        
  169.         for(row_count = 0; row_count < numb_of_lines; row_count++){
  170.             printf("Line %d: ", row_count);
  171.             col_count = 0;
  172.             while(strcmp(str_array[row_count][col_count], "\0")){
  173.                 //printf("%d  ", col_count);
  174.                 printf("%s ", str_array[row_count][col_count]);
  175.                 col_count++;
  176.             }
  177.             col_count = 0;
  178.             printf("\n");
  179.         }
  180.        
  181.     }
  182.  
  183.    
  184.    
  185.     //======================================
  186.    
  187.     //Closes the files
  188.     fclose(input_file);
  189.    
  190.     return 0;// Exits the program
  191. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement