Đề bài: Nhập vào xâu s, có đội dài không quá 100 kí tự
1. Hãy chuẩn hoá xâu S (xoá các kí tự trắng thừa giữa các từ trong xâu, chỉ giữ lại 1)
2. Đếm xem xâu S có bao nhiu từ
3. Đổi tất cả kí tự "a" trong xâu S thành "A"
Hãy nhập câu hỏi của bạn vào đây, nếu là tài khoản VIP, bạn sẽ được ưu tiên trả lời.
Program HOC24;
var S: string;
d,i: byte;
begin
write('S= '); readln(s);
For i:= length(s) downto 1 do write(s[i]);
writeln;
while s[length(s)]=#32 do delete(s,length(s),1);
while pos(#32#32,s)<>0 do delete(s,pos(#32#32,s),1);
while s[1]=#32 do delete(s,1,1);
d:=0;
for i:=1 to length(s) do d:=d+1;
write('So tu :',d);
readln
end.
uses crt;
var s:string[200];
i,d,dem:integer;
begin
clrscr;
write('Nhap xau:'); readln(s);
d:=length(s);
write('Xau viet theo chieu nguoc la: ');
for i:=d downto 1 do
write(st[i]);
writeln;
dem:=0;
for i:=1 to d do
if st[i]=#32 then inc(dem);
writeln('So tu la: ',dem);
readln;
end.
program bai1;
uses crt;
var i:integer;
s,s1:string;
begin
clrscr;
write('nhap S:');readln(s);
while pos('C',s)<>0 do
begin
insert('LOP11A',s,pos('C',s));
delete(s,pos('C',s),1);
end;
writeln('xau sau khi bien doi la: ',s);
writeln('do dai cua xau tren la: ',length(s));
write('nhap s1:');readln(s1);
if s1[1]=s[1] then writeln('ki tu dau cua hai xau trung nhau')
else writeln('ki tu dau cua hai xau khong trung nhau');
readln;
end.
uses crt;
var s:string;
i,d,dem,dem1,kt:integer;
begin
clrscr;
write('Nhap xau S:'); readln(s);
d:=length(s);
dem:=0;
for i:=1 to d do
if st[i] in ['0'..'9'] then inc(dem);
writeln('So ki tu la chu so la: ',dem);
dem1:=0;
for i:=1 to d do
if (st[i] in ['A'..'Z']) or (st[i] in ['a'..'z']) then inc(dem1);
writeln('So ki tu la chu cai la: ',dem1);
write('Xau sau khi xoa ki tu trang la: ');
for i:=1 to d do
if st[i]<>#32 then write(st[i]);
writeln;
kt:=0;
for i:=1 to d do
if st[i]<>st[d-i+1] then kt:=1;
if kt=0 then writeln('Xau doi xung')
else writeln('Xau khong doi xung');
readln;
end.
uses crt;
var s:string;
i,tong,x,code:integer;
f,g:text;
k:boolean;
const fi='XAU.INP';
fo='XAU.OUT';
begin
k:=false;
assign(f,fi); reset(f);
assign(g,fo); rewrite(g);
readln(f,s);
tong:=0;
for i:=1 to length(s) do
begin
if s[i] in ['0'..'9'] then
begin
k:=true;
val(s[i],x,code);
tong:=tong+x;
x:=0;
cod:=0;
end;
end;
if k=false then writeln(g,'Sai yeu cau')
else
begin
writeln(g,s);
writeln(g,tong);
end;
close(f);
close(g);
end.
Để chuẩn hóa xâu kí tự, ta sẽ loại bỏ các ký tự trống ở đầu và cuối xâu, sau đó loại bỏ các ký tự trống kề liền.
Để tìm số lượng kí tự chữ số trong xâu, ta sẽ duyệt qua từng ký tự của xâu và kiểm tra xem ký tự đó có phải là chữ số không.
Code Python để thực hiện yêu cầu đề bài như sau:
pythondef chuan_hoa_xau(s): # Xóa khoảng trắng ở đầu và cuối xâu s = s.strip() # Loại bỏ khoảng trắng kề nhau i = 0 while i < len(s) - 1: if s[i] == ' ' and s[i+1] == ' ': s = s[:i] + s[i+1:] else: i += 1 return s def dem_chu_so(s): count = 0 for c in s: if c.isdigit(): count += 1 return count # Đọc xâu kí tự từ input s = input() # Chuẩn hóa xâu s_chuan = chuan_hoa_xau(s) # Tìm số lượng kí tự chữ số so_luong_chu_so = dem_chu_so(s_chuan) # In ra kết quả print(s_chuan) print(so_luong_chu_so)Ví dụ:
Input:
csharpThis is an example 1234 string .Output:
csharpThis is an example 1234 string. 4