从键盘输入一个字符串,删除该字符串的所有首部和尾部数字字符,输出修改后的字符串以及该字符串的长度(限定在一个数组内进行)
#include <iostream>
using namespace std;
int main()
{
char s[1000];
gets(s);
int flag = 0,start = -1;
int i=0,j=0,k;
while(s[i] != '\0')
{
if(s[i] >='0' && s[i] <='9')
{
if(flag == 1 && start == -1)
{
start = i;
}
i++;
continue;
}
else
{
flag = 1;
if(start >= 0)
{
for(k=start;k<i;k++)
s[j++] = s[k];
start = -1;
}
s[j++] = s[i++];
}
}
s[j] = '\0';
cout<<s<<endl;
cout<<strlen(s)<<endl;
}
参考一下这个
#include <iostream>
#include <string>
#include <cctype>
using namespace std;
int main() {
string str;
getline(cin, str);
int i = 0;
while (isdigit(str[i])) {
i++;
}
str = str.substr(i);
int j = str.length() - 1;
while (j >= 0 && isdigit(str[j])) {
j--;
}
str = str.substr(0, j + 1);
cout << "修改后的字符串: " << str << endl;
cout << "长度: " << str.length() << endl;
return 0;
}
#include<iostream>
using namespace std;
class Date
{
public:
//构造函数
Date(int year = 2020, int month = 5, int day = 5)
{
_year = year;
_month = month;
_day = day;
cout << "Date(int,int ,int):" << this << endl;
//看构造的是哪一个对象
}
//析构函数
~Date()
{
//对于日期类来说,这里面没有什么资源是需要去释放的
//所以对于日期类来说,给不给析构函数其实都没有什么影响
cout << "~Date():" << this << endl;
//看析构的是哪一个对象
}
private:
int _year;
int _month;
int _day;
};
void TestDate()
{
Date d1(2020, 5, 5);
Date d2(d1);
}
int main()
{
TestDate();
return 0;
}