Description
A character string is said to has period K If it can be formed by concatenating one or more repetitions of another string of length k. For example, the string "abcabcabcabc" had period 3, since it was formed by 4 repetitions of the string "abc ". It also has periods 6 (both repetitions of "abcabc") and (one repetition of "abcabcabcabc").
Write a program to read a character string and determine its smallest period.
Input
The first line oif the input file would contain a single integer N indicating what many test case which your program would TES T followed by a blank line. Each of the test case would contain a single character string of Non-blank characters. Consecutive input would separated by a blank line.
Output
An integer denoting the smallest period of the input string for each input. The consecutive output is separated by a blank line.
Sample Input
1HoHoHo
Sample Output
2
Code Implementation
#include <stdio.h>#include<string.h>intIsloopstring (CharStr[],intAintb);intMain () {intn,i,k; Charstr[ -]; scanf ("%d",&N); while(n--) {scanf ("%s", str); for(i=1; I<=strlen (str); i++) { if(strlen (str)%i==0) { if(isloopstring (Str,i,strlen (str))) {k=i; Break; } } } if(n==0) printf ("%d\n", K); Elseprintf ("%d\n\n", K); } return 0;}intIsloopstring (CharStr[],intAintb) { inti,j,flag=1; for(i=0; i<a;i++) { for(j=i+a;j<b;j+=a) {if(str[j]!=Str[i]) {Flag=0; return 0; } } } if(flag)return 1;}
Exercise 3-4 Cycle string UVa455