Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
jobrod
/
Week3Programming2JordanBrodie
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Members
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
fb3f53e7
authored
2 months ago
by
jobrod
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add new file
parents
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
58 additions
and
0 deletions
wordcount.c
wordcount.c
0 → 100644
View file @
fb3f53e7
#include <stdio.h>
#include <string.h>
#define MAX_LINE 1024
// Function prototypes
void
process_file
(
const
char
*
filename
);
int
count_words
(
char
*
line
);
int
main
()
{
process_file
(
"raamat1184.txt"
);
return
0
;
}
// Function to process the file and count words
void
process_file
(
const
char
*
filename
)
{
FILE
*
file
=
fopen
(
filename
,
"r"
);
if
(
file
==
NULL
)
{
printf
(
"Error: Could not open file.
\n
"
);
return
;
}
else
{
printf
(
"File opened successfully.
\n
"
);
}
char
line
[
MAX_LINE
];
int
total_words
=
0
;
while
(
fgets
(
line
,
sizeof
(
line
),
file
))
{
total_words
+=
count_words
(
line
);
}
fclose
(
file
);
printf
(
"File closed successfully.
\n
"
);
printf
(
"There are %d words in this book.
\n
"
,
total_words
);
}
// Function to count words in a line
int
count_words
(
char
*
line
)
{
int
count
=
0
;
char
*
word
=
strtok
(
line
,
"
\t\n
"
);
while
(
word
!=
NULL
)
{
count
++
;
word
=
strtok
(
NULL
,
"
\t\n
"
);
}
return
count
;
}
This diff is collapsed.
Click to expand it.
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment