~josealberto4444/learning-C

e727e5c7055f8a53c105ce641b5e7c1e1eb4b1de — José Alberto Orejuela García 3 years ago f34fd48
Add exercise (2-5)

Exercise 2-5:
Write the function any(s1 ,s2), which returns the first location in the
string s1 where any character from the string s2 occurs, or -1 if s1
contains no characters from s2. (The standard library function strpbrk
does the same job but returns a pointer to the location.)
1 files changed, 43 insertions(+), 0 deletions(-)

A 2-5/any.c
A 2-5/any.c => 2-5/any.c +43 -0
@@ 0,0 1,43 @@
#include <stdio.h>

/* charinstring: check if char is in string */
int
charinstring(int c, char s[])
{
	int i;

	for (i = 0; s[i] != '\0'; ++i) {
		if (s[i] == c) {
			return 1;
		}
	}

	return 0;
}

/* htoi: returns the first location in the string s where any character from
 * the string t occurs */
int
any(char s[], char t[])
{
	int i;

	for (i = 0; s[i] != '\0'; ++i) {
		if (charinstring(s[i], t)) {
			return i;
		}
	}

	return -1;
}

/* print some examples */
int
main()
{
	char s[] = "Hay una z en la posición número";

	printf("%s %d.\n", s, any(s, "z"));

	return 0;
}