#include "stdafx.h" #include #include #include #include using namespace std; #define NULL 0 #define OK 1 typedef int Status; typedef struct { char *ch; int length; }HString; Status StrAssign(HString &T,char *chars) { //生成一个其值等于串常量chars的串T int i; char *c; if(T.ch) free(T.ch); //释放T的原有空间 for(i=0,c=chars;*c;++i,++c); //求chars的长度i if(!i) { T.ch=NULL; T.length=0; } else { if(!(T.ch=(char*)malloc(i* sizeof(char)))) exit(OVERFLOW); T.ch[i]=chars[i]; T.length=i; } return OK; } int StrLength(HString S) { //返回串S的长度 return S.length; } Status ClearString(HString &S) { //将S清为空串 if(S.ch) { free(S.ch); S.ch=NULL; } S.length=0; return OK; } void InputStr(char &chars) { cout<<"please input the string:"<>chars; } int main() { HString S; char *chars=""; InputStr(*chars); StrAssign(S,chars); StrLength(S); ClearString(S); system("pause"); return OK; }
把代码格式化一下,点击插入代码段发布。
啊我直接复制粘贴的 没想到那么乱 只好麻烦大家复制粘贴在vc里了
#include "stdafx.h" #include #include #include #include using namespace std; #define NULL 0 #define OK 1 typedef int Status; typedef struct { char *ch; int length; }HString; Status StrAssign(HString &T,char *chars) { //生成一个其值等于串常量chars的串T int i; char *c; if(T.ch) free(T.ch); //释放T的原有空间 for(i=0,c=chars;*c;++i,++c); //求chars的长度i if(!i) { T.ch=NULL; T.length=0; } else { if(!(T.ch=(char*)malloc(i* sizeof(char)))) exit(OVERFLOW); T.ch[i]=chars[i]; T.length=i; } return OK; } int StrLength(HString S) { //返回串S的长度 cout<>chars; } int main() { HString S; char *chars=""; InputStr(*chars); StrAssign(S,chars); StrLength(S); ClearString(S); system("pause"); return OK; }