From a8f909d1a913461c1494853c8c3e7a495997056f Mon Sep 17 00:00:00 2001 From: Intel A80486DX2-66 Date: Wed, 31 Jan 2024 20:00:17 +0300 Subject: [PATCH] D: add countLettersInTextFile.d --- .gitignore | 33 ++++++++++++++++++++++++++ d-programming/countLettersInTextFile.d | 16 +++++++++++++ 2 files changed, 49 insertions(+) create mode 100644 d-programming/countLettersInTextFile.d diff --git a/.gitignore b/.gitignore index ab6511a..a10dff3 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,7 @@ # ---> C # Prerequisites *.d +!d-programming/**.d # Object files *.o @@ -213,3 +214,35 @@ cython_debug/ # and can be added to the global gitignore or merged into this file. For a more nuclear # option (not recommended) you can uncomment the following to ignore the entire idea folder. #.idea/ + +# ---> D +# Compiled Object files +*.o +*.obj + +# Compiled Dynamic libraries +*.so +*.dylib +*.dll + +# Compiled Static libraries +*.a +*.lib + +# Executables +*.exe + +# DUB +.dub +docs.json +__dummy.html +docs/ + +# Code coverage +*.lst + +# Swap files of editors +*.*~ + +# Build directory +build/ diff --git a/d-programming/countLettersInTextFile.d b/d-programming/countLettersInTextFile.d new file mode 100644 index 0000000..386f51d --- /dev/null +++ b/d-programming/countLettersInTextFile.d @@ -0,0 +1,16 @@ +import std.algorithm.iteration : filter; +import std.algorithm.searching : count; +import std.ascii : isAlpha, isWhite; +import std.file : read; +import std.stdio : writefln; + +void main() +{ + auto fileContent = cast(string)read("file.txt"); + auto totalLetters = + count(filter!(a => isAlpha(a) || isWhite(a))(fileContent)); + auto percentage = (totalLetters * 100) / cast(double)fileContent.length; + + writefln("Percentage of letters: %.0f", percentage); +} +