-5

This was an exercise included in an exam by my professor and I can't get my head around it. I thought it had to do with comparing the arrays of chars but that does not seem to be just that.

#include <stdio.h>

int f (char s[], char t[]) {
  int i, j;
  for (i=0; s[i] !='\0'; i++)
    for (j=0; t[j] !='\0'; j++)
      if (s[i]==t[j]) return i;
    return -1;
}

int main (void) {
 char a[] ="estate";
 char b[] ="vacanze";
 char c[] ="esame";
 char d[] ="onda";
 char e[] ="surf";

 printf ("Call result: %d\n", f(a, b));
 printf ("Call result: %d\n", f(a, c));
 printf ("Call result: %d\n", f(a, d));
 printf ("Call result: %d\n", f(d, e));

 return 0;
}
Winter
  • 3,894
  • 7
  • 24
  • 56

3 Answers3

1

The function f is doing some character-by-character comparison of 2 strings. It's not a strcmp, because the indexes i and j are not advancing simultaneously (i.e. it's not just comparing s[0] with t[0], s[1] with t[1], etc.)

Look closely at the nested loops. The j loop goes over all the positions in the second string for every value of i, which loops over the positions in the first string.

As soon as any matching character is found, the function returns.

So if s[0] matches any character in t, 0 will be returned.

If s[0] doesn't matching any character in t, but s[1] does match some character in t, 1 will be returned.

And so on. What gets returned is the index of the first character in s that matches any character in t.

If the 2 strings have no characters in common, the -1 at the end is returned.

Except for the -1 case and the return type, this function is the same as strcspn.

Since the array index operator is a form of pointer arithmetic, there is plenty of pointer arithmetic in the code already.

Overlooking that objection for the sake of exam points, one possible method of rewriting it proceeds like this:

/* Step 1: replace foo[bar] with *(foo+bar) */

int f1 (char s[], char t[]) {
  int i, j;
  for (i=0; *(s+i) !='\0'; i++)
    for (j=0; *(t+j) !='\0'; j++)
      if (*(s+i)==*(t+j)) return i;
    return -1;
}

/* Step 2: introduce new loop variables p and q. Every time the original
   code modifies i and j, you modify p and q so that p==s+i and q==t+j. */

int f2 (char s[], char t[]) {
  int i, j;
  char *p, *q;
  for (i=0,p=s; *(s+i) !='\0'; i++,p++)
    for (j=0,q=t; *(t+j) !='\0'; j++,q++)
      if (*(s+i)==*(t+j)) return i;
    return -1;
}

/* Step 3: Replace s+i with p and t+j with q. Replace i with p-s. These
   are correct because of the work you did in step 2. */

int f3 (char s[], char t[]) {
  int i, j;
  char *p, *q;
  for (i=0,p=s; *p !='\0'; i++,p++)
    for (j=0,q=t; *q !='\0'; j++,q++)
      if (*p==*q) return p-s;
    return -1;
}

/* Step 4: Get rid of i and j. You're not using them any more. */

int f4 (char s[], char t[]) {
  char *p, *q;
  for (p=s; *p !='\0'; p++)
    for (q=t; *q !='\0'; q++)
      if (*p==*q) return p-s;
    return -1;
}
0

It's a simple function to check if a character in a given array of character (s) is contained in another array of character (t), if so, the index of the matching character is returned, -1 otherwise.
You can transform it to pointer arithmetic this way :

int f (char* s, char* t) {
   for (int i=0; *(s+i) !='\0\'; i++)
       for (int j=0; *(t+j) !='\0'; j++)
           if (*(s+i) == *(t+j)) return i;
    return -1;
}
ollaw
  • 2,086
  • 1
  • 20
  • 33
0

That funtion only return index number and -1 and the loop keeps on going if it finds the character right then the loop gets cut and returns index number else -1 no curly braces means it will just read the first line of code. Its not cpmparing all it only checks the existence of atleast 1 char on another string. Example a and c will return index number instantly because index 0 of each array variable is "e" its not a comparison of all but just 1.

Christopher Pelayo
  • 792
  • 11
  • 30