++i){
str[i]=(char)tolower(str[i]);
}
for(size_t i=0; i<str.length(); ++i){
for(size_t k=1; k<str.length(); ++k){
if(str[i]==str[k]){
return false;
}
}
}
return true;
}
An isogram is a word that has no repeating letters, consecutive or non-consecutive. Implement a function that determines whether a string that contains only letters is an isogram. Assume the empty string is an isogram. Ignore letter case.
Example: (Input --> Output)
"Dermatoglyphics" --> true "aba" --> false "moOse" --> false (ignore letter case)
что может быть неправильно? вроде и сам алгоритм уже на лист переносил
#include <cassert> #include <string> #include <cctype> #include <cstddef> bool is_isogram(const std::string &str) { const std::size_t size= str.size(); if (size <= 1) return true; for(std::size_t i = 0; i != (size - 1); ++i){ for(std::size_t k = (i + 1); k != size; ++k){ if(std::tolower(str[i])==std::tolower(str[k])){ return false; } } } return true; } int main () { assert(is_isogram("Dermatoglyphics") == true); assert(is_isogram("aba") == false); assert(is_isogram("moOse") == false); assert(is_isogram("aa") == false); assert(is_isogram("ab") == true); }
массив bool не лучше?
Обсуждают сегодня