歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux編程 >> Linux編程

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. { 
  10.     using namespace std; 
  11.     string first_name , last_name; 
  12.     cout<<"Enter your first and last name:"; 
  13.     cin>>first_name 
  14.         >>last_name; 
  15.     cout<<endl<<endl; 
  16.     cout<<"your last name is spelled:\n"; 
  17.     int i ; 
  18.     for(i = 0; i < last_name.length() ; i++) //用for循環操縱數組  
  19.     { 
  20.         cout<<last_name[i]<<" "; 
  21.         last_name[i] = '-'; 
  22.     } 
  23.     cout<<endl; 
  24.     for(i = 0; i < last_name.length() ; i++) 
  25.     { 
  26.         cout<<last_name[i]<<" ";//在每個字母下面顯示一個"-"  
  27.     } 
  28.     cout<<endl; 
  29.  
  30.     cout<<"Good day "<<first_name<<endl; 
  31.     return 0; 
  32. } 

Copyright © Linux教程網 All Rights Reserved