String对象具有类似数组的行为
String对象具有类似数组的行为
可以像处理数组那样来处理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;
- }
评论暂时关闭