Trusted by millions of Kenyans
Study resources on Kenyaplex

Get ready-made curriculum aligned revision materials

Exam papers, notes, holiday assignments and topical questions – all aligned to the Kenyan curriculum.

In the C++ standard library is the function strtol(), which has this prototype: long strtol|(const char*start, const **end, int base); The function cnverts the numeric string...

In the C++ standard library is the function strtol(), which has this prototype:
long strtol|(const char*start, const **end, int base);
The function cnverts the numeric string pointed to by start into a long integer. The number base of the numeric string is specified by base. Upon return, end points to the character in the string immediately following the end of the number. The long integer equivalent of the numeric string is returned. base must be in the range 2 to 38. However, most commonly, base 10 is used.
Create a function called mystrtol() that works the same as strtol() except that base is given the default argument of 10. Demonstrate that your version works correctly.

Answers


Davis
#include
#include
using namespace std;
long mystrtol(const char *s, char **end, int base = 10)
{
return strtol(s, end, base);
}
int main()
{
long x;
char *s1 ="100234 ";
char *p;
x = mystrtol(s1, &p,);
cout << "Base: "<x = mystrtol(s1, &p, 10);
cout << "Base 10: "<< x<< '\n';
x = (s1, &p); // use default base of 10
cout << "Base 10 byefault: "<< x<< '\n';
return 0;
}
Githiari answered the question on May 31, 2018 at 17:31

Answer Attachments

Exams With Marking Schemes

Related Questions