C语言判断字符串中的回文数,将其存放到一个二维数组里内。
问题描述:
我是萌新 ,不是很会 ,请求哪位大神 帮我把程序编出来 。
题目:字符串s中包含若干个回文字符子串,找出其中所有长度大于2的回文字符串,
然后将其存放在字符串数组str中,若某个回文字符串已在该数组中,则丢弃。
例如:字符串s为:"ILoLoL.jiangsu151uhj";
其中,回文子串有:"LoLoL", "LoL", "oLo", "LoL", "u151u","151"
在str中存储的字符串为:
LoLoL
u151u
LoL
oLo
151
编写程序:
从串s的start
下标至最后一个字符之间查找回文字符串,若找到则保存到字符串数组str中。
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <ctype.h>
#define N 250
#define M 250
int main()
{ char s[N]="ILoLoL.jiangsu151uhj";
char str[M][N]= {"\0"};
/Program/
/**********End **********/
printf("长度大于2的回文字符串有:\n");
for(i=0; str[i][0]; i++)
puts(str[i]);
return 0;
}
答
供参考:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
#include<ctype.h>
#define N 250
#define M 250
int main()
{
int i,j,l,len,k=0,m=0;
char s[N]="ILoLoL.jiangsu151uhj",str[M][N]={"\0"},tmp[M];
len = strlen(s);
for(i=0;i<len;i++){
for(j=i;j<len;j++){
tmp[k++] = s[j];
if(k > 2){
for(l=0;l<k/2;l++)
if(tmp[l] != tmp[k-l-1]) break;
if(l >= k/2) {
tmp[k] = '\0';
if(m == 0) strcpy(str[m++],tmp);
else{
for(l=0;l < m;l++){
if(strcmp(str[l],tmp) == 0)
break;
}
if(l >= m) strcpy(str[m++],tmp);
}
}
}
}
k = 0;
}
printf("长度大于2的回文字符串有:\n");
for(i=0;str[i][0];i++)
puts(str[i]);
return 0;
}