Nhập vào từ bàn phím một xâu. Hãy chuẩn hóa xâu vừa nhập theo quy tắc:
- Xóa các dấu cách ở đầu xâu và cuối xâu (nếu có).
- Nếu giữa xâu có 2 hay nhiều dấu cách thì xóa bỏ và chỉ để lại một dấu cách.
In ra xâu đã chuẩn hóa.
anh em giúp tôi với :(
Viết chương trình nhập vào từ bàn phím một xâu có thể chưa chuẩn về dấu cách, hãy loại bỏ các dấu cách thừa có trong xâu đó. Ví dụ : ' Hoc tap suot doi ''Hoc tap suot doi'
Cách làm:
+ Xóa dấu cách thừa ở đầu và cuối xâu: Kiểm tra cứ khi nào ký tự đầu tiên hoặc kí tự cuối của xâu S: là dấu cách thì xóa bỏ.
+Xóa dấu cách thừa ở giữa các từ: Cứ khi nào s[i] và s[i+1] đều là dấu cách thì xóa đi 1 dấu cách.
#include <bits/stdc++.h>
using namespace std;
int d,i,d1;
string st;
int main()
{
getline(cin,st);
d=st.length();
while (st[0]==32)
{
st.erase(0,1);
}
while (st[d-1]==32)
{
st.erase(d-1,1);
}
d1=st.length();
for (i=0; i<d1; i++)
if ((st[i]==32) && st[i+1]==32)
{
st.erase(i,1);
i--;
}
cout<<st;
return 0;
}
viết chương trình nhập 1 xâu từ bàn phím. a) Hãy xóa tất cả các cách ký tự ở đầu xâu b) Hãy xóa tất cả các cách ký tự ở đầu xâu c) Hãy xóa tất cả các cách ký tự ở giữa các từ (giữa các từ chỉ có 1 dấu cách ) d) Đếm các chữ cái có trong xâu e) Đếm các chữ số ở xâu chuỗi. Trong màn hình kết quả
#include <bits/stdc++.h>
using namespace std;
int d,i,d1;
string st;
int main()
{
getline(cin,st);
d=st.length();
while (st[0]==32)
{
st.erase(0,1);
}
while (st[d-1]==32)
{
st.erase(d-1,1);
}
d1=st.length();
for (i=0; i<d1; i++)
if ((st[i]==32) && st[i+1]==32)
{
st.erase(i,1);
i--;
}
cout<<st;
return 0;
}
Viết chương trình nhập vào một xâu S có độ dài không quá 125 ký tự từ bàn phím. Xuất xâu đã nhập. Thực hiện xóa hết các dấu cách có trong xâu S. Xuất xâu S đã xóa các dấu cách ra màn hình
uses crt;
var st:string;
i,d:integer;
begin
clrscr;
write('Nhap xau:'); readln(st);
d:=length(st);
writeln('Xau vua nhap la: ',st);
for i:=1 to d do
if st[i]=#32 then delete(st,i,1);
writeln('Xau moi la: ',st);
readln;
end.
Viết chương trình nhập vào xâu ký tự S ,độ dài không quá 100 ký tự . Hãy chuẩn hóa xâu
-Xâu chuẩn là xâu không có dấu cách ở cuối câu , giữa 2 từ trong xâu chỉ có 1 dấu cách .
Uses crt;
Var st:string;
I,d:integer;
Begin
Clrscr;
Write('nhap xau:'); readln(st);
D:=length(st);
While st[d]=#32 do
Begin
Delete(st,d,1);
D:=length(st);
End;
For i:=1 to d do
If (st[i]=#32) and (st[i+1]=#32) then delete(st,i,1);
Writeln(st);
Readln;
End.
Viết chương trình nhập vào xâu ký tự S , độ dài không quá 100 ký tự . Hãy chuẩn hóa xâu
- Xâu chuẩn là xâu không có dấu cách ở đầu câu , giữa 2 từ trong xâu chỉ có 1 dấu cách .
Uses crt;
Var st:string;
I,d:integer;
Begin
Clrscr;
Write('nhap xau:'); readln(st);
D:=length(st);
While st[d]=#32 do
Begin
Delete(st,d,1);
D:=length(st);
End;
For i:=1 to d do
If (st[i]=#32) and (st[i+1]=#32) then delete(st,i,1);
Writeln(st);
Readln;
End.
Nhập vào 1 xâu kí tự S từ bàn phím, rồi in ra xâu kí tự đó sau khi đã xoá hết các dấu cách thừa ở đầu, cuối xâu và giữa các từ trong xâu chỉ để lại 1 dấu cách BẰNG PYTHON Mọi người giúp em với ạ 😢😢😢
1 Viết chương trình nhập mạng một chiều n tính và in ra màn hình tổng số lẻ 2 viết chương trình nhập vào một xâu từ văn phím viết chương trình chuẩn hóa xâu bằng cách xóa các ký tự trống ở đâu xâu và cuối xâu 3 Viết các thao tác và lần tương ứng cần thực hiện để đọc một giá trị từ tệp ‘ DL.TXT ‘ Ghi giá trị vừa đọc vào tệp ‘KQ.TXT’ Giúp mình với ạ
1:
uses crt;
var a:array[1..100]of integer;
i,n,t:integer;
begin
clrscr;
write('Nhap n='); readln(n);
for i:=1 to n do
begin
write('A[',i,']='); readln(a[i]);
end;
t:=0;
for i:=1 to n do
if a[i] mod 2<>0 then t:=t+a[i];
writeln(t);
readln;
end.
3:
const fi='dl.txt'
fo='kq.txt'
var f1,f2:text;
a:integer;
begin
assign(f1,fi); reset(f1);
assign(f2,fo); rewrite(f2);
readln(f1,a);
writeln(f2,a);
close(f1);
close(f2);
end.
2:
uses crt;
var st:string;
d:integer;
begin
clrscr;
write('Nhap xau:'); readln(st);
d:=length(st);
while st[1]=#32 do
begin
delete(st,1,1);
d:=length(st);
end;
while st[d]=#32 do
begin
delete(st,d,1);
d:=length(st);
end;
writeln(st);
readln;
end.
Viết chương trình nhập vào một xâu hãy xóa đi các dấu cách ở đầu sâu và cuối sâu nếu có rồi đưa kết quả ra màn hình
Cho xâu kí tự S được lấy từ tập 'a'..'z','A'..'Z' và dấu cách. Từ trong xâu là dãy gồm tất cả các ký tự chữ cái liên tục. Xâu S được gọi là chuẩn hóa nếu thỏa mãn các điều kiện:
-Giữa các từ chỉ có 1 dấu cách.
-Đầu xâu và cuối xâu không có dấu cách.
-Ký tự đầu là chữ in hoa, các ký tự còn lại in thường.
Yêu cầu: Chuẩn hóa xâu S