string

#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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值