C Program to Find Longer Repeating Sequence

«
»
This C Program find Longer Repeating Sequence.

Here is source code of the C Program to find Longer Repeating Sequence. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. /*
  2.  * C Program to find Longer Repeating Sequence 
  3.  */
  4. #include <stdio.h>
  5. #include <string.h>
  6.  
  7. void main()
  8. {
  9.     char s1[100], ar[10][20], ar1[10][20], new[10];
  10.     int i, j = 0, k = 0, l, count = 0, flag = 0, n, temp, len[20], maxlen = 0;
  11.  
  12.     printf("\nEnter the string:");
  13.     scanf(" %[^\n]s", s1);
  14.  
  15.     /*COPYING GIVEN STRING TO 2D ARRAY*/
  16.     for (i = 0;s1[i] != '\0';i++,j++)
  17.     {
  18.         if (s1[i] >= 33 && s1[i] <= 64)
  19.             i++;
  20.         if (s1[i] == ' ')
  21.         {
  22.             ar[k][j] = '\0';
  23.             k++;
  24.             i++;
  25.             j = 0;
  26.         }
  27.         ar[k][j] = s1[i];
  28.     }
  29.     ar[k][j] = '\0';
  30.     /*PLACING THE REPEATED WORDS AND LENGTHS INTO NEW ARRAY*/
  31.     l = 0;
  32.     for (i = 0;i <= k;i++)
  33.     {
  34.         for (j = i + 1;j <= k;j++)
  35.         {
  36.             if (strcmp(ar[i], ar[j]) == 0)
  37.             {
  38.                 for (n = 0;n < l && l != 0; n++)
  39.                 {
  40.                     if (strcmp(ar[i], ar1[k]) == 0)
  41.                     {
  42.                         flag = 1;
  43.                         break;
  44.                     }
  45.                 }
  46.                 if (flag != 1)
  47.                 {
  48.                     strcpy(ar1[l], ar[i]);
  49.                     len[l] = strlen(ar1[l]);
  50.                     l++;
  51.                 }
  52.                 flag = 0;
  53.                 break;
  54.             }
  55.         }
  56.     }
  57.     printf("\n");
  58.     /*SORTING IS DONE BASED ON THEIR LENGTHS*/
  59.     for (i = 0;i < l;i++)
  60.     {
  61.         for (j = i + 1;j < l;j++)
  62.         {
  63.             if (len[i] < len[j])
  64.             {
  65.                 temp = len[i];
  66.                 strcpy(new, ar1[i]);
  67.                 len[i] = len[j];
  68.                 strcpy(ar1[i], ar1[j]);
  69.                 len[j] = temp;
  70.                 strcpy(ar1[j], new);
  71.             }
  72.         }
  73.     }
  74.     maxlen = len[0];
  75.     for (i = 0;i < l;i++)
  76.     {
  77.       if (len[i] == maxlen)
  78.       printf("\nThe longer repeating sequence of the given string is: %s", ar1[i]);
  79.     }
  80. }

$ cc string22.c
$ a.out
Enter the string:Welcome to C Programming Class, Welcome Again to C Programming Class!
 
The longer repeating sequence of the given string is: Programming
 
Enter the string:Welcome to Sanfoundry, Welcome to C Class
 
 
The longer repeating sequence of the given string is: Welcome

Sanfoundry Global Education & Learning Series – 1000 C Programs.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
advertisement
advertisement

Here’s the list of Best Books in C Programming, Data-Structures and Algorithms

If you wish to look at programming examples on all topics, go to C Programming Examples.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & technical discussions at Telegram SanfoundryClasses.