I'm self studying C with Practical C programming (3:rd) - O'Reilly.
I have the following exercise
"Exercise 9-2 : Write a function begins(string1,string2) that returns true if string1 begins string2. Write a program to test the function."
I write the following code to do it! Is this a correct and good code?
Code:
#include <stdio.h>
int begins(char str1[], char str2[])
{
int i, state;
for(i=0 ; str1[i] != '\0' ; i++)
{
if(str1[i] == str2[i])
{
state = 1;
}
else
{
state = 0;
break;
}
}
return(state);
}
int main()
{
printf("True : %i\n", begins("123", "12345"));
printf("False : %i\n", begins("1234", "122345"));
return(0);
}
I have the feeling that I could do this in another and better way for some reason.