Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- int main(int argc, char** argv) {
- FILE *inp; /* pointer to input file */
- FILE *outp; /* pointer to ouput file */
- char arr_w[50][50];
- char c;
- int word_count = 0;
- int char_count = 0;
- /* Prepare files for input or output */
- inp = fopen("in_file.txt", "r");
- //Reading whole words
- if (inp) {
- while ((c = fgetc(inp)) != EOF) {
- if (c == ' ' || c == '\n') {
- //printf("\n");
- arr_w[word_count][char_count] = '\0'; //Terminate the string
- char_count = 0; //Reset the counter.
- word_count++;
- }
- else {
- arr_w[word_count][char_count] = c;
- //printf("%c",arr_w[word_count][char_count]);
- if (char_count < 19) {
- char_count++;
- }
- else {
- char_count = 0;
- }
- }
- }
- }
- else {
- printf("File does not exit\n");
- }
- printf("word count: %d\n", word_count+1);
- for (int i = 0; i <word_count; i++) {
- if (strcmp(arr_w[i], "mark") == 0) {
- printf("%s found at index %d \n", arr_w[i], i);
- }
- }
- }
- //////
- Output:
- word count: 3
- /////
- in_file.txt:
- mark
- john
- bob
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement