我需要交一个C语言课题设计 请大家帮忙给个小程序啊 七八十行的 注释最好详细点啊 谢谢咯
来源:百度知道 编辑:UC知道 时间:2024/05/31 06:40:43
最好是个小游戏或小软件 我是初学者 没解释看不懂哦 麻烦大家咯
这是对字符串操作的程序
#include<stdio.h>
#include<stdlib.h>
typedef struct
{
char *ch;
int length; //串长度
}HString;
void StrAssign(HString *T,char *chars) //生成一个值等于串常量chars的串T
{
int i,j;
char *c;
if(T->ch)
free(T->ch); //释放T原有空间
for(i=0,c=chars; *c; ++i,++c) //求chars长度
;
printf("the length is %d\n",i);
if(!i)
{
T->ch = NULL;
T->length = 0;
}
else
{
if(!(T->ch=(char*)malloc((i+1)*sizeof(char))))
exit(1);
for(j=0;j<i;++j)
T->ch[j] = chars[j];
T->ch[i] = '\0';
T->length = i;
}
}
int StrLength(HString s) //返回S的元素个数,称串的长度