plz answer.. a program that takes a string e.g. "345" and
returns integer 345

Answers were Sorted based on User's Feedback



plz answer.. a program that takes a string e.g. "345" and returns integer 345..

Answer / ramu

int f(char s[])
{
int num=0,i;
for(int i=0;s[i]>='0' && s[i]<='9';i++)
n=10*n+(s[i]-'0');
if(i<sizeof(s))
{
printf("Error String");
return 0;
}
return n;
{

Is This Answer Correct ?    5 Yes 0 No

plz answer.. a program that takes a string e.g. "345" and returns integer 345..

Answer / swapnil chhajer

#include<stdio.h>
#include<stdlib.h>

int main()
{
char str[10];
printf("Enter the string : ");
gets(str);
printf("Converted integer : %d",atoi(str));
getchar();
}

Is This Answer Correct ?    5 Yes 0 No

plz answer.. a program that takes a string e.g. "345" and returns integer 345..

Answer / vadivel t

The equalent code to atoi() library fuction which i hav
written, below.


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

int MyAtoi(char *cptr);

main()
{
char *cptr = "123445";

printf("INTEGER EQU IS: %d\n", MyAtoi(cptr));
getch();
}
int MyAtoi(char *cptr)
{
int iptr = 0;
while((*cptr != '\0') && ((*cptr >= 48 && *cptr <= 57) ||
(*cptr == 32)))
{
if(*cptr != ' ')
iptr = (iptr * 10) + (*cptr - 48);
cptr++;
}
return iptr;
}

Is This Answer Correct ?    0 Yes 0 No

plz answer.. a program that takes a string e.g. "345" and returns integer 345..

Answer / vignesh1988i

#include<stdio.h>
#include<conio.h>
#include<string.h>
int fun(char *,int);
void main()
{
long int c,l;
char a1[20];
printf("enter the numerical string :");
scanf("%s",&a1);
l=strlen(a1);
c=fun(a,l);
printf("\n%ld",l);
getch();
}
int fun(char *a,long int l1)
{
long int a1[]=
{1,10,100,1000,10000,100000,1000000,10000000,100000000},c;
int p=a1[l1-1],c=0;
for(int i=0;a[i]!='\0';i++)
{
y=((int)a[i])*p; /*TYPE CASTING*/
c=c+y;
p=p/10;
}
return c;
}

Is This Answer Correct ?    1 Yes 2 No

Post New Answer

More C Interview Questions

Difference between constant pointer and pointer to a constant.

0 Answers   Huawei,


Write a program that can show the multiplication table.

0 Answers   Student,


i want to know aptitude questions,technical questions

2 Answers  


what is default constructor?

2 Answers   HCL,


difference between c and c++

3 Answers  






What is the difference between realloc() and free()

1 Answers  


How is null defined in c?

0 Answers  


what type of errors are checked during compilation

3 Answers  


when will be evaluated as true/ if(x==x==x) a) x=1; b) x=0; c) x=-1; d) none

7 Answers   HCL,


what is the output of the following program? #include<stdio.h> void main() { int x=4,y=3,z; z=x-- -y; printf("\n%d %d %d",x,y,z); }

14 Answers  


Why is a semicolon (;) put at the end of every program statement?

0 Answers  


#include main() { int i=1,j=2; switch(i) { case 1: printf("GOOD"); break; case j: printf("BAD"); break; } }

6 Answers   ME,


Categories