您的位置:首页 > 其它

2013=字符串

2013-11-04 21:15 357 查看


#include <stdio.h>
#include <stdlib.h>

#define ERROR -1
#define OK 1

typedef struct
{
char *ch;
int length;
} heapstring;

typedef struct
{
char item[100][80];
int last;
}wordlisttype;

typedef int status;

status  init(heapstring &t)
{
t.ch=(char *)malloc(sizeof(char));
return 1;
}

status strassign(heapstring &t,char *chars)
{
int i,j;
char *c;
init(t);
if (t.ch)	free(t.ch);
for (i=0,c=chars; *c; ++i,++c)
if (!i)
{
t.ch=NULL;
t.length =0;
}
else
if (!(t.ch=(char *)malloc(i*sizeof(char))))
return ERROR;
else
{
for (j=0;j<i;j++)  t.ch[j]=chars[j];
t.length =i;
}

return OK;
}

void outputstring(heapstring t)
{
int i;
for (i=0;i<t.length ;i++)
printf("%c",t.ch[i]);
}

status extractword(heapstring t,wordlisttype &wd)
{
int num=0,i=0,len=t.length ,label=-1,j=0;
char p,q;
p=' ';
q=t.ch[i];
while (i<len)
{
if (p==' '&&q!=' ')
num++;
p=q;
q=t.ch[i];
i++;
}
printf("\nnumber=%d\n",num);

return 1;
}

main()
{
heapstring str;
char *ch=" 1001  this  is hhg  a ff book!----- ";
wordlisttype wd;
strassign(str,ch);
outputstring(str);
extractword(str,wd);

}


  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: