設計一個字串類,並將字串處理函數的內容進行封裝

來源:互聯網
上載者:User

標籤:大小   end   system   設計   stream   參數   成員   cout   地方   

#include<iostream>using namespace std;class String {public:    //定義類的成員變數    char s[10] = "abcd";    char *ps = s;    const char t[10] = "dcba";    const char *pt = t;    char S[10] = "ABCDEF";        //建構函式部分    String();    String(const String & c);//字串處理函數    //串連    void strcat();    //複製    void strcpy();    //複製n個    void strcpy(int n);    //比較    int strcmp();    //取長度    int strlen( char *s);    //轉大小寫    void strwr( char *s);    void strup( char *s);        ~String();};String::~String(){}String::String(){}//拷貝函數String::String(const String & c) {  //由於是拷貝,所以把參數用const限定    const char* s = c.s;    cout << "1";};void String::strcat(){    while (*ps) {  //把指標移動到最後        ++ps;    }    while (*pt) {  //當*pt有值  就進入        *ps = *pt;  //複製        ++pt;  //指標後移        ++ps;    }    cout << s << endl;  //輸出下串連複製是否正確    return;}void String::strcpy(){    while ((*ps = *pt) != ‘\0‘) { //一直到*pt指向的內容為\0就跳出        ps++;  //往後移動指標        pt++;    }    *ps = *pt;  //把\0也給*ps    cout << s << endl;    return;}void String::strcpy(int n) //複製前n個{    int i = 0;    while (*pt) {  //首先也要判斷pt有值        if (i<n) {            *ps = *pt;            ++i;               ps++;            pt++;        }        else {   //如果已經複製了n個 那就結束            break;        }    }    //結束複製之後要 最後加Null 字元        *ps = ‘\0‘;        cout << s << endl;    return;}int String::strcmp(){    //用for迴圈  找到不相等的地方    for (; *ps == *pt; ++ps, ++pt) {        if (*ps == ‘\0‘) { //如果兩個完全相等 會進去            return 0;        }    }    return (*ps - *pt);    //返回當前指的位置 相減 就是字元對應的數值相減}int String::strlen( char * s){    int i = 0;    while (*s) { //計算的有效長度(不加Null 字元)        ++s;        ++i;    }    return i;}void String::strwr( char * s){     char*m = s;    while (*s)    {   //要判斷是否為大寫字母        if (*s >= ‘A‘&&*s <= ‘Z‘) {            *s += 32;        }        ++s;    }    //由於指標已經把 類裡面的成員S  變為小寫    //故直接輸出就可以驗證    cout << S << endl;    return;}void String::strup( char * s){     char*m = s;    while (*s)    {        if (*s >= ‘a‘&&*s <= ‘z‘) {            *s -= 32;        }        ++s;    }    cout << S << endl;    return;}void main(){    //調用函數    cout << "strcat:" << endl;    String e1;    e1.strcat();    cout << endl;    cout << "strcpy:" << endl;    String e2;    e2.strcpy();    cout << endl;    cout << "strncpy:" << endl;    String e3;    e3.strcpy(3);    cout << endl;    String e4;    cout << "strcmp:" << endl << e4.strcmp() << endl;    cout << endl;    String e5;    cout << "strlen:" << endl << e5.strlen(e5.s) << endl;    cout << endl;    cout << "strwr:" << endl;    String e6;    e6.strwr(e6.S);    cout << endl;    cout << "strup:" << endl;    String e7;    e7.strup(e6.S);    cout << endl;    system("pause");    return;}

 

設計一個字串類,並將字串處理函數的內容進行封裝

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.