#include <iostream>
#include <string>
std::string LamDepTen(std::string Input)
{
//Dùng biến Temp làm bản nháp để tính toán
std::string Temp = "";
//Loại các ký tự không phải chữ cái và dấu cách
for (int i = 0; i < Input.length(); i += 1)
{
if (isalpha(Input[i]) || (Input[i] == ' '))
Temp += Input[i];
}
Input = Temp;
//Nếu loại xong mà chuỗi không còn gì thì trả về luôn
if (Input.length() == 0)
return Temp;
//Loại các ký tự cách liên tiếp
Temp = "" + Input[0];
for (int i = 1; i < Input.length(); i += 1)
if (Input[i - 1] != ' ' || Input[i] != ' ')
Temp += Input[i];
Input = Temp;
//Nếu loại xong mà chuỗi chỉ còn dấu cách thì trả về khoảng trắng
if (Input == " ")
return "";
//Chuyển tất cả về chữ cái thường
Temp = "";
for (int i = 0; i < Input.length(); i += 1)
{
if (Input[i] >= 'A' && Input[i] <= 'Z')
Temp += (char)(Input[i] + 32);
else
Temp += Input[i];
}
Input = Temp;
//Thêm dấu space vào đầu chuỗi
if (Input[0] != ' ')
Input = ' ' + Input;
//Loại dấu space ở cuối cùng chuỗi
Temp = "";
for (int i = 0; i < Input.length()-1; i += 1)
{
Temp += Input[i];
}
if (Input[Input.length() - 1] != ' ')
Temp += Input[Input.length() - 1];
Input = Temp;
//Chuyển các ký tự sau dấu space thành chữ hoa
Temp = " ";
for (int i = 1; i < Input.length(); i +=1)
{
if (Input[i - 1] == ' ')
Temp += (char)(Input[i] - 32);
else
Temp += Input[i];
}
Input = Temp;
//Loại ký tự space ở đầu chuỗi
Temp = "";
for (int i = 1; i < Input.length(); i += 1)
Temp += Input[i];
Input = Temp;
//Trả về kết quả
return Input;
}
int main()
{
std::string TenBanDau = "@#TrA123N H!~!#$#@o<><><,.,.,aN";
std::cout << "Nhap ten can lam dep: " << TenBanDau << std::endl;
std::string TenDaLamDep = LamDepTen(TenBanDau);
std::cout << "Ten da lam dep: " << TenDaLamDep;
return 0;
}