C++: implement string manually

Implement the basic functions of string

1. Use MyString s1("asdf") to create a string object.
2. Use MyString s2('c',5) to create "ccccc"
3. Use MyString s3(100) to create a string "100" "
4. Get the length of the string.
5. Output the string
6. Find substrings and characters.

Main.cpp

#include"MyString.h"
#include<iostream>
#include<random>
#include<string>
#include<vector>
using namespace std;
int main(){
    
    
   MyString mystr("abcdefghijklmn");
    mystr.toString(); 
    MyString mystr2("10"); 
    mystr2.toString();  
    const char x = 'a';
    MyString mystr3(x,5);
    mystr3.toString();
    //vector<int> res =  mystr.findOne('f');
     vector<int> res = mystr.findAll("jk1");
    for (int i = 0;i<res.size();i++)
    {
    
    
        cout<<res[i]<<endl;
    }
    cout<<""<<endl;  
    MyString mystr2(100); 
    mystr2.toString();
    
    return 0;
}

MyString.h

#include<iostream>
#include<vector>
using namespace::std;
class MyString
{
    
    
private:
    const char *str;
    int length = 0;
public:
    MyString(const char pstr[]);
    MyString(const char x,int length);
    MyString(int x);
    ~MyString();
    void toString();
    int my_strlen(const char *str);
    int getLength();
    vector<int> findOne( char x);
    vector<int> findAll(const char x[]);
};

MyString.cpp

#include<iostream>
#include"MyString.h"
#include<random>
#include<string>
#include"vector"
#include<stdio.h>
#include<stdlib.h>
using namespace std;
MyString::MyString(const char pstr[]){
    
    
   while(pstr[length]!='\0'){
    
    
      length++;
   }
   char *help= new char[length];
   for (int i = 0; i < length; i++){
    
    
      help[i] = pstr[i];
   }
   str = help;
}

MyString::MyString(int x){
    
    
   vector<char> re;
   while(x>9){
    
    
      int res = x%10;
      char ress = res+'0';
      re.push_back(ress);
      x = x/10;
   }
    re.push_back(x+'0');
   length = re.size();
   cout<<length<<endl;
   char *w = new char[length];
   int index = length-1;
   int second_index = 0;
   while(index>=0){
    
    
      w[second_index] = re[index];
      index--;
      second_index++;
   }
   str = w;
}

MyString::MyString(const char x,int length){
    
    
   char *help= new char[length];
   for (int i = 0; i < length; i++){
    
    
      help[i] = x;
   }
   str = help;
   this->length = length;
} 

MyString::~MyString(){
    
    

}

void MyString::toString(){
    
    
   cout<<""<<endl;
   for (int i = 0; i < length; i++){
    
    
      cout<< *(str+i);
   }
   cout<<""<<endl;
}

int MyString::getLength(){
    
    
   return length;
}

vector<int> MyString::findOne( char x){
    
    
   vector<int> result;
   for (int i = 0; i < length; i++){
    
    
      if(*(str+i) == x){
    
    
         result.push_back(i);
      }
   }
   cout<<""<<endl;
   return result;
}

vector<int> MyString::findAll(const char x[]){
    
    
   vector<int> res;
   int len = 0;
   while(x[len]!='\0'){
    
    
      len++;
   }
   int end = length - len;
   for (int i = 0; i <= end; i++){
    
    
      bool flags = true;
      int j = i;
      int index = 0;
      while(index<len&&j<i+len){
    
    
         if(str[j]!= x[index]){
    
    
            flags = false;
         }
         index++;
         j++;
      }
      if(flags){
    
    
         res.push_back(i);
      }
   }
   if(res.empty()){
    
    
      cout<<"未找到该字符串"<<endl;
   }
   return res;
}

Compile script in Ubuntu environment

#!/bin/bash
g++ -c Main.cpp
g++ -c MyString.cpp
g++ Main.o MyString.o -o result
./result
rm Main.o MyString.o result

Put the script and three files in the same directory,
./script name, after executing ./result, you can see the running result.

Guess you like

Origin blog.csdn.net/qq_45789385/article/details/112233560