Showing posts with label String. Show all posts
Showing posts with label String. Show all posts

Thursday, 18 October 2018

Conversion of lowercase alphabet to uppercase or vice versa in C++

ASCII value/code


A computer is a digital device so it converts data & instructions into binary bits before performing any operation on it. The binary bits are generated by the encoder according to the character encoding standard. The binary bits may vary depending on the encoding standard used. The two most commonly used character encoding standards are ASCII and EBCDIC. Both the encoding standard have their own unique binary code for each character.

ASCII stands for American Standard Code for Information Interchange and is the most popular character encoding standard used for electronic communication (i.e. information interchange or transfer in electronics). In this standard, 128 characters are numerically encoded with a unique 7-bit binary combination known as 7-bit ASCII value. All the operations and processing within a digital system are performed on these binary combinations. But the decimal equivalent of the corresponding  7-bit binary codes is generally considered as ASCII values. Some of the characters with ASCII value and 7-bit ASCII value are given below.   

Character                    ASCII value                    7-bit ASCII
a                                   97                                    1100001
b                                   98                                    1100010
y                                   121                                  1111001
z                                   122                                  1111010
A                                   65                                   1000001
B                                   66                                   1000010
Y                                   88                                   1011001
Z                                   89                                   1011010
7                                   55                                    0110111
LF(Line feed)               10                                    0001010
                                                                                           FULL TABLE

From the table, we can observe that the ASCII value of any lowercase alphabet and the same uppercase alphabet differ by 32.
i.e.
               ASCII value of any uppercase alphabet + 32 = ASCII value of same lowercase alphabet

Example:
               ASCII value of 'B' + 32 = ASCII value of 'b'
                                       66 + 32 = 98


Write a program in C++ to input an alphabet from the user. If the entered character is in uppercase then convert it into lowercase or vice versa.


In this program, we input an alphabet from the user. We check the form(uppercase or lowercase) of the alphabet by comparing its ASCII value. If the entered alphabet is in uppercase we obtain the lowercase by adding 32 to the ASCII value. And if the entered alphabet is in lowercase we obtain the uppercase by subtracting 32 to the ASCII value. Then we display the alternative form of the alphabet.


PROGRAM


#include<iostream.h>
#include<conio.h>
void main()
{
char a;
clrscr();
cout<<"Enter an alphabet either in uppercase or lowercase=";
cin>>a;
if((a>=65)&&(a<=90))
{
a=a+32;
cout<<"\nLowercase="<<a;
}
else if((a>=97)&&(a<=122))
{
a=a-32;
cout<<"\nUppercase="<<a;
}
else
cout<<"\nInvalid input";
getch();
}


OUTPUT

Conversion of cases using ASCII value
PROGRAM TO FIND ALTERNATE CASE OF AN ALPHABET












EXPLANATION

In the program, 'r' is the entered alphabet whose ASCII value is 114. The alphabet is in lowercase so the second condition of the if....else if statement is true. The following expression is executed.

ASCII value of 'r' - 32 = 114-32
                                      = 82 (which is the ASCII value of 'R')

Finally, 'R' which is in uppercase was printed on the output screen.







Friday, 12 October 2018

Printing uppercase and lowercase alphabets in C++

English alphabet

Alphabets are the set of codes that enable written communication. English alphabets are the basic constituent of the English language. They are symbols or letters with a specific sound and are used in the formation of words in the English language. The English language contains 26 alphabets out of which 5 are vowels and 21 are consonants. The uppercase and lowercase are the two forms of the English alphabet. Commonly, uppercase letters are known as capital letters whereas lowercase letters are known as small letters.

Write a program in C++ to print the uppercase and lowercase form of all English alphabets.

In this program, we assign two character variables with 'a' and 'A' as their initial value. We will use the post-increment operator(++) to generate the successive alphabets till 'z' and 'Z'. We will use a for loop to print all the alphabets.


Program


#include<iostream.h>
#include<conio.h>
void main()
{
int n;
char capital='A',small='a';
clrscr();
for(n=1;n<=26;n++)
     {
     cout<<capital++<<small++<<"\t";
     }
getch();
}



Output


Program to A to Z
PRINTING UPPERCASE AND LOWER CASE ALPHABETS











Explanation

In the program, the for loop will be executed as follows:

n(1 to 26)             capital             small             Output screen
1                           A                      a                     Aa
2                           B                      b                     Bb
3                           C                      c                     Cc
.                            .                        .                      .
.                            .                        .                      .
.                            .                        .                      .
26                         Z                      z                     Zz

The post-increment operator will alter the value of variables(capital++ and small++) only after their values are printed. In this way, we obtain our required output on the screen.






Friday, 21 September 2018

Primary data types used in C++ Programming with an example

Data types in C++ 


Data types are used during the declaration of a variable which determined the type of data a variable can store. Primarily they are of three types:
  • Integer 
          If we declare int a; then a can store only integers values like 12, 3, 457.
  • Character
          If we declare char a; then a can store character and symbols like &, @, a, h, i.
  • Float
          If we declare float a; then a can store decimal values like 7.3333, 8.25.


Write a program in C++ giving an idea of primary data types. 


We need to consider different variables with different data types. We need to store the input data in their respective variables and print them. This will let us know about different data types.

Program


#include<iostream.h>
#include<conio.h>
void main()
{
long int admno;
char name[20];
int age;
float prcnt;
clrscr();
cout<<"Enter your admission number=";       //Input part
cin>>admno;
cout<<"Enter your name=";
cin>>name;
cout<<"Enter your age=";
cin>>age;
cout<<"Enter your percentage=";
cin>>prcnt;
cout<<"\nInformation";                                  //Output part    
cout<<"\nAdmission number="<<admno;
cout<<"\nName="<<name;
cout<<"\nAge="<<age;
cout<<"\nPercentage="<<prcnt<<"%";
getch();
}



Output

Example of data types
DATA TYPES IN C++











Explanation


In the program, each variable has own data type and stores a particular value only. The float data type is used to store percentage as it is a decimal number. The int data type can't store decimal so a float data type is used. The char data type is used to store characters and symbols.
// is used to add a comment. It starts from // and ends at end of a line.





Printing a String in C++ Programming

String

A string is a collection of characters which may be alphabets, numbers, symbols, and punctuation marks. E.g: $I am 21 years old$.

Write a program in C++ to print a string.


If we want to print a string directly into the output screen then this method is followed. The string is printed as per the choice of the programmer. The user can't alter this portion of the program on the output screen.

Program  


#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
cout<<"12345.Welcome to the basic of C++\n";
cout<<"@*Thank you*@";
getch();
}


Output


The output of string printing
PRINTING A STRING







Explanation

In the above program, the keyword cout is used to print a string.

Syntax:
cout<<"  ";

Example:
cout<<"Whatever is written inside the double inverted comma(" ") is printed on the screen.";