Hi!
I have seen a function to do "atoi for converting a string to its numeric equivalent. It copes with optional leading white space and an optional + or - sign. "
The structure of the program reflects the form of the input:

skip white space, if any

get sign, if any

get integer part and convert it

Given function is

#include <ctype.h>

/* atoi: convert s to integer; version 2 */

int atoi(char s[])

{

int i, n, sign;

for (i = 0; isspace(s[i]); i++) /* skip white space */

;

sign = (s[i] == '-') ? -1 : 1;

if (s[i] == '+' || s[i] == '-') /* skip sign */

i++;

for (n = 0; isdigit(s[i]); i++)

n = 10 * n + (s[i] - '0');

return sign * n;

}

I am unable to write a main function that calls it in order to give output.I am not sure where to give input and how to.I simply quite not understood the program.Please help me.
I have tried this..

#include<stdio.h>


main(){

atoi({abcdefghij});
}
#include <ctype.h>

/* atoi: convert s to integer; version 2 */

int atoi(char s[])

{

int i, n, sign;

for (i = 0; isspace(s[i]); i++) /* skip white space */

;

sign = (s[i] == '-') ? -1 : 1;

if (s[i] == '+' || s[i] == '-') /* skip sign */

i++;

for (n = 0; isdigit(s[i]); i++)

n = 10 * n + (s[i] - '0');

return sign * n;

}
main(){
int /* what to declare and input here*/
atoi(/*what to give here*/);
}

Are you trying to USE atoi() in your program, or are you trying to make your own atoi() function?

The C atoi() function will need a pointer to the char array, not this:
atoi({abcdefghij});

I have no idea what the abcd... stuff is all about.

Are you trying to USE atoi() in your program, or are you trying to make your own atoi() function?

The C atoi() function will need a pointer to the char array, not this:
atoi({abcdefghij});

I have no idea what the abcd... stuff is all about.

Sorry I have edited the post wrongly actually the program is here..
Now,I have given main function below the atoi function now I don't know how to take input for string and calling such atoi() what to give in () of atoi in main function.

#include<stdio.h>



#include <ctype.h>

/* atoi: convert s to integer; version 2 */

int atoi(char s[])

{

int i, n, sign;

for (i = 0; isspace(s[i]); i++) /* skip white space */

;

sign = (s[i] == '-') ? -1 : 1;

if (s[i] == '+' || s[i] == '-') /* skip sign */

i++;

for (n = 0; isdigit(s[i]); i++)

n = 10 * n + (s[i] - '0');

return sign * n;

}

main(){
int /* what to declare and input here*/
atoi(/*what to give here*/);
}

Sorry I have edited the post wrongly actually the program is here..
Now,I have given main function below the atoi function now I don't know how to take input for string and calling such atoi() what to give in () of atoi in main function.

#include<stdio.h>



#include <ctype.h>

/* atoi: convert s to integer; version 2 */

int atoi(char s[])

{

int i, n, sign;

for (i = 0; isspace(s[i]); i++) /* skip white space */

;

sign = (s[i] == '-') ? -1 : 1;

if (s[i] == '+' || s[i] == '-') /* skip sign */

i++;

for (n = 0; isdigit(s[i]); i++)

n = 10 * n + (s[i] - '0');

return sign * n;

}

main(){
int /* what to declare and input here*/
atoi(/*what to give here*/);
}

Actually,I have taken a screen shot and it is big in my system but is not here.Why I don't know so I am copying the text of lesson.
We have already encountered the while and for loops.


The structure of the program reflects the form of the input:

skip white space, if any

get sign, if any

get integer part and convert it

Each step does its part, and leaves things in a clean state for the next. The whole process terminates on the first character that could not be part of a number.

#include <ctype.h>

/* atoi: convert s to integer; version 2 */

int atoi(char s[])

{

int i, n, sign;

for (i = 0; isspace(s[i]); i++) /* skip white space */

;

sign = (s[i] == '-') ? -1 : 1;

if (s[i] == '+' || s[i] == '-') /* skip sign */

i++;

for (n = 0; isdigit(s[i]); i++)

n = 10 * n + (s[i] - '0');

return sign * n;

}

The standard library provides a more elaborate function strtol for conversion of strings to long integers;

First off ALWAYS put your code, between the code tags! It's VERY hard to study code like that is smashed together as html text.

Second, my atoi() function, is in stdlib.h, not ctype.h. Please recheck that on your compiler.

I can't tell if I'm looking at comma's or semi-colons on your code - post it up in between code tags. It will be ignored pretty much, otherwise.

Don't post about Shell sort and all the rest - I know Shell sort. Post up about your own problems with your own code. And please, limit the pic's - just copy and paste your code between code tags.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.