Строки

Пример 1:

В каждой строке текста найти символ[ы], который встречается наименьшее количество раз.

Решение от преподавателя:

#include
#include
#include
#include
#include
#include
#include
#include

using namespace std;
void Fun1(string A)
{
int b=0, c=10000;
char a1, a2, a3;
for (int i=0; i{
b=0;
if(A[i]!=' ')
{
a1=A[i];
for(int j=0; j{
if(a1==A[j])
{
b=b+1;
}
}
if(b{
c=b;
a2=A[i];
}
}
}
cout<<"char["<}

int main()
{setlocale (0, " Russian");
string st( "ssssss dddddddddd iii ffffff" );
cout<Fun1(st);
getch();
}

Пример 2:

В тексте найти общее количество цифр.

Решение от преподавателя:

#include "StdAfx.h"
#include
#include
#include
using namespace std;
const int n=1000;

class text
{
int amt;

public:

text(char * str)
{
cin.getline(str, n);
}

int amtDigits(char*str)
{
int i=0;
amt=0;
while(*(str+i)!='\0')
{
if (isdigit(*(str+i)))
amt++;
i++;
}
return amt;
}
};

int main()
{
char *str=new char[1000];
text string(str);
cout << string.amtDigits(str);
getch();
return 0;

}

Пример 3:

В тексте одно введённое слово заменить другим словом, которое так-же вводится с экрана. При выводе исходной и изменённой строки старое и новое слово выделить другим цветом.

Решение от преподавателя:

#include
#include
#include
using namespace std;

//находим в первой строке элементы(индексы этих элементов), которые будем заменять
int substring(string s1, string s2, int c) {
for (int j=c; j < s1.length() - s2.length()+1; j++)
{
if (s1[j] == s2[0]) {
bool issubstr = true;
for ( int i = 1; i < s2.length(); i++)
{
if (s1[j+i] != s2[i]) {
issubstr = false;
break;
}
}
if(issubstr) {
return j;
}
}
}
return -1;
}

void all_substrings(string s1, string s2, int* &res, int &num)
{
num = 0;
int c = 0;
bool sub = false;
do {
int index = substring(s1, s2, c);
if (index == -1)
{
sub = false;
}
else
{
num++;
sub = true;
c = index+1;
}
}while(sub);

res = new int[num];
c = 0;
sub = false;
int i = 0;
do {
int index = substring(s1, s2, c);
if (index == -1)
{
sub = false;
return;
}
else
{
sub = true;
c = index+1;
res[i] = index;
i++;
}
}while(sub);
}

//делит строку s по разделителю splitter, массив результата помещает в res, его размер в num
void split(string s, string splitter, string* &res, int &num) {
int n; int* spl_index;
all_substrings(s, splitter, spl_index, n);
res = new string[n+1];
int prev = 0;
for(int i=0;ires[i] = s.substr(prev, spl_index[i]-prev);
prev = spl_index[i]+splitter.length();
}
res[n] = s.substr(prev, s.length()-prev);
num = n+1;
}
//заменяет
void replace(string s, string s1, string s2, int* &index, string &res, int &replaces)
{
string *splitted; int n;
split(s, s1, splitted, n);
res = "";
replaces = n-1;
index = new int[n-1];
for(int i=0;ires += splitted[i]; //"12" + "34" = "1234"
if(i != n-1) {
index[i] = res.length();
res += s2;
}
}

}
int main ()
{
HANDLE hConsole;
hConsole = GetStdHandle(STD_OUTPUT_HANDLE);

string s = ""; //"12" "5" "" "12" ""
string s2;
string s3;
string res;
string temp;

cout << "Enter the source text (enter \"#\"" to continue): \n"";
//getline(cin, s);
//cin >> s;
getline(cin, temp);
while(temp != ""#""){
s += temp;
s += ""\n"";
getline(cin, temp);
}
cout << ""Enter the string to be replaced: \n"";
//cin >> s2;
getline(cin, s2);

Не нашли нужного вам решения? Оставьте заявку и наши авторы быстро и качественно помогут вам с решением.
Оставить заявку
Работа вам нужна срочно. Не волнуйтесь, уложимся!

Заполните, пожалуйста, данные для автора:

  • 22423 авторов готовы помочь тебе.
  • 2402 онлайн