This C Program finds the length of longest repeating sequence in a string.
Here is source code of the C Program to find the Length of the longest repeating sequence in a string. The C program is successfully compiled and run on a Linux system. The program output is also shown below.
/*
* C Program to Find the Length of the Longest Repeating Sequence in a String
*/
#include <stdio.h>
#include <string.h>
char string[100], words[100][100];
int len = 0, word_cnt = 0;
int main()
{
int i, j = 0, k, mlen = 0, rlen = 0, s = 0, c = 0, m = 0;
printf("\nEnter the string:");
scanf(" %[^\n]s", string);
for (len = 0;string[len] != '\0';len++);
/*
* Storing the individual words in an array
*/
for (k = 0;k < len;k++)
{
if (string[k] != ' ')
{
words[s][j] = string[k];
j++;
}
if (string[k] == ' ')
{
words[s][j] = '\0';
j = 0;
s++;
word_cnt++;
}
}
word_cnt++;
/*
* Compare on Word basis if same word is repeated then check next word & so on
* Increment a counter when consecutive words are repeated
*/
for (i = 0;i <= word_cnt;i++)
{
len = 0;
for (j = i+1;j <= word_cnt-1;j++)
{
if (strcmp(words[i], words[j]) != 0)
{
continue;
}
else if (strcmp(words[i], words[j]) == 0)
{
len++;
for (k = i+1, m = j+1;k < j;k++, m++)
{
if (strcmp(words[k], words[m]) == 0)
{
len++;
continue;
}
else
{
break;
}
}
if (rlen < len)
{
rlen = len;
len = 0;
}
len = 0;
}
/*
* Finding length of Longest Repeated Sequence
*/
if (mlen < rlen)
{
s = i;
mlen = rlen;
}
}
}
printf("\nLength of Longest Repeating Sequence:%d\n", mlen);
printf ("\nTotal number of words : %d", mlen);
printf ("\n The subsequence is : ");
for (i = s, j = 0;j < mlen;i++, j++)
printf(" %s", words[i]);
char subseq[100];
// concat all the words
for (i = s, j = 0; j < mlen; i++, j++)
strcat (subseq, words[i]);
printf ("\nThe length of longest common subsequence is: %d", strlen(subseq)+1);
printf("\n");
}
Enter the string: Welcome to C Programming Class, Welcome Again to C Programming Class! Length of Longest Repeating Sequence:3 Total number of words : 3 The subsequence is : to C Programming The length of longest common subsequence is : 19
Sanfoundry Global Education & Learning Series – 1000 C Programs.
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.
Next Steps:
- Get Free Certificate of Merit in C Programming
- Participate in C Programming Certification Contest
- Become a Top Ranker in C Programming
- Take C Programming Tests
- Chapterwise Practice Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
- Chapterwise Mock Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
Related Posts:
- Apply for C Internship
- Buy C Books
- Watch Advanced C Programming Videos
- Practice Computer Science MCQs
- Buy Computer Science Books