Saturday, December 13, 2014

String is a Circular Permutation of in c



Count the Occurrences of each C Keyword using Array Structure

#include 
#include 
#include 
#include 
#define keymax 32
 
struct keyword
{
    char word[10];
    int occur;
};
 
int binarysearch(char [], struct keyword[]);
 
int main()
{
    int i = 0, j = 0, pos;
    char string[100], unit[20], c;
    struct keyword key[32] = {"auto", 0, "break", 0, "case", 0,
                          "char", 0, "const", 0, "continue", 0,
                          "default", 0, "do", 0, "double", 0,
                          "else", 0, "enum", 0, "extern", 0,
                          "float", 0, "for", 0, "goto", 0,
                          "if", 0, "int", 0, "long", 0,
                          "register", 0, "return", 0, "short", 0,
                          "signed", 0, "sizeof", 0, "static", 0,
                          "struct", 0, "switch", 0, "typedef", 0,
                          "union", 0, "unsigned", 0, "void", 0,
                          "volatile", 0, "while", 0,};
 
    printf("Enter string: ");
    do
    {
        fflush(stdin);
        c = getchar();
        string[i++] = c;
 
    } while (c != '\n');
    string[i - 1] = '\0';
    printf("The string entered is: %s\n", string);
    for (i = 0; i < strlen(string); i++)
    {
        while (i < strlen(string) && string[i] != ' ' && isalpha(string[i]))
        {
            unit[j++] = tolower(string[i++]);
        }
        if (j != 0)
        {
            unit[j] = '\0';
            pos = binarysearch(unit, key);
            j = 0;
            if (pos != -1)
            {
               key[pos].occur++;
            }
        }
    }
    printf("***********************\n   Keyword\tCount\n***********************\n");
    for (i = 0; i < KEYMAX; i++)
    {
        if (key[i].occur)
        {
            printf("    %s\t  %d\n", key[i].word, key[i].occur);
        }
    }
 
    return 0;
}
 
int binarysearch(char *word, struct keyword key[])
{
    int low, high, mid;
 
    low = 0;
    high = KEYMAX - 1;
    while (low <= high)
    {
        mid = (low + high) / 2;
        if (strcmp(word, key[mid].word) < 0)
        {
            high = mid - 1;
        }
        else if (strcmp(word, key[mid].word) > 0)
        {
            low = mid + 1;
        }
        else
        {
            return mid;
        }
    }
 
    return -1;
 getch();
}

Output
 
Enter string: break, float and double are c keywords. float and double are primitive data types.  
The string entered is: break, float and double are c keywords. float and double are primitive data types.
***********************
   Keyword      Count
***********************
    break             1
    double           2
    float   2


Determine if One String is a Circular Permutation of Another String

#include 
#include 
#include 
#include 
#include 
#define char_size 26
 
void alphacheck(char *, int []);
void create(char [], char [], int[]);
 
int main()
{
    char str1[50], str2[50];
    int a1[char_size] = {0};
    char str2_rem[50];
    clrscr();
    printf("Enter string1: ");
    scanf("%s", str1);
    printf("Enter string2: ");
    scanf("%s", str2);
    alphacheck(str1, a1);
    create(str2_rem, str2, a1);
    printf("On removing characters from second string we get: %s\n", str2_rem);
 
    return 0;
}
 
void alphacheck(char *str, int a[])
{
    int i, index;
 
    for (i = 0; i < strlen(str); i++)
    {
        str[i] = tolower(str[i]);
        index = str[i] - 'a';
        if (!a[index])
        {
            a[index] = 1;
        }
    }
    printf("\n");
}
 
void create(char str_rem[], char str[], int list[])
{
    int i, j = 0, index;
 
    for (i = 0; i < strlen(str); i++)
    {
        index = str[i] - 'a';
        if (!list[index])
        {
            str_rem[j++] = str[i];
        }
    }
    str_rem[j] = '\0'; 
getch();
}

Output
 
Enter string 1: abcd
Enter string 2: dabc
abcd & dabc are circular permutation of each other.

No comments:

Post a Comment