Другие языки программирования и технологии

c++ в словах, которые начинаются и заканчиваются одной и той же буквой, послед и первая буква должны стать большими

работает почему-то только с первым словом, остальные почему-то игнорит
for (int i = 0; i < 1000; i++)
{
if(text[i] == ' ' || text[i] == ',' || text[i] == '.' || text[i] == '!' || text[i] == '?' || text[i] == '\n')
{ }
for (int j = i+1; j < 1000; j++)
{
if (isalpha(text[i]) && isalpha(text[j]) && text[i] == text[j] &&(text[j + 1]==' '|| text[j + 1] == ','|| text[j + 1] == '.' || text[j + 1] == '!' || text[j + 1] == '?' || text[j + 1] == '\n'))
{
text[i] = toupper(text[i]);
text[j] = toupper(text[j]);
i = i + j;
break;
}
if(text[j + 1] == ' ' || text[j + 1] == ',' || text[j + 1] == '.' || text[j + 1] == '!' || text[j + 1] == '?' || text[j + 1] == '\n')
{
i = i + j; break;
}
}

}
Парсинг слов в предложении - сто раз решенная задача, для нее пишется конечный автомат или же используется istringstream.
А не такое спагетти, как тут (( С таким ужасом C++ не осилить, НИЧЕГО работать не будет.
**
***rena ***
92 464
Лучший ответ
#include < iostream>
#include < vector>
#include < string>

int main() {
std::vector< std::string> words {"", "abc", "aba"};
for (auto& word: words) {
if (word.empty()) { continue; }
auto& f = word.front();
auto& b = word.back();
if (f == b && std::isalpha(f)) {
f = std::toupper(f);
b = f;
}
}

for (auto&& i : words)
std::cout < < i < < "\n";
}
***rena *** Смысл в этом без парсинга слов?
И еще видно, что over-complicated, если f == b, а f - буква, то зачем b то проверять.
http://coliru.stacked-crooked.com/a/fbfaccf151c9282b

#include <algorithm>
#include <iostream>
#include <regex>
#include <string>

auto main() -> int
{
std::regex re{R"~([/.,\s]+)~"};

std::string str = "A fishman brought a fishf. fish fishman fishers. lol. abracadabra saasj sa bacb ";
std::sregex_token_iterator begin{std::begin( str ), std::end( str ), re, -1}, end;

std::for_each( begin, end, [&str]( const std::ssub_match & match ) {
auto first = std::distance( std::cbegin( str ), match.first );
auto last = std::distance( std::cbegin( str ), match.second );
if ( first != last && (--last, str[first] == str[last]) )
str[first] = str[last] = std::toupper( *match.first );
} );

std::cout << str << std::endl;

return 0;
}
Женя Каштанов
Женя Каштанов
14 578
самыми простыми свойствами языка
https://pastebin.com/2L1c0Ksa

Похожие вопросы