consolidate all repos to one for archive
This commit is contained in:
14
semester_2/programiranje_2/naloga0302/README.md
Normal file
14
semester_2/programiranje_2/naloga0302/README.md
Normal file
@@ -0,0 +1,14 @@
|
||||
Napišite razred TextUtility, ki naj ima privatni privzeti konstruktor.
|
||||
|
||||
Razredu dodajte javne razredne metode:
|
||||
|
||||
capitalize(const std::string &str), ki kot argument prejme niz in vrne niz, ki ima za vsakim končnim ločilom (. ,! in ?) veliko začetnico. V primeru, da ugotovimo, da se velika začetnica že nahaja, na tistem mestu ne spremenimo nič. Pazite tudi na to, da je prva črka v nizu z veliko. Primer: lorem ipsum dolor, adipiscing magna? facil isi 2.5 morbi tempus urna id. Gravida non tellus orci! molestieac sed lectus. --> Lorem ipsum dolor, adipiscing magna? Facil isi 2.5 morbi tempus urna id. Gravida non tellus orci! Molestieac sed lectus.
|
||||
toUpperCase(const std::string &str), ki kot argument prejme niz in vrne niz, v katerem so vse črke velike tiskane. Primer: Lorem ipsum dolor 12, adipiscing Magna. --> LOREM IPSUM DOLOR 12, ADIPISCING MAGNA.
|
||||
isNumeric(const std::string &str), ki kot argument prejme niz in preveri, ali se v tem nizu nahajo le števke (0-9) . Primer: "432423" --> true, "4234 234" --> false, "4453asd" --> false
|
||||
contains(const std::string &str, const std::string &substr), ki kot argument prejme niz in iskani niz. Metoda vrne prvi indeks, kje se iskani niz pojavi v nizu. V primeru, da se iskani niz ne nahaja znotraj niza, potem vrnemo -1. Algoritem napišite sami in ni dovoljena uporaba knjižnice. Primer: contains("Lorem ipsum dolor.", "sum") vrne 8, contains("Neobvezna naloga.", "goft") vrne -1
|
||||
|
||||
Razredu dodajte še eno razredno metodo po lastni izbiri (naj bo vezana na tekst)!
|
||||
|
||||
V glavnem programu prikažite delovanje vseh metod. Pri tem pa kličite metode z različnimi argumenti in na ta način preverite, ali metode pokrivajo vse možne scenarije.
|
||||
|
||||
Pomoč za delo z nizi v C++. https://en.cppreference.com/w/cpp/string/basic_string
|
74
semester_2/programiranje_2/naloga0302/TextUtility.cpp
Normal file
74
semester_2/programiranje_2/naloga0302/TextUtility.cpp
Normal file
@@ -0,0 +1,74 @@
|
||||
#include "TextUtility.h"
|
||||
#include <string>
|
||||
#include <cctype>
|
||||
|
||||
std::string TextUtility::capitalize(const std::string &str) {
|
||||
std::string ret;
|
||||
bool nex = false;
|
||||
auto it = str.begin();
|
||||
ret.push_back(std::toupper(*it));
|
||||
it++;
|
||||
while (it != str.end()) {
|
||||
if (nex) {
|
||||
ret.push_back(std::toupper(*it));
|
||||
nex = false;
|
||||
} else {
|
||||
ret.push_back(*it);
|
||||
}
|
||||
if ('.' == *it || '!' == *it || '?' == *it) nex = true;
|
||||
it++;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
std::string TextUtility::toUpperCase(const std::string &str) {
|
||||
std::string ret;
|
||||
auto it = str.begin();
|
||||
while (it != str.end()) {
|
||||
ret.push_back(std::toupper(*it));
|
||||
it++;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
bool TextUtility::isNumeric(const std::string &str) {
|
||||
auto it = str.begin();
|
||||
while (it != str.end() && std::isdigit(*it)) ++it;
|
||||
return !str.empty() && it == str.end();
|
||||
}
|
||||
|
||||
int TextUtility::contains(const std::string &str, const std::string &substr) {
|
||||
std::size_t found = str.find(substr);
|
||||
if (found != std::string::npos) return found;
|
||||
return -1;
|
||||
}
|
||||
|
||||
std::string TextUtility::addSpaces(const std::string &str) {
|
||||
std::string ret;
|
||||
auto it = str.begin();
|
||||
while (it != str.end()) {
|
||||
ret.push_back(*it);
|
||||
ret.push_back(' ');
|
||||
it++;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
std::string TextUtility::removeDuplicatedSpaces(const std::string &str) {
|
||||
std::string ret;
|
||||
bool next = false;
|
||||
auto it = str.begin();
|
||||
while (it != str.end()) {
|
||||
if (next) {
|
||||
if(*it != ' ') {
|
||||
ret.push_back(*it);
|
||||
}
|
||||
next = false;
|
||||
} else {
|
||||
ret.push_back(*it);
|
||||
}
|
||||
if (*it == ' ') next = true;
|
||||
it++;
|
||||
}
|
||||
return ret;
|
||||
}
|
24
semester_2/programiranje_2/naloga0302/TextUtility.h
Normal file
24
semester_2/programiranje_2/naloga0302/TextUtility.h
Normal file
@@ -0,0 +1,24 @@
|
||||
#ifndef NALOGA0302_TEXTUTILITY_H
|
||||
#define NALOGA0302_TEXTUTILITY_H
|
||||
|
||||
#include <string>
|
||||
|
||||
class TextUtility {
|
||||
private:
|
||||
TextUtility() = default;
|
||||
|
||||
public:
|
||||
static std::string capitalize(const std::string &str);
|
||||
|
||||
static std::string toUpperCase(const std::string &str);
|
||||
|
||||
static bool isNumeric(const std::string &str);
|
||||
|
||||
static int contains(const std::string &str, const std::string &substr);
|
||||
|
||||
static std::string addSpaces(const std::string &str);
|
||||
|
||||
static std::string removeDuplicatedSpaces(const std::string &str);
|
||||
};
|
||||
|
||||
#endif //NALOGA0302_TEXTUTILITY_H
|
19
semester_2/programiranje_2/naloga0302/naloga0302.cpp
Normal file
19
semester_2/programiranje_2/naloga0302/naloga0302.cpp
Normal file
@@ -0,0 +1,19 @@
|
||||
#include <iostream>
|
||||
#include "TextUtility.h"
|
||||
|
||||
int main() {
|
||||
std::cout << "capitalize \n";
|
||||
std::cout << "Ni.kola!na: " << TextUtility::capitalize("Ni.kola!na") << "\n";
|
||||
std::cout << "toUpperCase \n";
|
||||
std::cout << "nikola!: " << TextUtility::toUpperCase("nikola!") << "\n";
|
||||
std::cout << "isNumeric \n";
|
||||
std::cout << "59 " << TextUtility::isNumeric("59") << "\n";
|
||||
std::cout << "nik " << TextUtility::isNumeric("nik") << "\n";
|
||||
std::cout << "contains \n";
|
||||
std::cout << "pa in nikola: " << TextUtility::contains("nikola", "pa") << "\n";
|
||||
std::cout << "ol in nikola: " << TextUtility::contains("nikola", "ol") << "\n";
|
||||
std::cout << "addSpaces \n";
|
||||
std::cout << "nikola: " << TextUtility::addSpaces("nikola") << "\n";
|
||||
std::cout << "nikola hi how are you: " << TextUtility::removeDuplicatedSpaces("nikola hi how are you") << "\n";
|
||||
return 0;
|
||||
}
|
Reference in New Issue
Block a user