[prev] 11 [next]

Exercise 1: Function to find the length of a string

Write MIPS code to implement:

char a[100];

int main(void)
{
   fgets(a, 100, stdin);
   printf("%d\n", length(a,100));
   return 0;
}
int length(char *s, int n)
{
   int nchars = 0;
   int *end = &s[n];
   while (s < end && *s != '\0')
      { s++; nchars++; }
   return nchars;
}