How to write a function that takes in a string parameter and checks to see whether or not it is an integer, and if it is then return the integer value

#include <stdio.h>

int strtoint(char * s) {
  int index = 0, flag = 0;

  while ( * (s + index) != '
#include <stdio.h>
int strtoint(char * s) {
int index = 0, flag = 0;
while ( * (s + index) != '\0') {
if (( * (s + index) >= '0') &&
*
(s + index) <= '9') {
flag = 1;
index++;
} else {
flag = 0;
break;
}
}
if (flag == 1)
return atoi(s);
else
return 0;
}
main() {
printf("%d", strtoint("0123"));
printf("\n%d", strtoint("0123ii"));
}
') { if (( * (s + index) >= '0') && * (s + index) <= '9') { flag = 1; index++; } else { flag = 0; break; } } if (flag == 1) return atoi(s); else return 0; } main() { printf("%d", strtoint("0123")); printf("\n%d", strtoint("0123ii")); }
Chitra
Chitra

Leave a Reply

Your email address will not be published. Required fields are marked *

Get the latest updates on your inbox

Be the first to receive the latest updates from Codesdoc by signing up to our email subscription.

    StudentProjects.in