按規則,一個字元的串也是天經地義的迴文字串,但這毫無意義,所以下面的程式碼把這種情況排除在外:;//#include "stdafx.h"//If the vc++6.0, with this line.#include "stdio.h"#include "string.h"#include "stdlib.h"int main(void){ int i,j,la,lb; char a[50],b[50]; while(1){ printf("Input a string(length<50)... str="); if(gets(a) && (la=strlen(a))<50) break; printf("Too long, enter again. "); } printf(" "); for(i=0;i<la;i++) for(j=2;j<=la-i;j++){ strncpy(b,a+i,j); b[j]=""; lb=strlen(b); if(!memcmp(strrev(b),a+i,lb)) printf("%s a[%d] to a[%d] ",b,i,i+lb-1); } printf(" "); return 0;}
按規則,一個字元的串也是天經地義的迴文字串,但這毫無意義,所以下面的程式碼把這種情況排除在外:;//#include "stdafx.h"//If the vc++6.0, with this line.#include "stdio.h"#include "string.h"#include "stdlib.h"int main(void){ int i,j,la,lb; char a[50],b[50]; while(1){ printf("Input a string(length<50)... str="); if(gets(a) && (la=strlen(a))<50) break; printf("Too long, enter again. "); } printf(" "); for(i=0;i<la;i++) for(j=2;j<=la-i;j++){ strncpy(b,a+i,j); b[j]=""; lb=strlen(b); if(!memcmp(strrev(b),a+i,lb)) printf("%s a[%d] to a[%d] ",b,i,i+lb-1); } printf(" "); return 0;}