# help with strings

• 09-19-2001
Unregistered
help with strings
I am not getting what the loops are doing in the following function concerning strings:

Code:

``` int alpha(char *mom, char *pop) {     char *m1, *p1;       for (m1 = mom; *m1; m1++)      // what is this doing???           {             for (p1 = pop; *p1; p1++)                   if (*m1 == *p1) break;        // what does this mean??                 if (*p1 == '\0') break;                                                                    // why break at these points?         }             return m1 - mom;                        // what is m1 - mom??? }```
assume values are called these different ways:::

alpha("rstu", "wxyz") what is alpha now??
alpha("rstu","wxy") what is alpha now??
i see the one string is shorter
than the other
when alpha is passed both strings mom and pop what does alpha return???

Please just help me get the notation in the for loops
and what happens with the line return m1 - mom ....
• 09-20-2001
kwigibo
what the whole function is actually doing is, it is comparing the strings and seeing how many of the letters are actually the same and returns the number of letters that are the same from the start. i.e try these and check the number returned.

"rstu","wxyz" (returns 0)
"wstu","wxyz" (returns 1)
"wxtu","wxyz" (returns 2)
"wxyu","wxyz" (returns 3)
"wxyz","wxyz" (returns 4)

Code:

``` #include <stdio.h> int alpha(char *mom, char *pop){     char *m1, *p1;       for (m1 = mom; *m1; m1++){             for (p1 = pop; *p1; p1++){//make sure u always put these in                 if (*m1 == *p1) break;             }             if (*p1 == '\0') break;         }       return m1 - mom; } int main(){     int s;     s=alpha("wxyz","wxyz");     printf("%d\n",s);     getchar(); }```
this is the code i used
• 09-20-2001
Unregistered
explain the for and if loops
Kwigibo

Could you explain the loops for me???

Code:

```#include <stdio.h> int alpha(char *mom, char *pop){     char *m1, *p1;       for (m1 = mom; *m1; m1++){    //what is this actually doing?             for (p1 = pop; *p1; p1++){                 if (*m1 == *p1) break;              //is the == operator             }                                                    //  comparing elements             if (*p1 == '\0') break;                    // at the pointers                  }       return m1 - mom;      //  and what is returning, step by step? } int main(){     int s;     s=alpha("wxyz","wxyz");     printf("%d\n",s);     getchar(); }```
• 09-20-2001
sballew
I tried to do this and get a Segmentation Fault (whatever that is)

Where is my error???

Code:

```#include <stdio.h> int f(char *, char *); main() {   char *p1, *p2;       printf("here's your output :\n");       f(p1,p2);       puts(p1);       puts(p2); }     int f(char *s, char *t)         {           char *p1, *p2;           for (p1 = s; *p1; p1++)               {for (p2 = t; *p2; p2++)                 {                   if (*p1 == *p2) break;                 }                                if (*p2 == '\0') break;               }           return p1 - s;     }```
HERE'S MY OUTPUT :::::