Wednesday, 18 November 2015

'Character' to 'integer' datatype conversion



A very simple way to convert the character format into the integeral format is the subtraction of the ASCII value of character '0' from the ASCII value of the cosidered character.

for example, lets see a code

char ch;
ch='5';
int n;

n=ch-'0';        //character to integer conversion 

Program requiring conversion :

Input : String "f5b6c3s4"

Output: fffffbbbbbbcccssss

program code :

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>
void main()
{
clrscr();
char ch[10];
cout<<"enter the code\n";
gets(ch);
int n=strlen(ch);
for(int i=0;i<n;i++)
{
char ch1=ch[i];
i++;
int c=ch[i]-'0';          //character to integer conversion
for(int j=0;j<c;j++)
cout<<ch1;
}
getch();
}



No comments:

Post a Comment