8000 merge by Karolina-Kuza8 · Pull Request #2 · RL-Coding-Class/cpp_string_01 · GitHub
[go: up one dir, main page]

Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 31 additions & 1 deletion string_01.cc
Original file line number Diff line number Diff line change
@@ -1,3 +1,33 @@
#include "string_01.h"
#include <iostream>

using namespace string_toys;

int main(void) {
std::string input;

std::cout << "Podaj dowolny string: ";
std::getline(std::cin, input);

std::cout << "String od tyłu: " << reverse(input) << std::endl;

std::cout << "String z pierwszymi literami słów zamienionymi na duże: " << first_to_upper(input) << std::endl;

std::cout << "Liczba samogłosek w stringu: " << count_vowels(input) << std::endl;

std::cout << "Suma cyfr w stringu: " << sum_digits(input) << std::endl;

std::cout << "Podaj substring do wyszukania w stringu: ";
std::string substr;
std::cin >> substr;
int position = search_substr(input, substr);
if (position != -1) {
std::cout << "Podstring '" << substr << "' znaleziony na pozycji " << position << " w stringu." << std::endl;
} else {
std::cout << "Podstring '" << substr << "' nie został znaleziony w stringu." << std::endl;
}

return 0;
}


auto main() -> int { return 0; }
93 changes: 82 additions & 11 deletions string_01.h
Original file line number Diff line number Diff line change
@@ -1,33 +1,104 @@
#ifndef CPP_STRING_H
#define CPP_STRING_H


#include <iostream>
#include <string>
namespace string_toys {

auto reverse(const std::string & str) -> std::string {
auto reverse(const std::string & str) {
std::string reversedStr;
for (int i = str.length() - 1; i >= 0; --i) {
reversedStr += str[i];
}
return reversedStr;
}

void printReversedString(const std::string &str) {
std::cout << reverse(str) << std::endl;
}

auto first_to_upper(const std::string & str) -> std::string {

auto first_to_upper(const std::string & str) {
std::string result = str;
bool newWord = true; // czy na początku nowego słowa

for (size_t i = 0; i < result.length(); ++i) {
if (std::isspace(result[i])) {
newWord = true; // Jeśli spację, to ustawia flagę na true, bo znaleziono nowe słowo
} else if (newWord) {
result[i] = std::toupper(result[i]); // Zamienia pierwszą literę nowego słowa na dużą literę
newWord = false; // Ustawia flagę na false, aby kolejne litery w tym słowie nie były zamieniane
}
}

return result;
}


auto count_vowels(const std::string & str) -> int {

char vowels_lower[] = {'a', 'e', 'i', 'o', 'u', 'y'};
int counter = 0;
for(char str_el : str) {
for(char vowel_char : vowels_lower) {
std::cout << str_el << std::endl;
if(str_el == vowel_char || str_el == (vowel_char - 32)) {
counter++;
}
}
}
return counter;
}

auto sum_digits(const std::string & str) -> int {

}
auto sum_digits(const std::string & str) {
int sum = 0;

auto search_substr(const std::string & str, const std::string & substr) -> int {
for (char c : str) {
if (std::isdigit(c)) {
sum += c - '0';
}
}

}
return sum;
}

auto custom_serach(const char * str, const char * substr) -> int {

auto search_substr(const std::string & str, const std::string & substr) {
size_t pos = str.find(substr);

if (pos != std::string::npos) {

return static_cast<int>(pos);
} else {
// Jeśli podstring nie został znaleziony, zwraca -1
return -1;
}
}

auto custom_search(const char *str, const char *substr) -> int
{
int pos = -1;
const char *p = str;
const char *q = substr;
while (*p != '\0')
{
if (*p == *q)
{
const char *temp_p = p;
const char *temp_q = q;
while (*temp_p == *temp_q && *temp_p != '\0' && *temp_q != '\0')
{
temp_p++;
temp_q++;
}
if (*temp_q == '\0')
{
pos = p - str;
break;
}
}
p++;
}
return pos;
}
}

#endif /* CPP_STRING_H */
0