字符串s中包含若干个回文字符子串,找出其中所有长度大于2的回文字符,然后按其长度的降序存放在字符串数组str中,若某个回文字符串已经存在str中,则丢弃。

**

题目:字符串s中包含若干个回文字符子串,找出其中所有长度大于2的回文字符,然后按其长度的降序存 放在字符串数组str中,若某个回文字符串已经存在str中,则丢弃。


例如:字符串s为:“ILoLoL.jiangsu151uhj”:
其中,回文子串有:“LoLoL”,“LoL”,“oLo”,“LoL”,“u151u”,“151”
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------------------------
编写程序:
1.编写函数int GenStr(int start,char s[ ] ,char str[ ] [ N ] ),从串s的str下标至最后一个字符之间查找回文字符串,若找到则保存到字符串数组str中,函数返回找到的回文字符串的个数。
2.编写函数void InsertStr(char str[ ][N],char step [ ][N],int n),将s的n个字符串按其长度的降序插入到字符串str中,如果插入的字符串已存在,则不插入。

 #include <stdio.h> 
 #include <string.h> 
 #include <stdlib.h>
 #include <math.h>
#include <ctype.h>
 #define N 255
 #define M 100
 
 int GenStr(int start,char s[ ] ,char str[ ] [ N ] )
 {
 int i,j,k,m;
 static p=0;
 j=strlen(s)-1;
 i=start;
 while(j-i>1)
 {
 	while(i<j)
 	if(s[i]!=s[j])j--;
 	else break;
 }
if(i!=j)
{
	k=1;m=j;
	while(i<=j)
	if(s[i]==s[j])
	{
	k=0;break;
	}
	else
	{
		i++;j--;
	}
		if(k)
		{
			j=0;
			for(i=start;i<=m;i++)
			str[p][j++]=s[i];
			str[p++][j]=0;
			j=m;i=start;j--;
		}
	}
}
return p;
}
void InsertStr(char str[ ]IN], char step[ ]IN], int n)
{
	int i,j,k,m=0,c;
	for(i=0;i<n;i++)
	{
		k=1;
		for(j=0;j<m;j++)
		if(strcmp(step[i],str[j])==0)
		{
			k=0;break;
		}
		if(k)
		{
				c=m-1;
				while(c>=0&&strlen(step[i])>strlen(str[c]))
				{
					strcpy(str[c+1], str[c]);
				}
		}
					c--;
				strcpy(str[c+1], step[i]);
				m++;
			}
		}
	}
int main( )
{
	FILE  *fp;
	int  i, n;
	char s[N]="ILoLoL.jiangsu151uhj";
	fp=fopen("DATA.dat" ,"w");
	if( fp==NULL)
	{
			printf("Write File Error");
			exit(0);
	}
	for (i=0; i<strlen(s)-2; i++)
	{
		n=GenStr(i,s, step);
		InsertStr(str,step,n);
	}
	printf("长度大于2的回文字符串有: \n");
	for(i=0; str[i][0]; i++)
	{
		puts(str[i]);
		fputs(str[i],fp);
	}
	fclose(fp);
	return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值