C语言 关于字符串处理的程序题知道

C语言 关于字符串处理的程序题知道,第1张

#include

<stdio.h>

#include<string.h>

#include<stdlib.h>

struct

STKY

{

char

keyword[100]

int

count

float

frq//出现频率

struct

STKY

*next

}*h//带头结点的单链表,头结点h->count中保留了句子中的单词个数,其他节点count保存的是keyword出现的次数

int

AddList(struct

STKY

*h,char

*szText)

{

struct

STKY

*p,*q

char

szTemp[50]={0}

int

i=0,j=0

h->count=1

while(szText[i])

{

if(szText[i]!='

')

szTemp[j++]=szText[i]

else

{

h->count++

szTemp[j]='\0'

p=q=h

if(p->next==NULL)

{

p=(struct

STKY*)malloc(sizeof(struct

STKY))

p->next=NULL

strcpy(p->keyword,szTemp)

p->count=1

q->next=p

p=q

}

else

{

p=q=h->next

while(p)

{

if(strcmp(p->keyword,szTemp)==0)

{

p->count++

break

}

q=p

p=p->next

}

if(p==NULL)

{

p=(struct

STKY*)malloc(sizeof(struct

STKY))

p->next=NULL

strcpy(p->keyword,szTemp)

p->co...

#include

<stdio.h>

#include<string.h>

#include<stdlib.h>

struct

STKY

{

char

keyword[100]

int

count

float

frq//出现频率

struct

STKY

*next

}*h//带头结点的单链表,头结点h->count中保留了句子中的单词个数,其他节点count保存的是keyword出现的次数

int

AddList(struct

STKY

*h,char

*szText)

{

struct

STKY

*p,*q

char

szTemp[50]={0}

int

i=0,j=0

h->count=1

while(szText[i])

{

if(szText[i]!='

')

szTemp[j++]=szText[i]

else

{

h->count++

szTemp[j]='\0'

p=q=h

if(p->next==NULL)

{

p=(struct

STKY*)malloc(sizeof(struct

STKY))

p->next=NULL

strcpy(p->keyword,szTemp)

p->count=1

q->next=p

p=q

}

else

{

p=q=h->next

while(p)

{

if(strcmp(p->keyword,szTemp)==0)

{

p->count++

break

}

q=p

p=p->next

}

if(p==NULL)

{

p=(struct

STKY*)malloc(sizeof(struct

STKY))

p->next=NULL

strcpy(p->keyword,szTemp)

p->count=1

q->next=p

p=q

}

}

j=0

}

i++

}

////////////////////计算出现频率

p=q=h->next

if(p==NULL)

return

1

while(p)

{

p->frq=(float)(p->count*1.0/h->count)

p=p->next

}

return

0

}

int

main()

{

char

szText[1024]="good

morning

just

have

a

good

breakfast

"//测试的字符串

char

szFormat[1024]={0}

int

i=0,j=0

int

iPrev=0

struct

STKY

*p

while(szText[i])

{

if(szText[i]!='

')

{

szFormat[j++]=szText[i]

iPrev=0

}

else

{

if(iPrev==0)

szFormat[j++]=szText[i]

iPrev=1

}

i++

}

if(szFormat[j-1]=='

')

szFormat[j-1]='\0'

puts("处理前的字串:")

puts(szText)

strcpy(szText,szFormat)

puts("处理后的字串:")

puts(szText)

////////////////////////////////////////////

h=(struct

STKY*)malloc(sizeof(struct

STKY))

h->next=NULL

AddList(h,szText)

p=h->next

puts("计算出的单词出现频率")

while(p)

{

printf("%s:\n%f\n",p->keyword,p->frq)

p=p->next

}

return

0

}

展开

#include<stdio.h>

#include<string.h>

int main()

{char s1[200],s2[200],s3[200]

 int a,b,c,n=0

 gets(s1)

 gets(s2)

 a=strlen(s1)

 b=strlen(s2)

 while(a-n>=b)

 {strcpy(s3,s1+n)

  s3[b]='\0'

  if(strcmp(s2,s3)==0)

  {printf("%d\n",n)

   return 0

  }

  n++  

 }

 printf("Not found!  %d\n",n)

 return 0

}


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/yw/7915700.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-04-11
下一篇2023-04-11

发表评论

登录后才能评论

评论列表(0条)

    保存