串的基本操作
前記:這一章課件裡主要講了串的屬性和一些常用的操作。課件裡面是通過偽**的方式來進行描述,這樣有利於同學們的理解,以及能夠適用於各種程式語言。下面就針對c++語言對這些基本操作做乙個具體的實現,大家在運用中可以參考。
mystring.h:
#ifndef mystring_h
#define mystring_h
#include #include #include #define maxsize 1000
#define sizeincrease 10
#define true 1
#define false 0
#define ok 1
#define error 0
#define infeasible -1
#define overflow -2
#define format "%3c"
#define turnline printf("\n\n")
typedef long status;
typedef char elemtype;
typedef struct
string;
void test();
void strprint(string *); //列印字串
status strassign(string *,char *); //複製字串
status strcopy(string *sourcestring,string *destinationstring); //複製字串
int strcompare(string *s1,string *s2); //比較兩個字串
status strclear(string *s); //清理字串
status strcontact(string *s3,string *s1,string *s2); //連線兩個字串
status substring(string *sub,string *s,int pos,int len); //返回某個字串在第i個位置的子串
void test()
void strprint(string *s)
status strassign(string *s,char *chars)
else
s->ch[i+1]='\0';
s->length=i;}
return ok;
}status strcopy(string *sourcestring,string *destinationstring)
destinationstring->ch=(elemtype *)malloc((sourcestring->length+1)*sizeof(elemtype));
if(!destinationstring) exit(overflow);
destinationstring->length =sourcestring->length ;
for(;index<=sourcestring->length ;++index)
destinationstring->ch[index]=sourcestring->ch[index];
destinationstring->ch[sourcestring->length +1]='\0';
return ok;
}int strcompare(string *s1,string *s2)
return s1->length -s2->length ;
}status strclear(string *s)
status strcontact(string *s3,string *s1,string *s2)
for(index=0;s2->ch[index] ;++index,++j)
s3->ch[s1->length+s2->length+1]='\0';
s3->length =s1->length+s2->length;
return ok;
}status substring(string *sub,string *s,int pos,int len)
#endif
呼叫的main函式:
#include "mystring.h"
#include "fun.h"
int main()
'); (window.slotbydup = window.slotbydup || ).push(); })();
'); (window.slotbydup = window.slotbydup || ).push(); })();
串的基本操作
include include include include define maxn 50 define ok 1 define error 0 typedef struct strnode snode void creat snode char 建立串 int getsubstr snode i...
串的基本操作
假設有串t s iphone 11 pro max?w pro strassign t,chars 賦值操作。把串t賦值為chars。strcopy t,s 複製操作。由串s複製得到串t。strempty s 判空操作。若s為空串,則返回true,否則返回false。strlength s 求串長。...
串的基本操作
include define maxstrlen 255 define true 1 define false 0 define ok 1typedef intstatus typedef unsigned char sstring maxstrlen 1 status strassign sstr...