C Language Practice: String Compare

From Eric & Company
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;
}