📅  最后修改于: 2020-09-25 06:32:51             🧑  作者: Mango
该程序接收用户输入的字符串 (对象),并删除除字母之外的所有字符 。
#include
using namespace std;
int main() {
string line;
string temp = "";
cout << "Enter a string: ";
getline(cin, line);
for (int i = 0; i < line.size(); ++i) {
if ((line[i] >= 'a' && line[i] <= 'z') || (line[i] >= 'A' && line[i] <= 'Z')) {
temp = temp + line[i];
}
}
line = temp;
cout << "Output String: " << line;
return 0;
}
输出
Enter a string: p2'r"o@gram84iz./
Output String: programiz
下面的程序从用户输入一个字符串 (C样式字符串),并删除除字母之外的所有字符 。
#include
using namespace std;
int main() {
char line[100], alphabetString[100];
int j = 0;
cout << "Enter a string: ";
cin.getline(line, 100);
for(int i = 0; line[i] != '\0'; ++i)
{
if ((line[i] >= 'a' && line[i]<='z') || (line[i] >= 'A' && line[i]<='Z'))
{
alphabetString[j++] = line[i];
}
}
alphabetString[j] = '\0';
cout << "Output String: " << alphabetString;
return 0;
}
输出
Enter a string: P2'r"o@gram84iz./
Output String: Programiz