Thursday, September 15, 2011

Characters and patterns with Characters




Character is defined in C as ‘char’. It is a primitive data type. Variable of ‘char’ are 1 byte (8 bits) in length. ‘char’ can be signed or unsigned. A character is stored as one ASCII character per location.

As Characters stored as ASCII, it is possible to use + or – to get forward or backward.

e.g. char ch= ‘D’, ch++  will give us ‘E’ and ch--  will produce ‘C’. We can use ch+=5 or move character forward and ch-=5 for backward.

%c   used to print a character but if use use %d then we will get the ASCII value of it.
e.g. char ch= ‘D’, printf("\n%c  and %d  ",ch,ch);
output – D and 68.

ASCII values for character ‘A to Z’ – 65 to 90
ASCII values for character ‘a to z’ – 97 to 122
ASCII values for digits ‘0 to 9’ – 48 to 57
ASCII value for space is 32.


Input a number, count the numbers of digits present and find the sum of digits.

//Finding ASCII Value

#include <stdio.h>
#include <conio.h>

  void main()
      {
           char ch='A';
           int i;
           clrscr();
           printf("\nPrinting the ASCII values :\n");
           for(i=1;i<=26;i++)
              {
                   printf("\n\t%c\t%d",ch,ch);
                   ch++;
              }
           printf("\n\npress any key to Cont...");
           getch();
      }


//Printing Alphabets

#include <stdio.h>
#include <conio.h>
  void main()
      {
           char ch='A';
           int i;
           clrscr();
           printf("\nAlphabets from A-Z and a-z :\n");
           for(i=1;i<=26;i++)
              {
                   printf("\n\t%c\t%c",ch,ch+32);
                   ch++;
              }
           printf("\n\npress any key to Cont...");
           getch();
      }


//Character Pattern

#include <stdio.h>
#include <conio.h>

      void main()
      {
           char ch='A';
           int i,j,n;
           clrscr();
           printf("\nEnter the Limit : ");
           scanf("%d",&n);
           for(i=1;i<=n;i++)
              {
                   for(j=1;j<=i;j++)
                        {
                            printf("%c",ch);
                        }
                   printf("\n");
                   ch++;
              }
           printf("\n\npress any key to Cont...");
           getch();
      }


// Character Pattern

#include <stdio.h>
#include <conio.h>


      void main()
      {
           char ch;
           int i,j,n;
           clrscr();
           printf("\nEnter the Limit : ");
           scanf("%d",&n);
           for(i=1;i<=n;i++)
              {
                   ch='A';
                   for(j=1;j<=i;j++)
                        {
                            printf("%c",ch);
                            ch++;
                        }
                   printf("\n");

              }
           printf("\n\npress any key to Cont...");
           getch();
      }


// Character Pattern

#include <stdio.h>
#include <conio.h>

  void main()
      {
           char ch;
           int t,i,j,k,n,m=-1;
           clrscr();
           printf("\nEnter the Limit : ");
           scanf("%d",&n);
           for(i=n;i>=1;i--)
              {
                   ch='A';
                   for(j=1;j<=i;j++)
                        {
                            printf("%c",ch);
                            ch++;
                        }
                   for(k=1;k<=m;k++)
                        {
                            printf(" ");
                        }
                   m+=2;
                   if(i==n)
                        {
                            t=i-1;
                            ch-=2;
                        }
                   else
                        {
                            t=i;
                            ch--;
                        }
                   for(j=1;j<=t;j++)
                        {
                            printf("%c",ch);
                            ch++;
                        }

                   printf("\n");

              }
           printf("\n\npress any key to Cont...");
           getch();
      }


// Character Pattern

#include <stdio.h>
#include <conio.h>

  void main()
      {
           char ch;
           int t,i,j,k,n,m=-1;
           clrscr();
           printf("\nEnter the Limit : ");
           scanf("%d",&n);
           for(i=n;i>=1;i--)
              {
                   ch='A';
                   for(j=1;j<=i;j++)
                        {
                            printf("%c",ch);
                            ch++;
                        }
                   for(k=1;k<=m;k++)
                        {
                            printf(" ");
                        }
                   m+=2;
                   if(i==n)
                        {
                            t=i-1;
                            ch-=2;
                        }
                   else
                        {
                            t=i;
                            ch--;
                        }
                   for(j=1;j<=t;j++)
                        {
                            printf("%c",ch);
                            ch++;
                        }

                   printf("\n");
             }
             m=(n*2)-3;
           for(i=1;i<=n;i++)
              {
                   ch='A';
                   for(j=1;j<=i;j++)
                        {
                            printf("%c",ch);
                            ch++;
                        }
                   for(k=1;k<=m;k++)
                        {
                            printf(" ");
                        }
                   m-=2;
                   if(i==n)
                        {
                            t=i-1;
                            ch-=2;
                        }
                   else
                        {
                            t=i;
                            ch--;
                        }
                   for(j=1;j<=t;j++)
                        {
                            printf("%c",ch);
                            ch++;
                        }

                   printf("\n");
              }
           printf("\n\npress any key to Cont...");
           getch();
      }

No comments: