
C/C++
Помогите с задачей по С++

#include <iostream>
#include <string>
#include <sstream>
#include <vector>
#include <cctype>
using namespace std;
string capitalized(const string& word) {
string box;
box += toupper(word.front());
for (auto i = 1U; i < word.length(); ++i) box += tolower(word[i]);
return box;
}
string uppercase(const string& word) {
string box;
for (auto i = 0U; i < word.length(); ++i) box += toupper(word[i]);
return box;
}
bool findupper(const string& word) {
for (auto letter : word) if (isupper(letter)) return true;
return false;
}
int main() {
string text;
getline(cin, text);
istringstream iss(text);
vector<string> box;
string line;
while (getline(iss, line, '.')) box.push_back(line);
text.clear();
for (const auto& item : box) {
istringstream ss(item);
vector<string> words;
string word;
while (ss >> word) words.push_back(word);
for (auto i = 0; i < words.size(); ++i) {
if (i && findupper(words[i])) words[i] = uppercase(words[i]);
else if (!i) words[i] = capitalized(words[i]);
text += words[i] + ' ';
}
text.pop_back();
text += ". ";
}
text.pop_back();
cout << text;
system("pause > nul");
}
P.S. Работает только с Латиницей
#include <string>
#include <sstream>
#include <vector>
#include <cctype>
using namespace std;
string capitalized(const string& word) {
string box;
box += toupper(word.front());
for (auto i = 1U; i < word.length(); ++i) box += tolower(word[i]);
return box;
}
string uppercase(const string& word) {
string box;
for (auto i = 0U; i < word.length(); ++i) box += toupper(word[i]);
return box;
}
bool findupper(const string& word) {
for (auto letter : word) if (isupper(letter)) return true;
return false;
}
int main() {
string text;
getline(cin, text);
istringstream iss(text);
vector<string> box;
string line;
while (getline(iss, line, '.')) box.push_back(line);
text.clear();
for (const auto& item : box) {
istringstream ss(item);
vector<string> words;
string word;
while (ss >> word) words.push_back(word);
for (auto i = 0; i < words.size(); ++i) {
if (i && findupper(words[i])) words[i] = uppercase(words[i]);
else if (!i) words[i] = capitalized(words[i]);
text += words[i] + ' ';
}
text.pop_back();
text += ". ";
}
text.pop_back();
cout << text;
system("pause > nul");
}
P.S. Работает только с Латиницей
Дядя Саша 🖐️
Спасибо тебе, добрый человек! Желаю здоровья тебе и твоей семье!
#include < iostream >
#include < sstream >
#include < windows.h >
using namespace std;
char* tosmall, *tobig;
void create()
{
const char* str = "ёйцукенгшщзхъфывапролджэячсмитьбюqwertyuiopasdfghjklzxcvbnm";
const char* STR = "ЁЙЦУКЕНГШЩЗХЪФЫВАПРОЛДЖЭЯЧСМИТЬБЮQWERTYUIOPASDFGHJKLZXCVBNM";
tobig = new char[256];
for (int i = 0; i < 256; i++)
tobig[i] = i;
for (int i = 0; i < 59; i++)
tobig[str[i] + 128] = STR[i] + 128;
tosmall = new char[256];
for (int i = 0; i < 256; i++)
tosmall[i] = i;
for (int i = 0; i < 59; i++)
tosmall[STR[i] + 128] = str[i] + 128;
}
void to_big(string& word)
{
for (char& c : word) c = tobig[c + 128] - 128;
}
void to_small(string& word)
{
for (char& c : word) c = tosmall[c + 128] - 128;
}
bool is_small(char& c)
{
return c == tosmall[c + 128] - 128 && c != tobig[c+128]-128;
}
bool is_big(char& c)
{
return c == tobig[c + 128] - 128 && c != tosmall[c+128]-128;
}
bool is_up2(string& word)
{
int count = 0;
for (char&c:word)
{
count += is_big(c);
if (count == 2) return true;
}
return false;
}
int main()
{
SetConsoleOutputCP(1251);
SetConsoleCP(1251);
create();
bool nw_flag = true;
string source, word;
getline(cin, source);
istringstream input(source);
while (input >> word)
{
if (is_up2(word))
{
to_big(word); if (nw_flag&& word[word.size() - 1] != '.') nw_flag = false;
cout << word << " "; continue;
}
if (nw_flag) { if(word[word.size() - 1] != '.') nw_flag = false; to_small(word); word[0] = tobig[word[0] + 128] - 128; cout << word << " "; continue; }
if (word[word.size() - 1] == '.') nw_flag = true;
to_small(word); cout << word << " ";
}
}
#include < sstream >
#include < windows.h >
using namespace std;
char* tosmall, *tobig;
void create()
{
const char* str = "ёйцукенгшщзхъфывапролджэячсмитьбюqwertyuiopasdfghjklzxcvbnm";
const char* STR = "ЁЙЦУКЕНГШЩЗХЪФЫВАПРОЛДЖЭЯЧСМИТЬБЮQWERTYUIOPASDFGHJKLZXCVBNM";
tobig = new char[256];
for (int i = 0; i < 256; i++)
tobig[i] = i;
for (int i = 0; i < 59; i++)
tobig[str[i] + 128] = STR[i] + 128;
tosmall = new char[256];
for (int i = 0; i < 256; i++)
tosmall[i] = i;
for (int i = 0; i < 59; i++)
tosmall[STR[i] + 128] = str[i] + 128;
}
void to_big(string& word)
{
for (char& c : word) c = tobig[c + 128] - 128;
}
void to_small(string& word)
{
for (char& c : word) c = tosmall[c + 128] - 128;
}
bool is_small(char& c)
{
return c == tosmall[c + 128] - 128 && c != tobig[c+128]-128;
}
bool is_big(char& c)
{
return c == tobig[c + 128] - 128 && c != tosmall[c+128]-128;
}
bool is_up2(string& word)
{
int count = 0;
for (char&c:word)
{
count += is_big(c);
if (count == 2) return true;
}
return false;
}
int main()
{
SetConsoleOutputCP(1251);
SetConsoleCP(1251);
create();
bool nw_flag = true;
string source, word;
getline(cin, source);
istringstream input(source);
while (input >> word)
{
if (is_up2(word))
{
to_big(word); if (nw_flag&& word[word.size() - 1] != '.') nw_flag = false;
cout << word << " "; continue;
}
if (nw_flag) { if(word[word.size() - 1] != '.') nw_flag = false; to_small(word); word[0] = tobig[word[0] + 128] - 128; cout << word << " "; continue; }
if (word[word.size() - 1] == '.') nw_flag = true;
to_small(word); cout << word << " ";
}
}
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<ctype.h>
void FirstWord(char *word)
{
word[0]=toupper(word[0]);
for(int i = 1; i < strlen(word);
i++)
word[i]=tolower(word[i]);
}
void TooMany(char *word)
{
int count=0;
for(int i = 0; i < strlen(word);
i++)
if(isupper(word[i])) count++;
if(count>1)
for(int i = 0; i < strlen(word);
i++)
word[i]=toupper(word[i]);
else
for(int i = 0; i < strlen(word);
i++)
word[i]=tolower(word[i]);
}
void Group(char *text,char *group,char *substr)
{
substr=strstr(text,".");
substr++;
strncpy(group,text,
strlen(text)-strlen(substr)+1);
strcpy(text,substr);
}
int main()
{
char text[1024]={0},
restext[1024]={0},
resword[50]={0},
group[256]={0},
*word,
*substr;
int wcount=0;
printf("Enter your text:\n");
fgets(text,sizeof(text),stdin);
text[strlen(text)-2]='.';
while(strlen(text)>1)
{//while strlen
Group(text,group,substr);
word=strtok(group," ");
wcount=0;
while(word!=NULL)
{//while word
if(!wcount)
{
FirstWord(word);
wcount++;
}
else TooMany(word);
strcpy(resword,"\0");
sprintf(resword,"%s ",word);
strcat(restext,resword);
word=strtok(NULL," ");
}//while word
}//while strlen
printf("\n\nResult:\n%s",restext);
system("pause");
return 0;
}

#include<string.h>
#include<stdlib.h>
#include<ctype.h>
void FirstWord(char *word)
{
word[0]=toupper(word[0]);
for(int i = 1; i < strlen(word);
i++)
word[i]=tolower(word[i]);
}
void TooMany(char *word)
{
int count=0;
for(int i = 0; i < strlen(word);
i++)
if(isupper(word[i])) count++;
if(count>1)
for(int i = 0; i < strlen(word);
i++)
word[i]=toupper(word[i]);
else
for(int i = 0; i < strlen(word);
i++)
word[i]=tolower(word[i]);
}
void Group(char *text,char *group,char *substr)
{
substr=strstr(text,".");
substr++;
strncpy(group,text,
strlen(text)-strlen(substr)+1);
strcpy(text,substr);
}
int main()
{
char text[1024]={0},
restext[1024]={0},
resword[50]={0},
group[256]={0},
*word,
*substr;
int wcount=0;
printf("Enter your text:\n");
fgets(text,sizeof(text),stdin);
text[strlen(text)-2]='.';
while(strlen(text)>1)
{//while strlen
Group(text,group,substr);
word=strtok(group," ");
wcount=0;
while(word!=NULL)
{//while word
if(!wcount)
{
FirstWord(word);
wcount++;
}
else TooMany(word);
strcpy(resword,"\0");
sprintf(resword,"%s ",word);
strcat(restext,resword);
word=strtok(NULL," ");
}//while word
}//while strlen
printf("\n\nResult:\n%s",restext);
system("pause");
return 0;
}

Михаил Конышев
Можно сделать такой же исходник для кириллицы, заменив все функции для строк char на функции для строк wchar_t
Дядя Саша 🖐️
Спасибо тебе, замечательный человек. Желаю счастья теье и твоей семье!
Похожие вопросы
- Помогите решить задачу по программированию на C++
- СРОЧНО! Помогите с задачей.
- ПОМОГИТЕ С ЗАДАЧЕЙ НА C++
- Помогите решить задачу по C++!
- Помогите решить задачу по программированию
- Помогите решить задачу пожалуйста, в C++
- Помогите решить задачу на c++
- Помогите с задачей на языке СИ
- Помогите решить задачу на С++ (мне не совсем ясен смысл задания)
- Помогите решить задачу на С++, используя статические массивы