#include "iostream"
#include "string"
#include "vector"
using namespace std;
int DemCau(string str);
string ChuanHoaChuoiKieu1(string str);
string ChuanHoaChuoiKieu2(string str);
void DemTu(string str, int n);
void main()
{
int cs[100];
string str = "Sang som da 28-30 do C, mo hoi chay uot dam lung ao. Trua nong hon 38 do. Hoang hon, mat duong Sai Gon lai ham hap pha vao mat nguoi. Nhung ngay qua nguoi dan keu troi vi nong. Tai khu phong tro cong nhan tren duong Bui Van Ba, quan 7, 22h dem ngay 9/5, hang chuc nam nu cong nhan khu che xuat Tan Thuan thay vi ngu lay suc de sang di lam, lai keo nhau ra duong ngoi hong gio. Nam, mot cong nhan trong nhom cho hay, phong tro cua ho von da chat choi, nay lai them cai nong hung huc vua dat lung xuong da chay mo hoi, mo quat thay toan hoi nong nen khong the ngu duoc.";
//Chuan hoa theo kieu 1 (xoa khoang trang du, doi hoa thanh thuong, xoa cac ky tu . ! ? ,)
string str1 = ChuanHoaChuoiKieu1(str);
cout << str1;
//Chuan hoa theo kieu 2 (xoa khoang trang du, xoa cac ky tu,)
string str2 = ChuanHoaChuoiKieu2(str);
cout << "\n" << str2;
//Dem cau
int kq1 = DemCau(str);
cout << "\n-------------------------------------------------------------------";
cout << "\nSo luong cau la: " << kq1;
//Dem tu
DemTu(str, kq1);
vector<string> s1(1000);
int k1 = 0;
int i1 = 0;
for(int j1 = 0; j1 < str1.length(); j1++)
{
if( str1[j1] == ' ')
{
s1[k1] = str1.substr(i1, j1-i1);
k1++;
i1 = j1+1;
}
}
s1.resize(k1);
//Tim tu xuat hien nhieu nhat
int max = 0;
int sl = 0;
int m, n;
for(m = 0; m < k1-1; m++)
{
int dem = 1;
for(n = m+1; n < k1; n++)
{
if( s1[m] == s1[n])
{
dem++;
}
}
if( dem > max )
{
max = dem;
sl = 1;
cs[0] = m;
}
else
{
if (max == dem)
{
sl++;
cs[sl-1] = m;
}
}
}
cout << "\nSo lan xuat hien nhieu nhat cua tu: " << max;
cout << "\nBao gom cac tu: ";
for(int p = 0; p < sl; p++)
{
cout << s1[cs[p]] << " ";
}
//Sap xep tu trong moi cau theo thu tu tu dien
cout << "\nSap xep tang dan cac tu(theo thu tu tu dien) trong moi cau:\n";
vector<string> s2(1000);
int k2 = 0;
int i2 = 0;
for(int j2 = 0; j2 < str2.length(); j2++)
{
if( str2[j2] == ' ')
{
s2[k2] = str2.substr(i2, j2-i2);
k2++;
i2 = j2+1;
}
}
s2.resize(k2);
//
int j3 = 0;
for(int i3 = 0; i3 < kq1; i3++)
{
vector<string> temp(1000); //hello new day. alo alo.
for(; j3 < s2.size(); )
{
temp[j3] = s2[j3];
if(temp[j3][temp[j3].size()-1] == '.' || temp[j3][temp[j3].size()-1] == '?' || temp[j3][temp[j3].size()-1] == '!' )
{
j3++;
temp.resize(j3);
for(int i4 = 0; i4 < temp.size() - 1; i4++)
{
for(int j4 = i4+1; j4 < temp.size(); j4++)
{
if(temp[i4] > temp[j4])
{
string tam = temp[i4];
temp[i4] = temp[j4];
temp[j4] = tam;
}
}
}
/*int ktMax(string a, string b)
{
string t1 = a;
string t2 = b;
int i, j;
for(i = 0; i < t1.length(); i++)
{
if( t1
>= 'A' && t1 <= 'Z')
t1 = t1 + 32;
}
tuong tu cho t2
if( t1 == t2 )
{
if(a[0] > b[0] )
return 1;
}
else
{
if(t1 > t2 )
return 1;
else
return 0;
}
*/
cout << "\n";
for(int i5 = 0; i5 < temp.size(); i5++)
{
cout << temp[i5] << " ";
}
cout << "\n";
break;
}
else
{
j3++;
}
}
}
}
int DemCau(string str)
{
int dem = 0;
for(int i =0; i < str.length() ; i++)
{
if (str == '.' || str == '?' || str == '!')
dem++;
}
return dem;
}
void DemTu(string str, int n)
{
vector<string> s1(1000);
int k1 = 0;
int i1 = 0;
for(int j1 = 0; j1 < str.length(); j1++)
{
if( str[j1] == ' ')
{
s1[k1] = str.substr(i1, j1-i1);
k1++;
i1 = j1+1;
}
}
s1.resize(k1);
//Dem tu moi cau
cout << "\nSo luong tu trong moi cau la: " ;
int j2 = 0;
int k =0;
for(int i2 = 0; i2 < n; i2++)
{
vector <string> s2(1000);
for(; j2 < s1.size()
{
s2[j2] = s1[k++];
if(s2[j2][s2[j2].size()-1] == '.' || s2[j2][s2[j2].size()-1] == '!' || s2[j2][s2[j2].size()-1] == '?' )
{
j2++;
s2.resize(j2);
cout << "\nCau thu " << i2+1 << ": " << j2 << " tu: ";
for(int i3 = 0; i3 < s2.size(); i3++)
cout << s2[i3] << " ";
j2 = 0;
break;
}
else
{
j2++;
}
}
}
}
string ChuanHoaChuoiKieu1(string str)
{
string strcpy = string(str, 0, str.length());
int n = strcpy.length();
while (strcpy[0] == '0')
strcpy.erase(0,1);
while (strcpy[n-1] == '0')
strcpy.erase(n-1, 1);
//Xoa cac khoang trang du
for(int i = n - 2; i >= 1; i--)
{
if(strcpy == ' ' && strcpy[i-1] == ' ')
strcpy.erase(i,1);
}
int m = strcpy.length();
//Xoa cac dau . , ! ?
for(int i = m -1; i >=0; i--) //ko dung n nua vi luc nay n da thay doi
{
if(strcpy == '.' || strcpy == '!' || strcpy == '?' || strcpy == ',')
strcpy.erase(i,1);
}
//Doi chu hoa thanh chu thuong
for(int j = 0; j < strcpy.length(); j++)
{
if(strcpy[j] >= 'A' && strcpy [j] <= 'Z')
{
strcpy[j] = strcpy[j] + 32;
}
}
return strcpy;
}
string ChuanHoaChuoiKieu2(string str)
{
string strcpy = string(str, 0, str.length());
while (strcpy[0] == '0')
strcpy.erase(0,1);
while (strcpy[strcpy.length()-1] == '0')
strcpy.erase(strcpy.length()-1, 1);
//Xoa cac khoang trang du
for(int i = strcpy.length() - 2; i >= 1; i--)
{
if(strcpy == ' ' && strcpy[i-1] == ' ')
strcpy.erase(i,1);
}
//Xoa cac dau . , ! ?
for(int i = strcpy.length() -1; i >=0; i--) //ko dung n nua vi luc nay n da thay doi
{
if(strcpy == ',')
strcpy.erase(i,1);
}
return strcpy;
}