ft_putstr — Print a String
Allowed functions: write
Write a function that displays a string on the standard output.
The pointer passed to the function contains the address of the string's first character.
void ft_putstr(char *str);
Solution
Download ft_putstr.c#include <unistd.h>
void ft_putstr(char *str) {
while (*str) {
write(1, str, 1);
str++;
}
}
How It Works
Goal: Display a string to standard output one character at a time.
Approach: Loop through the string using a pointer, writing each character until the null terminator.
Step by step:
- Dereference the pointer to check if the current character is not
'\0'. - Write the current character to stdout.
- Increment the pointer to move to the next character and repeat.
Key concept: Pointer dereferencing and while loops — using *str to access the value at a pointer and incrementing the pointer to traverse a string.