Play With Characters in C Programming Language:

Play With Characters in C Programming Language:

  1. Write a program to accept a character from the user and display the character.

  2. Write a program to accept a character in lower case and display it in uppercase.

  3. Write a program to accept a character from the user and display its ASCII value.

  4. Write a program to accept a character in uppercase and display it in lowercase.

  5. Write a program to display the ASCII values of 0 and 9.

  6. Write a program to display the ASCII values from 0 to 9.

  7. Write a program to display the ASCII values of all characters from A to Z.

  8. Write a program to accept a character using getch(),getche() and getchar() functions and display the character.

  9. Write a program to let the user press any key on the keyboard until x

 but count only how many upper case character keys are pressed on the keyboard?



Q1

Solution:

//program to accept a character from the user

#include<stdio.h>

int main()

{

    char ch;

    printf("Enter a character :");

    scanf("%c",&ch);

    printf("\n The character is %c",ch);

    return 0;


}//end of main


Q2

Solution:

//Program to accept a character in lower case and display it in uppercase.

#include<stdio.h>

int main()

{

    char ch;

    printf("Enter a character :");

    scanf("%c",&ch);

    ch=ch-32;

    printf("\n The character is %c",ch);

    return 0;


}//end of main







Q3

Solution:

//program to accept a character from the user and display its ASCII value.

#include<stdio.h>

int main()

{

    char ch;

    printf("Enter a character :");

    scanf("%c",&ch);

    printf("\n The ASCII value of %c is %d ",ch,ch);

    return 0;


}//end of main



Q4

Solution:

// program to accept a character in uppercase and display it in lowercase.

#include<stdio.h>

int main()

{

    char ch;

    printf("Enter a character in upper case :");

    scanf("%c",&ch);

    ch=ch+32;

    printf("\n The character in lower case is %c",ch);

    return 0;


}//end of main


Q5

Solution:

// program to display the ASCII values of 0 and 9.

#include<stdio.h>

int main()

{

    char ch1,ch2;

    ch1='0';

    ch2='9';

    printf("\n The ASCII value of 0 is %d",ch1);

    printf("\n The ASCII value of 9 is %d",ch2);

    return 0;


}//end of main


Q6

Solution:

//  program to display the ASCII values from 0 to 9.

#include<stdio.h>

int main()

{

    char ch;

    ch='0';

    int i;

    for(i=0;i<=9;i++)

    {

        printf("\n The ASCII of %c is %d",ch,ch);

        ch++;


    }

    return 0;


}//end of main


Q7

Solution:

/*program to display the ASCII values of all characters from A to Z.*/

#include<stdio.h>

int main()

{

    char ch;

    ch='A';

    int i;

    for(i=1;i<=26;i++)

    {

        printf("\n The ASCII of %c is %d",ch,ch);

        ch++;


    }

    return 0;


}//end of main



Q8

Solution:

/* program to accept a character using getch(),getche()

and getchar() functions and display the character.*/

#include<stdio.h>

int main()

{

    char ch;

   printf("\n Accepting a character using getch() :");

   ch=getch();

   printf("\n The character is %c",ch);


   printf("\n Accepting a character using getche() :");

   ch=getche();

   printf("\n The character is %c",ch);


   printf("\n Accepting a character using getchar() :");

   ch=getchar();

   printf("\n The character is %c",ch);

    return 0;


}//end of main


Q9

/* program to let the user press any key on the keyboard until x

 but count only how many upper case character keys are pressed on the keyboard?*/

#include<stdio.h>

int main()

{

    char ch;

   int keys=0;

   printf("\n Press any key including upper case alphabets:");

   while(1)

   {

        ch=getche();

        if((ch>=65)&&(ch<=90))

        {

            keys++;

        }

        if((ch=='x')||(ch=='X'))

        {

            break;

        }

   }//end of while loop

   printf("\n The uppercase character keys are pressed (%d) Times",keys);

getch();

    return 0;


}//end of main


Comments