Advertisement
rrz0

strcmp() issue

Dec 11th, 2018
402
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.36 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. int main(int argc, char** argv) {
  6.  
  7.     FILE *inp; /* pointer to input file */
  8.     FILE *outp; /* pointer to ouput file */
  9.     char arr_w[50][50];
  10.     char c;
  11.     int word_count = 0;
  12.     int char_count = 0;
  13.  
  14.  
  15.     /* Prepare files for input or output */
  16.     inp = fopen("in_file.txt", "r");
  17.  
  18.     //Reading whole words
  19.     if (inp) {
  20.         while ((c = fgetc(inp)) != EOF) {
  21.            if (c == ' ' || c == '\n') {
  22.                //printf("\n");
  23.  
  24.                arr_w[word_count][char_count] = '\0'; //Terminate the string
  25.                char_count = 0; //Reset the counter.
  26.                word_count++;
  27.            }
  28.            else {
  29.                arr_w[word_count][char_count] = c;
  30.                //printf("%c",arr_w[word_count][char_count]);
  31.  
  32.                if (char_count < 19) {
  33.                    char_count++;
  34.                }
  35.                else {
  36.                    char_count = 0;
  37.                }
  38.            }  
  39.        }
  40.     }
  41.     else {
  42.         printf("File does not exit\n");
  43.     }
  44.  
  45.     printf("word count: %d\n", word_count+1);
  46.     for (int i = 0; i <word_count; i++) {
  47.         if (strcmp(arr_w[i], "mark") == 0) {
  48.             printf("%s found at index %d \n", arr_w[i], i);
  49.         }
  50.     }
  51. }
  52.  
  53. //////
  54. Output:
  55. word count: 3
  56.  
  57. /////
  58.  
  59. in_file.txt:
  60. mark
  61. john
  62. bob
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement