In: Computer Science
Currently the code outputs 3. Is there a way to make it output 32 writing logic in the parseint and not the main function?
#include <stdio.h>
#include <ctype.h>
int main()
{
printf("%d\n", parseint("32"));
return 0;
}
int parseint(char *str) {
int i=0;
while(str[i] != '\0'){
return (str[i] - '0');
i++;
}
return 1;
The statement that is being updated is provided below:
return (str[i] - '0');
The above statement is updated as follows:
x = x*10 + str[i] - '0';
Explanation:
The complete correct code that display 32 is provided below:
Screenshot of the code:
//The modified lines are highlighted in yellow:
Sample Output:
Code To Copy:
#include <stdio.h>
#include <ctype.h>
int main()
{
printf("%d\n", parseint("32"));
return 0;
}
int parseint(char *str) {
int i=0 ,x = 0;
while(str[i] != '\0')
{
x = x*10 + str[i] - '0';
i++;
}
return x;
}
//For any query please comment. Please provide the positive feedback. Thanks