练笔-字符串,向量和数组5

1 将输入的字符串所有字转换为X

for实现

#include <iostream>
#include <string>
using namespace std;
int main()
{
	string line;
	getline(cin,line);
	int i=0;
	for (i=0;i<line.size();i++)
	{
		line[i]='X';
	}
	cout<<line<<endl;
}

while实现

#include <iostream>
#include <string>
using namespace std;
int main()
{
    string line;
    getline(cin,line);
    int i=0;
    while(line[i]!='\0')
    {
        line[i]='X';
        i++;
    }
    cout<<line<<endl;
}

有关C++11中涉及到的

for(auto c : line)这样的,很多都不支持,但是可以了解一下。auto会自动判定C的类型,对于上面的字符串,C直接自动判定为char型。

 

2 编写一段程序,读入一个包含标点符号的字符串,将标点符号除去后读入剩余的部分。

首先要知道函数ispunct(C);用来判定和字符c是不是标点符号,如果是返回true,否则返回false;

#include <iostream>
#include <string>
#include <cctype>
using namespace std;
int main()
{
    string line;
    getline(cin,line);
    int i=0;
    for (i=0;i<line.size();i++)
    {
        if (!ispunct(line[i]))
        {
            cout<<line[i];
        }
    }
}

3 用cin读入一组整数,并且把他们存入一个vector对象

#include <iostream>
#include <vector>
using namespace std;
int main()
{
    vector<int> vint;
    int i;
    char flag='y';
    while(cin>>i)
    {
        vint.push_back(i);
        cout<<"continue?"<<endl;
        cin>>flag;
        if (flag!='y'&&flag!='Y')
        {
            break;
        }
    }
    for (int j = 0; j < vint.size(); j++)
    {
        cout<<vint[j]<<endl;//在利用下标运算符访问vector元素的问题,要注意
    }
    return 0;
}

当vector为字符串的时候,修改上述代码

#include <iostream>
#include <vector>
#include <string>
using namespace std;
int main()
{
    vector<string> vstring;
    string s;
    char flag='y';
    while(cin>>s)
    {
        vstring.push_back(s);
        cout<<"continue?"<<endl;
        cin>>flag;
        if (flag!='y'&&flag!='Y')
        {
            break;
        }
    }
    for (int j = 0; j < vstring.size(); j++)
    {
        cout<<vstring[j]<<endl;
    }
    return 0;
}

 

4 从cin输入的所有词存入vector对象,并且对输入的所有字符串改为大写。

将字符改为大写使用toupper()函数,同样将大写改小写使用tolower()函数;

具体这两个函数的实现在库函数中本来就有,不过可以自己动手写一下。

// Note:Your choice is C++ IDE
#include <iostream>
using namespace std;
char toupper(char c);
char tolower(char c);
int main()
{
    char c;
    cout<<"输入小写字符"<<endl;
    cin>>c;
    c=toupper(c);
    cout<<c<<endl;
    char d;
    cout<<"输入大写字符"<<endl;
    cin>>d;
    d=tolower(d);
    cout<<d<<endl;
    return 0;
}
char toupper(char c)
{
    if ((c>'a')&&(c<'z'))
    {
        c=c-('a'-'A');
    }
    return c;
}
char tolower(char c)
{
    if ((c>'A')&&(c<'Z'))
    {
        c=c+('a'-'A');
    }
    return c;
}

那么现在我们来解决这个问题

#include <iostream>
#include <vector>
#include <string>
using namespace std;
int main()
{
    vector<string> vstring;
    string i;
    char flag='y';
    while(cin>>i)
    {
        vstring.push_back(i);
        cout<<"continue?"<<endl;
        cin>>flag;
        if (flag!='y'&&flag!='Y')
        {
            break;
        }
    }
    for (int j = 0; j < vstring.size(); j++)
    {
        string c=vstring[j];
        for(int m=0;m<c.size();m++)
        {
            c[m]=toupper(c[m]);//注意需要赋值改变C的值,否则只是在第二个for循环内改变局部变量,除了循环就不变们不能达到大小写字母变换的效果。
        }
        cout<<c<<endl;
    }
    return 0;
}

 5  读入一组整数,存入vector,(1)相邻的相加;(2)首尾相加;

(1)注意for循环中的边界;

#include <iostream>
#include <vector> 
using namespace std;
int main()
{
    vector<int> v;
    int p;
    while (cin>>p)
    {
        if (p==000)
        {
            break;
        }
        else
            v.push_back(p);
    }
    if (v.size()==0)
    {
        cout<<"the vector is empty"<<endl;
    }
    for (int i = 0; i < v.size()-1; i=i+2)
    {
        cout<<v[i]+v[i+1]<<endl;
    }
    if (v.size()%2!=0)
    {
        cout<<v[v.size()-1]<<endl;
    }
    return 0;
}

(2)和上面一样,要单独讨论个数为奇数的时候的情况。

 1 #include <iostream>
 2 #include <vector> 
 3 using namespace std;
 4 int main()
 5 {
 6     vector<int> v;
 7     int p;
 8     while (cin>>p)
 9     {
10         if (p==000)
11         {
12             break;
13         }
14         else
15             v.push_back(p);
16     }
17     if (v.size()==0)
18     {
19         cout<<"the vector is empty"<<endl;
20     }
21     for (int i = 0; i < v.size()/2; i=i+1)
22     {
23         cout<<v[i]+v[v.size()-1-i]<<endl;
24     }
25     if (v.size()%2!=0)
26     {
27         cout<<v[v.size()/2]<<endl;
28     }
29     return 0;
30 }

 

转载于:https://www.cnblogs.com/tao-alex/p/5542060.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值