Tuesday, October 28, 2014

C String Pro

Find Highest Frequency Character in a String

#include
#include
#include
char string1[100], visited[100];
int count[100] = {0}, flag = 0;
void main()
{
    int i, j = 0, k = 0, l, max, index;
    clrscr();
    printf("Enter a string : ");
    scanf("%[^\n]s", string1);

    l = strlen(string1);

    for (i = 0; i < l; i++)
    {
        if (i == 0)
        {
            visited[j++] = string1[i];
            count[j - 1]++;
        }
        else
        {
            for (k = 0; k  < j; k++)
            {
                if (string1[i] == visited[k])
                {
                    count[k]++;
                    flag = 1;
                }
            }
            if (flag == 0)
            {
                visited[j++] = string1[i];
                count[j - 1]++;
            }
            flag = 0;
        }
    }   

    for (i = 0; i < j; i++)
    {
        if ((i == 0) && (visited[i] != ' '))
        {
            max = count[i];
            continue;
        }
        if ((max < count[i]) && (visited[i] != ' '))
        {
            max = count[i];
            index = i;
        }
    }

    printf("\nMax repeated character in the string = %c ", visited[index]);
    printf("\nIt occurs %d times", count[index]);
    getch();
}

Output

Enter a string : Welcome to Sanfoundry's C Programming Class !
Max repeated character in the string = o
It occurs 4 times

Display every possible Combination of two Words or Strings from the input Strings without Repeated Combinations

#include 
#include 
#include 
void main()
{
    int i, j = 0, k, k1 = 0, k2 = 0, row = 0;
    char temp[50];
    char str[100], str2[100], str1[5][20], str3[6][20], str4[60][40];
    clrscr();
    printf("enter the string :");
    scanf(" %[^\n]s", &str);
    printf("enter string:");
    scanf(" %[^\n]s", &str2);
 
              for (i = 0;str[i] != '\0'; i++)
    {
        if (str[i] == ' ')
        {
            str1[k1][j] = '\0';
            k1++;
            j = 0;
        }
        else
        {
            str1[k1][j] = str[i];
            j++;
        }
    }
    str1[k1][j] = '\0';
    j = 0;
    for (i = 0;str2[i] != '\0';i++)
    {
        if (str2[i] == ' ')
        {
            str3[k2][j] = '\0';
            k2++;
            j = 0;
        }
        else
        {
            str3[k2][j] = str2[i];
            j++;
        }
    }
    str3[k2][j] = '\0';
 
    row = 0;
    for (i = 0;i <= k1;i++)
    {
        for (j = 0;j <= k2;j++)
        {
            strcpy(temp, str1[i]);
            strcat(temp, str3[j]);
            strcpy(str4[row], temp);
            row++;
        }
    }
    for (i = 0;i <= k2;i++)
    {
        for (j = 0;j <= k1;j++)
        {
            strcpy(temp, str3[i]);
            strcat(temp, str1[j]);
            strcpy(str4[row], temp);
            row++;
        }
    }
 
    for (i = 0;i < row;i++)
    {
        for (j = i + 1;j < row;j++)
        {
            if (strcmp(str4[i], str4[j]) == 0)
            {
                for (k = j;k <= row;k++)
                {
                    strcpy(str4[k], str4[k + 1]);
                }
                row--;
            }
        }
    }
 
    for (i = 0;i < row;i++)
    {
        printf("\n%s", str4[i]);
    }
    getch();
}
 
Output
 
enter the string :welcome to sanfoundry's class
enter string:welcome to c programming class
 
welcomewelcome
welcometo
welcomec
welcomeprogramming
welcomeclass
towelcome
toto
toc
toprogramming
toclass
sanfoundry'swelcome
sanfoundry'sto
sanfoundry'sc
sanfoundry'sprogramming
sanfoundry'sclass
classwelcome
classto
classc
classprogramming
classclass
welcomesanfoundry's
tosanfoundry's
cwelcome
cto
csanfoundry's
cclass
programmingwelcome
programmingto
programmingsanfoundry's
programmingclass
classsanfoundry's

No comments:

Post a Comment