在一个字符串中判断是否由两次重复子字符组成
)写一个函数 int isRePattern(const char s[]),它能够判断一个字符串是否由一个子串的两次重复组成,如字符串“abcabc”就是一个重复 Pattern,它由
子串“abc”的两次重复组成。其中,s 为待判断的字符串。并在主程序中测试该
函数。
2020-12-16 23:44
程序代码:#include <stdio.h>
#include <string.h>
_Bool IsRePattern( const char s[] )
{
size_t n = strlen(s);
return n%2==0 && memcmp(s,s+n/2,n/2)==0;
}
void test( const char* s )
{
printf( "\"%s\" %s a RePattern string.\n", s, IsRePattern(s)?"is":"isn't" );
}
int main( void )
{
test( "" );
test( "a" );
test( "aa" );
test( "ab" );
test( "abc" );
test( "abcabc" );
}
2020-12-17 08:43
2020-12-17 17:05