Saturday, December 13, 2014

Largest & Smallest Word in a String in c



Find the Consecutive Occurrence of any Vowel in a String

#include 
#include 
#include 
#include 
 struct detail
{
    char word[20];
};
 
int update(struct detail [], const char [], int);
int vowelcheck(char);
 
int main()
{
    struct detail s[10];
    char string[100], unit[20], c;
    int i = 0, j = 0, count = 0;
 
    printf("Enter string: ");
    i = 0;
    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] != ' ' && isalnum(string[i]))
        {
            unit[j++] = string[i++];
        }
        if (j != 0)
        {
            unit[j] = '\0';
            count = update(s, unit, count);
            j = 0;
        }
    }
 
    printf("**Words with consecutive vowel**\n");
    for (i = 0; i < count; i++)
    {
        printf("%s\n", s[i].word);
    }
 
    return 0;
}
 
int update(struct detail s[], const char unit[], int count)
{
    int i, j = 0;
 
    for (i = 0; i < strlen(unit) - 1; i++)
    {
        if (vowelcheck(unit[i]))
        {
            if (vowelcheck(unit[i+ 1]))
            {
                /*To avoid duplicate strings*/
                while (j < count && strcmp(s[j].word, unit))
                {
                    j++;
                }
                if (j == count)
                {
                    strcpy(s[j].word, unit);
 
                    return (count + 1);
                }
            }
        }
    }
 
    return count;
}
 
int vowelcheck(char c)
{
    char vowel[5] = {'a', 'e', 'i', 'o', 'u'};
    int i;
 
    c = tolower(c);
    for (i = 0; i < 5; i++)
    {
        if (c == vowel[i])
        {
            return 1;
        }
    }
 return 0;
getch();
}
 
 
 
 
 
Output
 
Enter string: Who will lead his team to victory
The string entered is: Who will lead his team to victory
**Words with consecutive vowel**
lead
team

Find the Largest & Smallest Word in a String

#include 
#include 
#include 
#include 
 
int main()
{
    char string[100], word[20], max[20], min[20], c;
    int i = 0, j = 0, flag = 0;
    clrscr();
    printf("Enter string: ");
    i = 0;
    do
    {
        fflush(stdin);
        c = getchar();
        string[i++] = c;
 
    } while (c != '\n');
    string[i - 1] = '\0';
    for (i = 0; i < strlen(string); i++)
    {
        while (i < strlen(string) && !isspace(string[i]) && isalnum(string[i]))
        {
            word[j++] = string[i++];
        }
        if (j != 0)
        {
            word[j] = '\0';
            if (!flag)
            {
                flag = !flag;
                strcpy(max, word);
                strcpy(min, word);
            }
            if (strlen(word) > strlen(max))
            {
                strcpy(max, word);
            }
            if (strlen(word) < strlen(min))
            {
                strcpy(min, word);
            }
            j = 0;
        }
    }
    printf("The largest word is '%s' and smallest word is '%s' in '%s'.\n", max, min, string);
 
    return 0;
 getch();
}
 
Output
 
Enter string: amazing programmers exists here
The largest word is 'programmers' and smallest word is 'here' in 'amazing programmers exi

No comments:

Post a Comment