歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> String對象具有類似數組的行為

String對象具有類似數組的行為

日期:2017/3/1 10:08:10   编辑:Linux編程

可以像處理數組那樣來處理String 對象!

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

Copyright © Linux教程網 All Rights Reserved