ft_isnumber - 1Fr3aK2/Cub3d GitHub Wiki

📝 ft_isnumber

This function checks if a given string represents a valid number, meaning all characters in the string are digits.

⚙️ Parameters

Parameter Type Description
str char* The input string to be checked.

🔁 Returns

Return value Description
bool Returns true if the string contains only digits, otherwise false.

📖 Description

The ft_isnumber function iterates through each character of the input string to verify whether it is a digit. It returns true if all characters are digits, and false if any character is not a digit or if the string is NULL.

  • The function first checks if the input string is NULL. If it is, it immediately returns false.
  • It then loops through the string character by character.
  • For each character, it uses ft_isdigit to check if it is a numeric digit.
  • If a non-digit character is found, the function returns false.
  • If the loop completes without finding any non-digit characters, it returns true.

💡 Example Usage

char *str1 = "12345";
char *str2 = "12a45";

if (ft_isnumber(str1)) {
    printf("%s is a number.\n", str1); // This will print
}

if (!ft_isnumber(str2)) {
    printf("%s is not a number.\n", str2); // This will print
}