diff --git a/src/fun.cpp b/src/fun.cpp index 418f426..d9937d4 100644 --- a/src/fun.cpp +++ b/src/fun.cpp @@ -1,14 +1,134 @@ -// Copyright 2022 UNN-IASR #include "fun.h" -unsigned int faStr1(const char *str) { - return 0; +bool isSpace(char c) +{ + return c == ' '; } -unsigned int faStr2(const char *str) { - return 0; +bool isDigit(char c) +{ + return c >= '0' && c <= '9'; } -unsigned int faStr3(const char *str) { - return 0; +bool isUpperLatin(char c) +{ + return c >= 'A' && c <= 'Z'; +} + +bool isLowerLatin(char c) +{ + return c >= 'a' && c <= 'z'; +} + +// Задача 1 + +unsigned int faStr1(const char *str) +{ + if (str == nullptr) + return 0; + + unsigned int count = 0; + int i = 0; + + while (str[i] != '\0') + { + while (str[i] != '\0' && isSpace(str[i])) + i++; + + if (str[i] == '\0') + break; + + bool hasDigit = false; + + while (str[i] != '\0' && !isSpace(str[i])) + { + if (isDigit(str[i])) + hasDigit = true; + i++; + } + + if (!hasDigit) + count++; + } + + return count; +} + +// Задача 2 +unsigned int faStr2(const char *str) +{ + if (str == nullptr) + return 0; + + unsigned int count = 0; + int i = 0; + + while (str[i] != '\0') + { + while (str[i] != '\0' && isSpace(str[i])) + i++; + + if (str[i] == '\0') + break; + + bool valid = true; + int wordLength = 0; + + if (!isUpperLatin(str[i])) + valid = false; + + wordLength++; + i++; + + while (str[i] != '\0' && !isSpace(str[i])) + { + if (!isLowerLatin(str[i])) + valid = false; + + wordLength++; + i++; + } + + if (valid && wordLength > 0) + count++; + } + + return count; +} + +// Задача 3 + +unsigned int faStr3(const char *str) +{ + if (str == nullptr) + return 0; + + unsigned int wordCount = 0; + unsigned int totalLength = 0; + int i = 0; + + while (str[i] != '\0') + { + while (str[i] != '\0' && isSpace(str[i])) + i++; + + if (str[i] == '\0') + break; + + unsigned int currentLength = 0; + + while (str[i] != '\0' && !isSpace(str[i])) + { + currentLength++; + i++; + } + + totalLength += currentLength; + wordCount++; + } + + if (wordCount == 0) + return 0; + + return (totalLength + wordCount / 2) / wordCount; } diff --git a/src/main.cpp b/src/main.cpp index 89bbf61..d0b4683 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,6 +1,25 @@ // Copyright 2022 UNN-IASR #include "fun.h" -int main() { +#include + +int main() +{ + const char *s1 = "abc test12 hello world7 code"; + const char *s2 = "Apple Orange banana Cat Dog X Yz John Smith A1 Test"; + const char *s3 = " one three seven "; + + std::cout << "Строка 1: \"" << s1 << "\"" << std::endl; + std::cout << "faStr1 = " << faStr1(s1) << std::endl; + std::cout << std::endl; + + std::cout << "Строка 2: \"" << s2 << "\"" << std::endl; + std::cout << "faStr2 = " << faStr2(s2) << std::endl; + std::cout << std::endl; + + std::cout << "Строка 3: \"" << s3 << "\"" << std::endl; + std::cout << "faStr3 = " << faStr3(s3) << std::endl; + std::cout << std::endl; + return 0; }