Saturday, December 13, 2014

Combination of two Words in c pgm



Print Smallest and Biggest possible Word which is Palindrome in a given String

#include 
#include 
#include 
#include 
int main()
{
int i = 0, l = 0, j, k, space = 0, count = 0, init = 0, min = 0, max = 0, len = 0, flag;
    char a[100], b[30][20], c[30], d[30], minP[30], maxP[30];
    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';
    }
    for (j = 0;j < space + 1;j++)
        printf("%s ", b[j]);
    printf("\n");
    for (i = 0;i < space + 1;i++)
    {
        strcpy(c, b[i]); 
        count = strlen(b[i]);
        k = 0;
        for (l = count - 1;l >= 0;l--)
            d[k++] = b[i][l];
        d[k] = '\0';
        if (strcmp(d, c) == 0)                {
            flag = 1;
        if (init < 1) 
        {
            strcpy(minP, d);
            strcpy(maxP, d);
            min = strlen(minP);
            max = strlen(maxP);
            init++;
        }
        printf("String %s is a Palindrome\n", d);
        len = strlen(d);
        if (len >= max)
            strcpy(maxP, d);
        else if (len <= min)
            strcpy(minP, d);
        else
            printf("");
        }
    }
    if (flag == 1)
    {
        printf("The minimum palindrome is %s\n", minP);
        printf("The maximum palindrome is %s\n", maxP);
    }
    else
        printf("given string has no pallindrome\n");
getch();
}

Output
 
Read a string:
aba abcba abcdcba bcd
aba abcba abcdcba bcd
String aba is a Palindrome
String abcba is a Palindrome
String abcdcba is a Palindrome
The minimum palindrome is aba
The maximum palindrome is abcdcba
 
Read a string:
abc abcd
abc abcd
given string has no pallindrome



Print Combination of two Words of two given Strings without any Repetition

#include 
#include 
#include 
void main()
{
    char string[100], str[10], c[10];
    int z, occ = 0, i = 0, j = 0, count = 0, len = 0;
    clrscr();
    printf("Enter a string:");
    scanf("%[^\n]s", string);
    printf("Enter the word to check its occurence:");
    scanf("%s", str);
    len = strlen(str);
    for (i = 0;string[i] != '\0';i++)
    {
        count = 0;
        for (j = 0, z = i;j < len; j++, z++)
        {
            c[j] = string[z];
            if (c[j] == str[j])
            {
                count++; 
            }
        }
        if (count == len && string[z] == ' ')
        {
            occ++;        
        }
    }
    printf("The number of occ is %d\n", occ);
 getch();
}

Output
 
Enter a string:welcome to sanfoundry's c programming class,  welcome again to c class
Enter the word to check its occurence:welcome
The number of occ is 2
 
Enter a string:welcome to sanfoundry's c programming class,  welcome again to c class
Enter the word to check its occurence:c
The number of occ is 2

No comments:

Post a Comment