max=string.Copy(tmp);
tmp="";
}
}
return max;
}
static void hienTu(string s)
{
Console.WriteLine("Cac tu co o trong xau");
char[]dau={' ','?','.','!'};
foreach(string con in s.Split(dau))
if(con.Length>0)
Console.WriteLine(con);
}
static void Main()
{
string s;
Console.Write("Nhap xau:");s=Console.ReadLine();
//Câu a
Console.WriteLine(" So ky tu la:" + demKhongChuSo(s));
//Câu b
Console.WriteLine("So cum tu mua xuan co trong xau:"
+demXauCon("mua xuan",s));
//Câu c
Console.WriteLine("Xau can tim la:" + timXauLon(s));
//Câu d
hienTu(s);
Console.ReadKey();
}
}
---------------------------------------------------------------------------------------------
Ví dụ 2: Tìm tất cả các số tự nhiên không quá n sao cho số đó trùng với phần cuối của bình phương chính nó. Chẳng hạn:62=36; 252=625
---------------------------------------------------------------------------------------------
using System;
class BaiXau
{
static bool KiemTra(long i)
{
bool ok;
string s=Convert.ToString(i*i);
string con=i.ToString();
if(string.Compare(s.Substring(s.Length-con.Length,con.Length),con)==0)
ok=true;
else
ok=false;
return ok;
}
static void Main()
{
long n;
Console.Write("Nhap n=");n=long.Parse(Console.ReadLine());
Console.WriteLine("Cac so thoa ma yeu cau bai toan:");
for(long i=1;i<=n;++i)
if(KiemTra(i))
Console.WriteLine(i+ "-->"+i*i );
Console.ReadKey();
}
}
---------------------------------------------------------------------------------------------
Ví dụ 3: Nhập vào một xâu ký tự s. Sau đó thực hiện các yêu cầu sau:
- Xoá đi trong xâu s các dấu cách thừa: ở bên trái, bên phải và dấu cách thừa ở giữa xâu, gữa các từ chỉ để lại một dấu cách
- Xoá đi tất cả các xâu con có nội dung là “mua xuan” có ở trong xâu s
- Sắp xếp các từ trong xâu s theo thứ tự từ điển
- Đếm tần xuất xuất hiện của các từ có trong xâu s
Chú ý: Từ là các ký tự viết liền nhau và cách nhau bởi dấu cách
---------------------------------------------------------------------------------------------
using System;
class BaiXau
{
static string LTrim(string s)
{
string tmp=string.Copy(s);
while(tmp[0]==' ') tmp=tmp.Remove(0,1);
return tmp;
}
static string RTrim(string s)
{
string tmp=string.Copy(s);
while(tmp[tmp.Length-1]==' ') tmp=tmp.Remove(tmp.Length-1,1);
return tmp;
}
static string Trim(string s)
{
string tmp=string.Copy(s);
tmp=LTrim(tmp);tmp=RTrim(tmp);
while(tmp.IndexOf(" ")>0)
tmp=tmp.Remove(tmp.IndexOf(" "),1);
return tmp;
}
static string Xoa(string con, string s)
{
string tmp=string.Copy(s);
while(tmp.IndexOf(con)>0)
tmp=tmp.Remove(tmp.IndexOf(con),con.Length);
return tmp;
}
static void catTu(string s,ref string []tu)
{
int d=0;
foreach(string con in s.Split(' '))
if(con.Length>0)
{
Array.Resize(ref tu,++d);
tu[d-1]= con;
}
}
static string sapXep(string []tu)
{
Array.Sort(tu);
string tmp="";
for(int i=0;i tmp=tmp+" "+tu[i];
return tmp;
}
static void tanXuat(string []tu)
{
string []tmp=null;
int i,j,d=0;
bool ok;
for(i=0;i {
ok=true;
for(j=0;j if(string.Compare(tmp[j],tu[i])==0) ok=false;
if(ok)
{
Array.Resize(ref tmp,++d);
tmp[d-1]=string.Copy(tu[i]);
}
}
Console.WriteLine("Tan xuat xuat hien cua cac tu la:");
for(j=0;j {
d=0;
for(i=0;i if(string.Compare(tu[i],tmp[j])==0) d++;
if(d>0) Console.WriteLine("Tu {0} xuat hien {1} lan",tmp[j],d);
}
}
static void Main()
{
string s,tmp;
Console.Write("Nhap xau:");s=Console.ReadLine();
//Cau a
tmp=Trim(s);
Console.WriteLine("Xau sau khi xoa:{0}",tmp);
//Cau b
tmp=Xoa("mua xuan",s);
Console.WriteLine("Xau sau khi xoa:{0}",tmp);
//Cau c
string []tu=null;
catTu(s,ref tu);
Console.WriteLine("Xau sau khi sap xep:"+sapXep(tu));
//Cau d
tanXuat(tu);
Console.ReadKey();
}
}