Создание функции std::size_t String::find(char ch), которая будет находить символ ch и возвращать индекс местоположения;
Если функция не находит символ, то возвращает - 1;
НЕ ИСПОЛЬЗУЯ сторонние функции
C/C++
Создание функции std::size_t String::find(char ch)
#include <iostream>
#include <string>
using namespace std;
struct String {
string s;
String(const char* s) : s(s) {}
String(const string& s) : s(s) {}
String(string&& s) : s(move(s)) {}
size_t find(const char ch)const {
const auto n = s.length();
for (auto i = 0U; i < n; ++i) if (s.at(i) == ch) return i;
return npos;
}
friend istream& operator>>(istream& inp, String& s) {
return inp >> s.s;
}
friend ostream& operator<<(ostream& out, const String& s) {
return out << s.s;
}
static const size_t npos = -1;
};
int main(void) {
String hello = "Hello world!";
cout << hello << '\n';
const char w = 'w';
auto index = hello.find(w);
cout << "Letter \'" << w << "\' - index: ";
if (index != String::npos) cout << index << '\n';
else puts("not found");
const char b = 'b';
index = hello.find(b);
cout << "Letter \'" << b << "\' - index: ";
if (index != String::npos) cout << index << '\n';
else puts("not found");
system("pause > nul");
}
#include <string>
using namespace std;
struct String {
string s;
String(const char* s) : s(s) {}
String(const string& s) : s(s) {}
String(string&& s) : s(move(s)) {}
size_t find(const char ch)const {
const auto n = s.length();
for (auto i = 0U; i < n; ++i) if (s.at(i) == ch) return i;
return npos;
}
friend istream& operator>>(istream& inp, String& s) {
return inp >> s.s;
}
friend ostream& operator<<(ostream& out, const String& s) {
return out << s.s;
}
static const size_t npos = -1;
};
int main(void) {
String hello = "Hello world!";
cout << hello << '\n';
const char w = 'w';
auto index = hello.find(w);
cout << "Letter \'" << w << "\' - index: ";
if (index != String::npos) cout << index << '\n';
else puts("not found");
const char b = 'b';
index = hello.find(b);
cout << "Letter \'" << b << "\' - index: ";
if (index != String::npos) cout << index << '\n';
else puts("not found");
system("pause > nul");
}
тупой перебор в цикле по длине строки, если равны, возврат текущего индекса. за циклом возврат - 1. только вот size_t - беззнаковый.
И в чем проблема? Перебираем строку. Если нашли - просто возвращаем индекс.
Если вышли из цикла (так и не нашли) - то возвращаем -1
Если вышли из цикла (так и не нашли) - то возвращаем -1
Такая уже есть, только если она не находит символ, то она возвращает std:: string::npos
Евгений Зотин
ему физкультура нужна судя по всему на С...
Похожие вопросы
- WINAPI C++ string в char
- С++. Является ли замена string на char, где это возможно, оптимизацией кода?
- СОЗДАНИЕ ФУНКЦИЙ В C++
- Как конвертировать Char или Tchar или wchar_t в LPWSTR?
- Какой самый простой способ привести std::string в код ASCII?
- Различие указателей int *; и char *;
- Значение типа const char* нельзя присвоить сущности типа char*
- Программа на C++ с переводом Char в Int и наоборот при переводе возвращает НИЧЕГО
- Что такое #include <iostream>, std using namespace std В языке программирования C++?
- Сравнение элементов массива типа char с символом типа char