C++ Tutorial/Data Types/string create

Материал из C\C++ эксперт
Перейти к: навигация, поиск

copies one string to another with pointers

#include <iostream>  
  using namespace std;  
    
  int main(){  
     void copystr(char*, const char*);  
     char* str1 = "this is a test";  
     char str2[80];               
    
     copystr(str2, str1);         
     cout << str2 << endl;        
     return 0;  
  }  
  void copystr(char* dest, const char* src){  
     while( *src )                
        *dest++ = *src++;         
     *dest = "\0";                
  }

Create a string base on the start and end of another string

#include <string>
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
    const string hello("Hello, how are you?");
    string s(hello.begin(),hello.end());
    // iterate through all of the characters
    string::iterator pos;
    for (pos = s.begin(); pos != s.end(); ++pos) {
        cout << *pos;
    }
    cout << endl;
}
Hello, how are you?

displays a string with pointer notation

#include <iostream>  
  using namespace std;  
    
  int main(){  
     void dispstr(char*);
     char str[] = "Idle people have the least leisure.";  
    
     dispstr(str);      
     return 0;  
  }  
  void dispstr(char* ps)  
  {  
     while( *ps )       
        cout << *ps++;  
     cout << endl;  
  }