Tuesday, December 16, 2014

Longest Repeating Sequence in c



Sort the String and Repeated Characters should be present only Once

#include 
#include 
#include 
void main()
{
    int i, j = 0, k = 0;
    char str[100], str1[10][20], temp, min;
    clrscr();
    printf("enter the string:");
    scanf("%[^\n]s", str);
 
    for (i = 0; str[i]!= '\0';i++)
    {
        if (str[i] == ' ')
        {
            for (j = i;str[j] != '\0'; j++)
            {
                str[j] = str[j + 1];
            }
        }
    }
 
    for (i = 0;str[i]!= '\0';i++)
    {
        for (j = i + 1;str[j] != '\0';j++)
        {
            if (str[i] == str[j])
            {
                for (k = j; str[k] != '\0'; k++)
                str[k] = str[k+1];
                j--;
            }
        }
    }
 
    for (i = 0; str[i] != '\0'; i++) 
    {
        for (j = 0; str[j] != '\0';j++)
        {
            if (str[j] > str[i])
            {
                temp = str[i];
                str[i] = str[j];
                str[j] = temp;
            }
        }
    }
    printf("%s", str);
 getch();
}

Output
 
enter the string:abcde| bcd! abcdefg??
!?abcdefg|

Find the Length of the Longest Repeating Sequence in a String

#include 
#include 
#include 
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;
 
    printf("\nEnter the string");
    scanf(" %[^\n]s", string);
    for (len = 0;string[len] != '\0';len++);
    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++;
    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;
            }
            if (mlen < rlen)
            {
                s = i;
                mlen = rlen;
            }
        }
    }
   printf("\nLength of Longest Repeating Sequence:%d\n", mlen);
 
        for (i = s, j = 0;j < mlen;i++, j++)
            printf(" %s", words[i]);
        printf("\n");
getch();
}

Output
 
Enter the string
Welcome to C Programming Class,   Welcome Again to C Programming Class !
 
Length of Longest Repeating Sequence:4
to C Programming Class


Count the Number of Unique Words

#include  
#include 
#include 
#include 
int main()
{
    int i = 0, e, j, d, k, space = 0;
    char a[50], b[15][20], c[15][20];
    clrscr();
    printf("Read a string:\n");
    fflush(stdin);
    scanf("%[^\n]s", a);
    for (i = 0;a[i] != '\0';i++)        
    {
        if (a[i] =  = ' ')
            space++;
    }
    i = 0;
    for (j = 0;j<(space + 1);i++, j++)    
    {
        k = 0;
        while (a[i] != '\0')
        {
            if (a[i] == ' ')
            {
                break;
            }
            else
            {
                b[j][k++] = a[i];
                i++;
            }
        }
        b[j][k] = '\0';
    }
    i = 0;
    strcpy(c[i], b[i]);
    for (e = 1;e <= j;e++)        
 
    {
        for (d = 0;d <= i;d++)
        {
            if (strcmp(c[i], b[e]) == 0)
                break;
            else
            {
                i++;
                strcpy(c[i], b[e]);
                break;
            }
        }
    }
    printf("\nNumber of unique words in %s are:%d", a, i);
    return 0;
getch();
}

Output
 
Read a string:
Welcome to Sanfoundry's C-programming class,  Welcome again to C class!
The length of input string is:70
 
Number of unique words in Welcome to Sanfoundry's C-programming class, Welcome again to C class! are:8

No comments:

Post a Comment