您的位置:首页 > 其它

string对象具有类似数组的行为

2012-10-25 10:49 162 查看
可以像处理数组那样来处理String 对象!

// String.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include<iostream>
#include<string>

int main(int argc, char* argv[])
{
	using namespace std;
	string first_name , last_name;
	cout<<"Enter your first and last name:";
	cin>>first_name
		>>last_name;
	cout<<endl<<endl;
	cout<<"your last name is spelled:\n";
	int i ;
	for(i = 0; i < last_name.length() ; i++) //用for循环操纵数组
	{
		cout<<last_name[i]<<" ";
		last_name[i] = '-';
	}
	cout<<endl;
	for(i = 0; i < last_name.length() ; i++)
	{
		cout<<last_name[i]<<" ";//在每个字母下面显示一个"-"
	}
	cout<<endl;

	cout<<"Good day "<<first_name<<endl;
	return 0;
}


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐