. Write a function in C that takes an array of char and a char ** expression as
ID: 3626466 • Letter: #
Question
. Write a function in C that takes an array of char and a char ** expression as parameters. The array of type char would contain a string the possibly starts with a sequence of decimal digits. Have the function convert the sequence of decimal digits it finds to type int and return the converted value. Have the function set the contents of its second parameter (a char * value) to point to the first character in the array that was not converted. For example, if the function was called convert () and the call to convert () wasintval = convert (array, & charptr);
where array contained the string
2 7 4 A D A M S
then after the call, intval would contain the 274, and charptr would point to the ‘A’ in the string.
Explanation / Answer
#include< stdio.h >
#include< cctype.h >
int convert(char ar[],char *pointer);
void main()
{
char ar[]="274ADAMS";
char charpointer=ar[0];
//function call
int intVal=convert(ar,&charpointer);
printf("Value=%d %c",intVal,charpointer);
getch();
}
int convert(char ar[],char *pointer)
{
int i=0,value=0;
while(ar[i]!='')
{
if(isalpha(ar[i]))
{ *pointer=ar[i];
break;}
i++;
}
if (isdigit(ar[0]))
value = atoi (ar);
return value;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.