C++ Tutorial/Data Types/string create

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

copies one string to another with pointers

<source lang="cpp">#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";                
 }</source>

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

<source lang="cpp">#include <string>

  1. include <iostream>
  2. 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;

}</source>

Hello, how are you?

displays a string with pointer notation

<source lang="cpp">#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;  
 }</source>