Thursday, December 18, 2014

Number of Vowels & Consonants in a c pgm



Check if a given String is Palindrome
#include
#include
#include
void main()
{
    char string[25], reverse_string[25] = {'\0'};
    int  i, length = 0, flag = 0;
    fflush(stdin);
    clrscr();
    printf("Enter a string \n");
    gets(string);
    for (i = 0; string[i] != '\0'; i++)
    {
        length++;
    }
    for (i = length - 1; i >= 0; i--)
    {
       reverse_string[length - i - 1] = string[i];
    }
   
    for (i = 0; i < length; i++)
    {
        if (reverse_string[i] == string[i])
            flag = 1;
        else
            flag = 0;
    }
    if (flag == 1)
        printf("%s is a palindrome \n", string);
    else
        printf("%s is not a palindrome \n", string);
getch();
}
Output

Enter a string
sanfoundry
sanfoundry is not a palindrome

Enter a string
malayalam
malayalam is a palindrome

 

Read two Strings & Concatenate the Strings

#include 
#include 
#include 
void main()
{
    char string1[20], string2[20];
    int i, j, pos;
    clrscr();
       
    memset(string1, 0, 20);
    memset(string2, 0, 20);
 
    printf("Enter the first string : ");
    scanf("%s", string1);
    printf("Enter the second string: ");
    scanf("%s", string2);
    printf("First string  = %s\n", string1);
    printf("Second string = %s\n", string2);
 
   
    for (i = 0; string1[i] != '\0'; i++)
    {
        ;
    }
    pos = i;
    for (j = 0; string2[j] != '\0'; i++)
    {
        string1[i] = string2[j++];
    }
   
    string1[i] = '\0';
    printf("Concatenated string = %s\n", string1);
 getch();
}

Output
 
Enter the first string : Ammu
Enter the second string: Kutty
First string  = Ammu
Second string = Kutty
Concatenated string = Ammukutty

Replace Lowercase Characters by Uppercase & Vice-Versa

#include 
#include 
#include 
void main()
{
    char sentence[100];
    int count, ch, i;
    clrscr();
    printf("Enter a sentence \n");
    for (i = 0; (sentence[i] = getchar()) != '\n'; i++)
    {
        ;
    }
    sentence[i] = '\0';
    count = i;
    printf("The given sentence is   : %s", sentence);
    printf("\n Case changed sentence is: ");
    for (i = 0; i < count; i++)
    {
        ch = islower(sentence[i])? toupper(sentence[i]) :
tolower(sentence[i]);
        putchar(ch);
    }
getch();
}

Output
 
Enter a sentence
wELCOME tO sANFOUNDRY
The given sentence is   : wELCOME tO sANFOUNDRY
Case changed sentence is: Welcome To Sanfoundry

Count the Number of Vowels & Consonants in a Sentence

#include 
#include
void main()
{
    char sentence[80];
    int i, vowels = 0, consonants = 0, special = 0;
 
    clrscr();
    printf("Enter a sentence \n");
    gets(sentence);
    for (i = 0; sentence[i] != '\0'; i++)
    {
        if ((sentence[i] == 'a' || sentence[i] == 'e' || sentence[i] ==
        'i' || sentence[i] == 'o' || sentence[i] == 'u') ||
        (sentence[i] == 'A' || sentence[i] == 'E' || sentence[i] ==
        'I' || sentence[i] == 'O' || sentence[i] == 'U'))
        {
            vowels = vowels + 1;
        }
        else
        {
            consonants = consonants + 1;
        }
        if (sentence[i] =='t' ||sentence[i] =='\0' || sentence[i] ==' ')
        {
            special = special + 1;
        }
    }
    consonants = consonants - special;
    printf("No. of vowels in %s = %d\n", sentence, vowels);
    printf("No. of consonants in %s = %d\n", sentence, consonants);
getch();
}
 
Output
 
Enter a sentence
welcome to sanfoundry
No. of vowels in welcome to sanfoundry = 7
No. of consonants in welcome to sanfoundry = 12

No comments:

Post a Comment