C Language convert string to int | Converting String to Integer in C
Method 1:
#include
#include
int main(){
char str[50];
int number;
//Accepting a numer in string format
printf("Enter any number as a string : ");
scanf("%s",str);
//Converting string into the number;
number = atoi(str);
if(number==0 && str[0]!='0')
printf("\nInvalid number");
else
printf("\n Equivalent number is : %d",number);
return 0;
}
Method 2:
#include
#include
#include
#include
char * toString(int);
int main(){
int number;
char *string;
//Accepting a number from user
printf("Enter any integer number : ");
scanf("%d",&number);
//Checking number is valid or not
if(isdigit(number)==0){
printf("\nInvalid number");
//exit(1); //to exit from program
}
//Converting int to string
string = toString(number);
//printing the string in console
printf("\nEqivalent string: %s",string);
return 0;
}
//function to conver int into string
char * toString(int num){
static char str[50];
char *ptr =(char *) #
int i,length=0,temp,le;
temp=num;
while(temp){
temp = temp/10;
length++;
}
le=length;
printf("le %d",length);
for(i=0;i<=le;i++){
printf("%d",num/pow(10,length) + 48);
num= num + (pow(10,length--));
}
str[i]='\0';
return str;
}
Related Links :
History Of C..
In the beginning was Charles Babbage and his Analytical Engine, a machine
he built in 1822 that could be programmed to carry out different computations.
Move forward more than 100 years, where the U.S. government in
1942 used concepts from Babbage’s engine to create the ENIAC, the first
modern computer.
Meanwhile, over at the AT&T Bell Labs, in 1972 Dennis Ritchie was working
with two languages: B (for Bell) and BCPL (Basic Combined Programming
Language). Inspired by Pascal, Mr. Ritchie developed the C programming
language.
My 1st Program...
#include
#include
void main ()
{
clrscr ();
printf ("\n\n\n\n");
printf ("\t\t\t*******Pankaj *******\n");
printf ("\t\t\t********************************\n");
printf ("\t\t\t\"Life is Good...\"\n");
printf ("\t\t\t********************************");
getch ();
}
Next Step...
#include
#include
void main ()
{
clrscr ();
printf ("\n\n\n\n\n\n\n\n");
printf ("\t\t\t --------------------------- \n\n");
printf ("\t\t\t | IGCT, Info Computers, INDIA | \n\n");
printf ("\t\t\t --------------------------- ");
getch ();
}
No comments:
Post a Comment