Testing strings for equality with your own function - C Function

C examples for Function:Utility Function

Description

Testing strings for equality with your own function

Demo Code

#include <stdio.h>
#include <stdbool.h>

bool equalStrings (const char s1[], const char s2[]){
    int i = 0;//ww w  . j  a  va  2 s. co m
    bool areEqual;

    while (s1[i] == s2[i] && s1[i] != '\0' && s2[i] != '\0')
        ++i;

    if (s1[i] == '\0' && s2[i] == '\0')
        areEqual = true;
    else
        areEqual = false;

    return areEqual;
}

int main (void)
{
    char stra[] = "string test";
    char strb[] = "string";

    printf ("%i\n", equalStrings(stra, strb));
    printf ("%i\n", equalStrings(stra, stra));
    printf ("%i\n", equalStrings(strb, "string"));

    return 0;
}

Related Tutorials