Skip to content

Instantly share code, notes, and snippets.

@yclim95
Created May 30, 2022 07:31
Show Gist options
  • Save yclim95/a7b3369c75cab3565323785f3fb61264 to your computer and use it in GitHub Desktop.
Save yclim95/a7b3369c75cab3565323785f3fb61264 to your computer and use it in GitHub Desktop.
ft_striteri() in c
#include <stdio.h>

void	ft_striteri(char *s, void (*f)(unsigned int, char *))
{
	unsigned int	i;

	if (!s || !f)
		return ;
	i = 0;
	while (s[i])
	{
		f(i, s + i);
		i++;
	}
}   

void    test(unsigned int i, char *str)
{
    printf("My inner function: index = %d and %s\n", i, str);
}

int main()
{
    char *str = "Hello 42KL";
    ft_striteri(str,test);
    printf("test: %s\n", str);

    return 0;
}
My inner function: index = 0 and Hello 42KL
My inner function: index = 1 and ello 42KL
My inner function: index = 2 and llo 42KL
My inner function: index = 3 and lo 42KL
My inner function: index = 4 and o 42KL
My inner function: index = 5 and  42KL
My inner function: index = 6 and 42KL
My inner function: index = 7 and 2KL
My inner function: index = 8 and KL
My inner function: index = 9 and L
test: Hello 42KL
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment