Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- #define NR_ROWS 10
- #define NR_COLUMNS 10
- #define WORD_L 32
- typedef char WORD [WORD_L];
- int main(){
- //Declares and initializes variables
- int row_count, col_count;
- WORD str_array[NR_ROWS][NR_COLUMNS];
- char c;
- int terminate_flag = 1, pos_m, pos_n, numb_of_lines = 0, counter = 0, temp_counter = 0;
- char str_input[1000], temp_str[32];
- //Defines and open text files
- FILE *input_file;
- input_file = fopen("input7_3.txt", "r");
- if(input_file == NULL){
- printf("The input file does not exist or cannot be opened.\n");
- exit(1);
- }
- //Initializes the array as null strings
- for(row_count = 0; row_count < 10; row_count++){
- for(col_count = 0; col_count < 10; col_count++){
- strcpy(str_array[row_count][col_count], "");
- }
- }
- //=============================
- //Inroduces the program
- printf("This program manipulates a 2D array of strings\n");
- //=============================
- printf("Note: The counting is starting from 0 like an array.\n");
- row_count = 0;
- col_count = 0;
- while (fscanf (input_file, "%s", str_array[row_count][col_count]) != EOF) {
- // removes the space after the first word of the line
- c = fgetc (input_file);
- col_count++;
- // read all the remaining words of the current line
- while (c != '\n' && c != EOF) {
- fscanf (input_file, "%s", str_array[row_count][col_count]);
- //printf("%d, %d: %s\n", row_count, col_count, str_array[row_count][col_count]);
- c = fgetc (input_file);
- //printf("%c\n", c);
- col_count++;
- }
- strcpy(str_array[row_count][col_count], "");
- //printf("T %d, %d: %s\n", row_count, col_count, str_array[row_count][col_count]);
- //str_array[row_count][col_count]
- numb_of_lines++;
- row_count++;
- col_count = 0;
- } // while
- numb_of_lines;
- printf("%c\n", str_array[4][8]);
- for(row_count = 0; row_count < numb_of_lines; row_count++){
- printf("Line %d: ", row_count);
- col_count = 0;
- while(strcmp(str_array[row_count][col_count], "\0")){
- //printf("%d ", col_count);
- printf("%s ", str_array[row_count][col_count]);
- col_count++;
- }
- col_count = 0;
- printf("\n");
- }
- printf("I - Inserts a new line.\n");
- printf("D - deletes all lines between m and n\n");
- printf("R - Replaces 2 lines between m and n\n");
- printf("E - Terminates editing\n");
- while(terminate_flag){
- printf("Please give an input of I, D, R, or E: ");
- scanf("%c", &c);
- fflush(stdin);
- switch(c){
- case'i':
- case'I':{
- printf("Input the line m that the line will be inserted after: ");
- scanf("%d", &pos_m);
- fflush(stdin);
- printf("Input the line you wish to input: ");
- scanf("%[^\n]s", str_input);
- fflush(stdin);
- if(numb_of_lines == 9){
- numb_of_lines = 8;
- }
- fflush(stdin);
- //If it is set to be after position m, line 0 can never be changed
- for(row_count = numb_of_lines; row_count >= pos_m; row_count--){
- for(col_count = 0; col_count < 10; col_count++){
- strcpy(str_array[row_count + 1][col_count], str_array[row_count][col_count]);
- strcpy(str_array[row_count][col_count], "\0");
- }
- }
- col_count = 0;
- for(counter = 0; counter < strlen(str_input); counter++){
- printf("%d ", counter);
- if(str_input[counter] != ' '){
- printf("%c", str_input[counter]);
- temp_str[temp_counter] = str_input[counter];
- temp_counter++;
- }
- else if(str_input[counter] == ' '){
- printf("T1 \n\n%s\n\n", temp_str);
- strcpy(str_array[pos_m][col_count], temp_str);
- temp_counter = 0;
- col_count++;
- }
- else{
- printf("T2 \n\n%s\n\n", temp_str);
- strcpy(str_array[pos_m][col_count], temp_str);
- break;
- }
- }
- numb_of_lines++;
- break;
- }
- case'd':
- case'D':{
- printf("Input the line m: ");
- scanf("%d", &pos_m);
- fflush(stdin);
- printf("Input the line n: ");
- scanf("%d", &pos_n);
- fflush(stdin);
- for(row_count = pos_n; row_count >= pos_m; row_count--){
- for(col_count = 0; col_count < 10; col_count++){
- strcpy(str_array[row_count][col_count], "\0");
- }
- }
- break;
- }
- case'r':
- case'R':{
- printf("Input the line m: ");
- scanf("%d", &pos_m);
- fflush(stdin);
- printf("Input the line n: ");
- scanf("%d", &pos_n);
- fflush(stdin);
- break;
- }
- case'e':
- case'E':{
- terminate_flag = 0;
- break;
- }
- default: printf("Invalid input.\n");
- }
- for(row_count = 0; row_count < numb_of_lines; row_count++){
- printf("Line %d: ", row_count);
- col_count = 0;
- while(strcmp(str_array[row_count][col_count], "\0")){
- //printf("%d ", col_count);
- printf("%s ", str_array[row_count][col_count]);
- col_count++;
- }
- col_count = 0;
- printf("\n");
- }
- }
- //======================================
- //Closes the files
- fclose(input_file);
- return 0;// Exits the program
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement