wu :: forums
« wu :: forums - Repeated String »

Welcome, Guest. Please Login or Register.
Nov 28th, 2024, 9:17pm

RIDDLES SITE WRITE MATH! Home Home Help Help Search Search Members Members Login Login Register Register
   wu :: forums
   riddles
   cs
(Moderators: towr, Icarus, william wu, SMQ, Eigenray, ThudnBlunder, Grimbal)
   Repeated String
« Previous topic | Next topic »
Pages: 1  Reply Reply Notify of replies Notify of replies Send Topic Send Topic Print Print
   Author  Topic: Repeated String  (Read 815 times)
johny_cage
Full Member
***





   


Gender: male
Posts: 155
Repeated String  
« on: Nov 24th, 2007, 5:58am »
Quote Quote Modify Modify

Find the first 4 character substring in a given string that occurs more than once. and return its starting index. E.g. for “abcdebcxbcdebyz” output should be “bcde” and index is 1.
IP Logged
towr
wu::riddles Moderator
Uberpuzzler
*****



Some people are average, some are just mean.

   


Gender: male
Posts: 13730
Re: Repeated String  
« Reply #1 on: Nov 24th, 2007, 6:12am »
Quote Quote Modify Modify

Seems like a duplicate of the thread Finding "Substring Position".
Even the example used is the same, although Robin posed the question more generally.
« Last Edit: Nov 24th, 2007, 6:12am by towr » IP Logged

Wikipedia, Google, Mathworld, Integer sequence DB
downloadprj
Newbie
*





   


Posts: 4
Re: Repeated String  
« Reply #2 on: Nov 24th, 2007, 7:11am »
Quote Quote Modify Modify

Following code finds the repeating substring of size 4 or less in the input string.  
 
int Substr(char str[], char sub[], int subSize)
{
 for (int i = 0; str[i] != '\0'; i++)
 {
  for (int j = 0, k = i; (str[k] == sub[j]) && (j < subSize); j++, k++);
  if (j == subSize)
   return 1;
 }
 
 return 0;
}
 
int LongestRepeatingSubString(char str[])
{
 int length = 4;
 for (int sublen = length/2; sublen != 1; sublen--)
 {
  for (int i = 0; i + sublen <= length - sublen; i++)
  {
   if (Substr(str + sublen + i, str + i, sublen) == 1)
   {
    char t = str[i + sublen];
    str[i + sublen] = '\0';
    printf("%s\n", str + i);
    str[i + sublen] = t;
    return sublen;
   }
  }
 }
}
« Last Edit: Nov 24th, 2007, 7:14am by downloadprj » IP Logged
Pages: 1  Reply Reply Notify of replies Notify of replies Send Topic Send Topic Print Print

« Previous topic | Next topic »

Powered by YaBB 1 Gold - SP 1.4!
Forum software copyright © 2000-2004 Yet another Bulletin Board