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


#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;
        }
    }
    //結束復制之后要 最后加空字符
        *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) { //計算的有效長度(不加空字符)
        ++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;
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM