请编写程序实现以下功能。
(1)假设有一串英文文字,存储在字符串str 中。
(2)编写函数lettercount(),统计在字符串str中每个字母和数字字符出现的次数,统计结果形成一个链表,函数返回指向头结点的指针head。
(3)编写函数listcount(head),在屏幕上输出统计结果链表中,每个字母出现的次数,输出格式见示例。
要求:请使用链表实现上述两个函数的功能。
假设str初始为: aaa bbb cc sss 123 4567 abc 456 4567
屏幕输出:
a(4),b(4),c(3),s(3),1(1),2(1),3(1),4(3),5(3),6(3),7(2)
#include<stdio.h>
#include<malloc.h>
#include<string.h>
#define LEN sizeof(struct node)
struct node
{
char l;
int c;
struct node *next;
};
struct node *lettercount(char str[100])
{
struct node *p1,*p2,*head;
char letter[100]={'\0'};
int ci[100]={0},n1,n2=1,n=0,i,j,t=0;
n1=strlen(str);
letter[0]=str[0];
for (i=1;i<n1;i++)
{
for (j=0;j<n2;j++)
{
if (str[i]==letter[j])
{
ci[j]=ci[j]+1;
break;
}
else
t=t+1;
}
if (t==n2)
{
n2=n2+1;
letter[n2-1]=str[i];
}
t=0;
}
p1=p2=(struct node *)malloc(LEN);
if (letter[0]!=' ')
{
p1->l=letter[0];
p1->c=ci[0]+1;
}
head=NULL;
for (i=1;i<n2+1;i++)
{
n=n+1;
if (n==1)
head=p1;
else
p2->next=p1;
p2=p1;
p1=(struct node *)malloc(LEN);
if (letter[i]!=' ')
{
p1->l=letter[i];
p1->c=ci[i]+1;
}
else
{
i=i+1;
if (i!=n2)
{
p1->l=letter[i];
p1->c=ci[i]+1;
}
}
}
p2->next=NULL;
return(head);
}
void listcount(struct node *head)
{
struct node *p;
p=head;
while (p!=NULL)
{
printf("%c(%d) ",p->l,p->c);
p=p->next;
}
printf("\n");
}
void main()
{
struct node *head;
char str[100]={"aaa bbb cc sss 123 4567 abc 456 4567"};
head=lettercount(str);
listcount(head);
}
