C / C++
C / C++ - isdigit isalpha atoi stoi
https://stackoverflow.com/questions/11523569/how-can-i-avoid-char-input-for-an-int-variable
The function isdigit() can be used to tell digits and alphabets apart. This function is present in the <cctype> header.
for(int i=0; char[i]!='\0';i++){
if(!isdigit(str[i]))
return false;
}
return true;
http://www.cplusplus.com/reference/cctype/isdigit/
/* isdigit example */
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
int main ()
{
char str[]="1776ad";
int year;
if (isdigit(str[0]))
{
year = atoi (str);
printf ("The year that followed %d was %d.\n",year,year+1);
}
return 0;
}
https://stackoverflow.com/questions/20583945/what-is-the-difference-between-stdatoi-and-stdstoi
In order to convert that string to an integer, you’d have to do the following:
int my_integer = atoi(my_c_string);
C++11 offers a succinct replacement:
int my_integer = std::stoi(my_string);
1). Are there any other differences between the two?
2). Efficiency and performance wise which one is better?
3). Which is safer to use?
1). Are there any other differences between the two?
I find std::atoi() a horrible function: It returns zero on error. If you consider zero as a valid input, then you cannot tell whether there was an error during the conversion or the input was zero. That's just bad. See for example:
For C++11 and later:
The go-to function for string-to-integer conversion is now stoi, which takes a string and returns an int,
or throws an exception on error. You can properly distinguish errors from zero as input.
No need for the verbose istringstream hack mentioned in the accepted answer anymore.
(There's also stol/stoll/stof/stod/stold for long/long long/float/double/long double conversions, respectively.)
2). Efficiency and performance wise which one is better?
If you don't care about correctness or you know for sure that you won't have zero as input or you consider that an error anyway, then, perhaps the C functions might be faster (probably due to the lack of exception handling). It depends on your compiler, your standard library implementation, your hardware, your input, etc. The best way is to measure it. However, I suspect that the difference, if any, is negligible.
If you need a fast (but ugly C-style) implementation, the most upvoted answer to the How to parse a string to an int in C++? question seems reasonable. However, I would not go with that implementation unless absolutely necessary (mainly because of having to mess with char* and \0 termination).
3). Which is safer to use?
See the first point.
In addition to that, if you need to work with char* and to watch out for \0 termination, you are more like to make mistakes; std::string is much easier and safer to work with as it will take care of all these stuff.
https://www.geeksforgeeks.org/isalpha-isdigit-functions-c-example/
// C program to demonstrate working of isalpha() and
// isdigit().
#include<stdio.h>
#include<stdlib.h>
int main()
{
char str[] = "12abc12";
int alphabet = 0, number = 0, i;
for (i=0; str[i]!= '\0'; i++)
{
// check for alphabets
if (isalpha(str[i]) != 0)
alphabet++;
// check for decimal digits
else if (isdigit(str[i]) != 0)
number++;
}
printf("Alphabetic_letters = %d, "
"Decimal_digits = %d\n", alphabet, number);
return 0;
}
https://www.youtube.com/watch?v=J_AEhwphwe8
isdigit() is a bool function
/* isdigit example */
#include <iostream>
#include <cctype>
using namespace std;
int main()
{
char c = '3';
char c2 = 'w';
cout << isdigit(c) << endl; // 1 (true)
cout << isdigit(c2) << endl; // 0 (false)
return 0;
}