#include <iostream>
#include <cctype>
#include <string>
#include <fstream>
char* functionC(char* destination, const char* source)
{
int k = 0;
for (int i = 0; source[i] != '\0'; i++)
{
std::cout << i << ";" << source[i] << std::endl;
if (!isdigit((unsigned char)source[i])) //isDigit проверяет число ли
{
destination[k] = source[i];
k++;
}
}
destination[k] = '\0';
return destination;
}
std::string functionString(const std::string& source)
{
std::string destination = ""; //строка для вывода输出线,输出线
for (int i = 0; i < source.size(); i++)
{
if (!isdigit(source[i]))
{
destination += source[i];
}
}
return destination;
}
int main()
{
std::ifstream infile;
std::ofstream outfile;
infile.open("infile.txt");
outfile.open("outfile1.txt");
if (infile.peek() == std::ifstream::traits_type::eof()) //проверка на пустой файл
{
outfile << "File is empty!\n";
return 1;
}
int length; //длина строки长字符串
infile >> length;
while (!infile.eof())
{
length++; // учитываем символ \0考虑到符号
if (infile.fail() || length <= 0)
{
outfile << "\n";
return 1;
}
char* arrayC1 = new char[length];
char* arrayC2 = new char[length];
arrayC2[0] = '\0';
infile.ignore(length, '\n');
infile.get(arrayC1, length);
for (int i = 0; i < length - 1; i++)
{
if (arrayC1[i] == '\0')
{
outfile << "Invalid string length\n";
return 1;
}
}
std::string str = arrayC1;
outfile << "Source string\t\t\t\t" << str << '\n';
outfile << "With C-style strings:\t" << functionC(arrayC2, arrayC1) << '\n';
outfile << "Using the string class:\t" << functionString(str) << '\n';
delete[] arrayC1;
delete[] arrayC2;
infile >> length;
}
infile.close();
outfile.close();
return 0;
}
infile.txt
10
23rtyrey89
2
s8
7
ылы7тв6
2
M9
5
д9=d%
3
\09
9
во52 рч6л
12
sj78 _@ВЬ6=2
outfile1.txt
Source string: 23rtyrey89
With C-style strings: rtyrey
Using the string class: rtyrey
Source string: s8
With C-style strings: s
Using the string class: s
Source string: ылы7зщ6
With C-style strings: ылызщ
Using the string class: ылызщ
Source string: M9
With C-style strings: M
Using the string class: M
Source string: д9=d%
With C-style strings: д=d%
Using the string class: д=d%
Source string: \09
With C-style strings:
Using the string class: \
Source string: во52 рч6л
With C-style strings: во рчл
Using the string class: во рчл
Source string: sj78 _@ВЬ6=2
With C-style strings: sj78 _@ВЬ6
Using the string class: sj78 _@ВЬ6