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


可以像处理数组那样来处理String 对象!

  1. // String.cpp : Defines the entry point for the console application.  
  2. //  
  3.  
  4. #include "stdafx.h"  
  5. #include<iostream>  
  6. #include<string>  
  7.  
  8. int main(int argc, char* argv[]) 
  9.     using namespace std; 
  10.     string first_name , last_name; 
  11.     cout<<"Enter your first and last name:"
  12.     cin>>first_name 
  13.         >>last_name; 
  14.     cout<<endl<<endl; 
  15.     cout<<"your last name is spelled:\n"
  16.     int i ; 
  17.     for(i = 0; i < last_name.length() ; i++) //用for循环操纵数组  
  18.     { 
  19.         cout<<last_name[i]<<" "
  20.         last_name[i] = '-'
  21.     } 
  22.     cout<<endl; 
  23.     for(i = 0; i < last_name.length() ; i++) 
  24.     { 
  25.         cout<<last_name[i]<<" ";//在每个字母下面显示一个"-"  
  26.     } 
  27.     cout<<endl; 
  28.  
  29.     cout<<"Good day "<<first_name<<endl; 
  30.     return 0; 

相关内容