C Language Practice: String Compare

From Eric & Company
Revision as of 17:32, 12 March 2022 by Eric (talk | contribs) (Created page with "== Compare: If one string happens to match another string == * Step 1: Get a string compare function * Step 2: Call it and print it <syntaxhighlight lang="c" line style="border: 1px dashed red;"> #include <stdio.h> #include <stdlib.h> #include <string.h> int strcheck(char *str, char *check) { int ftime; int i, j; for (i = 0; str[i] != '\0'; i++) { for(j = 0; check[j] != '\0'; j++) { if (check[j] == str[i]) {...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Compare: If one string happens to match another string

  • Step 1: Get a string compare function
  • Step 2: Call it and print it
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int strcheck(char *str, char *check)
{
    int ftime;
    int i, j;
    for (i = 0; str[i] != '\0'; i++)
    {
        for(j = 0; check[j] != '\0'; j++)
        {
            if (check[j] == str[i])
            {
                printf("yes, ");
                return i += 1;
            }
        }
    }

    printf("strings do not match\n");
    return 0;
}

int main(void)
{
    char str[25];
    printf("please enter a string: ");
    scanf("%s", str);
    char check[25];
    printf("please enter another string: ");
    scanf("%s", check);

    printf("%d\n", strcheck(str, check));

    return 0;
}