2008年12月9日 星期二

字串》最大相似尾巴 ( C語言 )

寫一個程式,它會讀入兩個字串,之後它會檢查並列出這兩個字串的尾巴最大相似部分。例如:字串"money"以及"honey"的最大相似尾巴是"oney",而"money"以及"cherry"的最大相似尾巴則是"y"。

1 則留言:

瑞克 提到...

#include < stdio.h >
#include < string.h >

void main(void)
{
char word1[10];
char word2[10];
int w1_Length,w2_Length;
int i,j=0;
char temp[10];

printf("enter word1:");
scanf("%s", &word1);
printf("enter word2:");
scanf("%s", &word2);

w1_Length = strlen(word1);
w2_Length = strlen(word2);


for(i=w1_Length-1; i>=0; i--)
{
if(word1[i] == word2[w2_Length-1])
{
w2_Length--;
temp[j] = word1[i];
//printf("%c", temp[j]);
j++;
}else
{
break;
}
}
temp[j] = '\0';
printf("\"%s\"以及\"%s\"的最大相似尾巴是\"%s\"", word1, word2, strrev(temp));
printf("\n");

}